Wed, 09 Sep 2020 14:19:14 -0400
8062947: Fix exception message to correctly represent LDAP connection failure
Reviewed-by: dfuchs, xyin, vtewari
1 /*
2 * Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 package com.sun.jndi.ldap;
28 import java.io.IOException;
29 import java.util.concurrent.BlockingQueue;
30 import java.util.concurrent.LinkedBlockingQueue;
31 import javax.naming.CommunicationException;
32 import javax.naming.NamingException;
33 import java.util.concurrent.TimeUnit;
35 final class LdapRequest {
37 private final static BerDecoder EOF = new BerDecoder(new byte[]{}, -1, 0);
38 private final static String CLOSE_MSG = "LDAP connection has been closed";
39 private final static String TIMEOUT_MSG_FMT = "LDAP response read timed out, timeout used: %d ms.";
41 LdapRequest next; // Set/read in synchronized Connection methods
42 final int msgId; // read-only
44 private final BlockingQueue<BerDecoder> replies;
45 private volatile boolean cancelled;
46 private volatile boolean closed;
47 private volatile boolean completed;
48 private final boolean pauseAfterReceipt;
50 LdapRequest(int msgId, boolean pause, int replyQueueCapacity) {
51 this.msgId = msgId;
52 this.pauseAfterReceipt = pause;
53 if (replyQueueCapacity == -1) {
54 this.replies = new LinkedBlockingQueue<>();
55 } else {
56 this.replies = new LinkedBlockingQueue<>(8 * replyQueueCapacity / 10);
57 }
58 }
60 void cancel() {
61 cancelled = true;
62 replies.offer(EOF);
63 }
65 synchronized void close() {
66 closed = true;
67 replies.offer(EOF);
68 }
70 private boolean isClosed() {
71 return closed && (replies.size() == 0 || replies.peek() == EOF);
72 }
74 synchronized boolean addReplyBer(BerDecoder ber) {
75 // check the closed boolean value here as we don't want anything
76 // to be added to the queue after close() has been called.
77 if (cancelled || closed) {
78 return false;
79 }
81 // peek at the BER buffer to check if it is a SearchResultDone PDU
82 try {
83 ber.parseSeq(null);
84 ber.parseInt();
85 completed = (ber.peekByte() == LdapClient.LDAP_REP_RESULT);
86 } catch (IOException e) {
87 // ignore
88 }
89 ber.reset();
91 // Add a new reply to the queue of unprocessed replies.
92 try {
93 replies.put(ber);
94 } catch (InterruptedException e) {
95 // ignore
96 }
98 return pauseAfterReceipt;
99 }
101 /**
102 * Read reply BER
103 * @param millis timeout, infinite if the value is negative
104 * @return BerDecoder if reply was read successfully
105 * @throws CommunicationException request has been canceled and request does not need to be abandoned
106 * @throws NamingException request has been closed or timed out. Request does need to be abandoned
107 * @throws InterruptedException LDAP operation has been interrupted
108 */
109 BerDecoder getReplyBer(long millis) throws NamingException,
110 InterruptedException {
111 if (cancelled) {
112 throw new CommunicationException("Request: " + msgId +
113 " cancelled");
114 }
115 if (isClosed()) {
116 throw new NamingException(CLOSE_MSG);
117 }
119 BerDecoder result = millis > 0 ?
120 replies.poll(millis, TimeUnit.MILLISECONDS) : replies.take();
122 if (cancelled) {
123 throw new CommunicationException("Request: " + msgId +
124 " cancelled");
125 }
127 // poll from 'replies' blocking queue ended-up with timeout
128 if (result == null) {
129 throw new NamingException(String.format(TIMEOUT_MSG_FMT, millis));
130 }
131 // Unexpected EOF can be caused by connection closure or cancellation
132 if (result == EOF) {
133 throw new NamingException(CLOSE_MSG);
134 }
135 return result;
136 }
138 boolean hasSearchCompleted() {
139 return completed;
140 }
141 }