1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.connector;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Objects;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.WriteListener;
import org.apache.tomcat.util.res.StringManager;
/**
* Coyote implementation of the servlet output stream.
*/
public class CoyoteOutputStream extends ServletOutputStream {
protected static final StringManager sm = StringManager.getManager(CoyoteOutputStream.class);
// ----------------------------------------------------- Instance Variables
protected OutputBuffer ob;
// ----------------------------------------------------------- Constructors
protected CoyoteOutputStream(OutputBuffer ob) {
this.ob = ob;
}
// --------------------------------------------------------- Public Methods
/**
* Prevent cloning the facade.
*/
@Override
protected Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}
// -------------------------------------------------------- Package Methods
/**
* Clear facade.
*/
void clear() {
ob = null;
}
// --------------------------------------------------- OutputStream Methods
@Override
public void write(int i) throws IOException {
boolean nonBlocking = checkNonBlockingWrite();
try {
ob.writeByte(i);
} catch (IOException ioe) {
handleIOException(ioe);
}
if (nonBlocking) {
checkRegisterForWrite();
}
}
@Override
public void write(byte[] b) throws IOException {
write(b, 0, b.length);
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
boolean nonBlocking = checkNonBlockingWrite();
try {
ob.write(b, off, len);
} catch (IOException ioe) {
handleIOException(ioe);
}
if (nonBlocking) {
checkRegisterForWrite();
}
}
public void write(ByteBuffer from) throws IOException {
Objects.requireNonNull(from);
boolean nonBlocking = checkNonBlockingWrite();
try {
ob.write(from);
} catch (IOException ioe) {
handleIOException(ioe);
}
if (nonBlocking) {
checkRegisterForWrite();
}
}
/**
* Will send the buffer to the client.
*/
@Override
public void flush() throws IOException {
boolean nonBlocking = checkNonBlockingWrite();
try {
ob.flush();
} catch (IOException ioe) {
handleIOException(ioe);
}
if (nonBlocking) {
checkRegisterForWrite();
}
}
/**
* Checks for concurrent writes which are not permitted. This object has no state information so the call chain is
* CoyoteOutputStream->OutputBuffer->CoyoteResponse.
*
* @return <code>true</code> if this OutputStream is currently in non-blocking mode.
*/
private boolean checkNonBlockingWrite() {
boolean nonBlocking = !ob.isBlocking();
if (nonBlocking && !ob.isReady()) {
throw new IllegalStateException(sm.getString("coyoteOutputStream.nbNotready"));
}
return nonBlocking;
}
/**
* Checks to see if there is data left in the Coyote output buffers (NOT the servlet output buffer) and if so
* registers the associated socket for write so the buffers will be emptied. The container will take care of this.
* As far as the app is concerned, there is a non-blocking write in progress. It doesn't have visibility of whether
* the data is buffered in the socket buffer or the Coyote buffers.
*/
private void checkRegisterForWrite() {
ob.checkRegisterForWrite();
}
@Override
public void close() throws IOException {
try {
ob.close();
} catch (IOException ioe) {
handleIOException(ioe);
}
}
@Override
public boolean isReady() {
if (ob == null) {
throw new IllegalStateException(sm.getString("coyoteOutputStream.null"));
}
return ob.isReady();
}
@Override
public void setWriteListener(WriteListener listener) {
ob.setWriteListener(listener);
}
private void handleIOException(IOException ioe) throws IOException {
try {
ob.setErrorException(ioe);
} catch (NullPointerException npe) {
/*
* Ignore.
*
* An IOException on a non-container thread during asynchronous Servlet processing will trigger a dispatch
* to a container thread that will complete the asynchronous processing and recycle the request, response
* and associated objects including the OutputBuffer. Depending on timing it is possible that the
* OutputBuffer will have been cleared by the time the call above is made - resulting in an NPE.
*
* If the OutputBuffer is null then there is no need to call setErrorException(). Catching and ignoring the
* NPE is (for now at least) a simpler solution than adding locking to OutputBuffer to ensure it is non-null
* and remains non-null while setErrorException() is called.
*
* The longer term solution is likely a refactoring and clean-up of error handling for asynchronous requests
* but that is potentially a significant piece of work.
*/
}
throw ioe;
}
}
|