use of javax.servlet.ServletInputStream in project jetty.project by eclipse.
the class ProxyServletTest method testExpect100ContinueRespond100Continue.
@Test
public void testExpect100ContinueRespond100Continue() throws Exception {
CountDownLatch serverLatch1 = new CountDownLatch(1);
CountDownLatch serverLatch2 = new CountDownLatch(1);
startServer(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
serverLatch1.countDown();
try {
serverLatch2.await(5, TimeUnit.SECONDS);
} catch (Throwable x) {
throw new InterruptedIOException();
}
// Send the 100 Continue.
ServletInputStream input = request.getInputStream();
// Echo the content.
IO.copy(input, response.getOutputStream());
}
});
startProxy();
startClient();
byte[] content = new byte[1024];
CountDownLatch contentLatch = new CountDownLatch(1);
CountDownLatch clientLatch = new CountDownLatch(1);
client.newRequest("localhost", serverConnector.getLocalPort()).header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()).content(new BytesContentProvider(content)).onRequestContent((request, buffer) -> contentLatch.countDown()).send(new BufferingResponseListener() {
@Override
public void onComplete(Result result) {
if (result.isSucceeded()) {
if (result.getResponse().getStatus() == HttpStatus.OK_200) {
if (Arrays.equals(content, getContent()))
clientLatch.countDown();
}
}
}
});
// Wait until we arrive on the server.
Assert.assertTrue(serverLatch1.await(5, TimeUnit.SECONDS));
// The client should not send the content yet.
Assert.assertFalse(contentLatch.await(1, TimeUnit.SECONDS));
// Make the server send the 100 Continue.
serverLatch2.countDown();
// The client has sent the content.
Assert.assertTrue(contentLatch.await(5, TimeUnit.SECONDS));
Assert.assertTrue(clientLatch.await(5, TimeUnit.SECONDS));
}
use of javax.servlet.ServletInputStream in project jetty.project by eclipse.
the class ProxyServletTest method testExpect100ContinueRespond100ContinueSomeRequestContentThenFailure.
@Test
public void testExpect100ContinueRespond100ContinueSomeRequestContentThenFailure() throws Exception {
CountDownLatch serverLatch = new CountDownLatch(1);
startServer(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Send the 100 Continue.
ServletInputStream input = request.getInputStream();
try {
// Echo the content.
IO.copy(input, response.getOutputStream());
} catch (IOException x) {
serverLatch.countDown();
}
}
});
startProxy();
startClient();
long idleTimeout = 1000;
client.setIdleTimeout(idleTimeout);
byte[] content = new byte[1024];
new Random().nextBytes(content);
int chunk1 = content.length / 2;
DeferredContentProvider contentProvider = new DeferredContentProvider();
contentProvider.offer(ByteBuffer.wrap(content, 0, chunk1));
CountDownLatch clientLatch = new CountDownLatch(1);
client.newRequest("localhost", serverConnector.getLocalPort()).header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()).content(contentProvider).send(result -> {
if (result.isFailed())
clientLatch.countDown();
});
// Wait more than the idle timeout to break the connection.
Thread.sleep(2 * idleTimeout);
Assert.assertTrue(serverLatch.await(555, TimeUnit.SECONDS));
Assert.assertTrue(clientLatch.await(555, TimeUnit.SECONDS));
}
use of javax.servlet.ServletInputStream in project Openfire by igniterealtime.
the class WebDAVLiteServlet method doPut.
/**
* Handles a PUT request for uploading files.
*
* @param request Object representing the HTTP request.
* @param response Object representing the HTTP response.
* @throws ServletException If there was a servlet related exception.
* @throws IOException If there was an IO error while setting the error.
*/
@Override
protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Verify authentication
if (!isAuthenticated(request, response))
return;
String path = request.getPathInfo();
Log.debug("WebDAVLiteServlet: PUT with path = " + path);
if (request.getContentLength() <= 0) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
if (path == null || !path.startsWith("/rooms/")) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
String[] pathPcs = path.split("/");
if (pathPcs.length != 5) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
String service = pathPcs[2];
String room = pathPcs[3];
String filename = pathPcs[4];
// Verify authorization
if (!isAuthorized(request, response, service, room))
return;
Log.debug("WebDAVListServlet: Service = " + service + ", room = " + room + ", file = " + filename);
File file = getFileReference(service, room, filename);
Boolean overwriteFile = file.exists();
FileOutputStream fileStream = new FileOutputStream(file, false);
ServletInputStream inputStream = request.getInputStream();
byte[] byteArray = new byte[request.getContentLength()];
int bytesRead = 0;
while (bytesRead != -1) {
bytesRead = inputStream.read(byteArray, bytesRead, request.getContentLength());
}
fileStream.write(byteArray);
fileStream.close();
inputStream.close();
if (overwriteFile) {
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
response.setHeader("Location", request.getRequestURI());
} else {
response.setStatus(HttpServletResponse.SC_CREATED);
response.setHeader("Location", request.getRequestURI());
}
}
use of javax.servlet.ServletInputStream in project iosched by google.
the class SendMessageServlet method readBody.
private String readBody(HttpServletRequest req) throws IOException {
ServletInputStream inputStream = req.getInputStream();
java.util.Scanner s = new java.util.Scanner(inputStream).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}
use of javax.servlet.ServletInputStream in project OpenRefine by OpenRefine.
the class RefineBrokerTests method call.
private JSONObject call(boolean successful, RefineBroker broker, HttpServletRequest request, HttpServletResponse response, String service, String... params) throws Exception {
if (params != null) {
for (int i = 0; i < params.length; ) {
String name = params[i++];
String value = params[i++];
if ("data".equals(name)) {
final ByteArrayInputStream inputStream = new ByteArrayInputStream(value.getBytes("UTF-8"));
when(request.getInputStream()).thenReturn(new ServletInputStream() {
public int read() throws IOException {
return inputStream.read();
}
});
} else {
when(request.getParameter(name)).thenReturn(value);
}
}
}
StringWriter writer = new StringWriter();
when(response.getWriter()).thenReturn(new PrintWriter(writer));
broker.process(service, request, response);
JSONObject result = new JSONObject(writer.toString());
if (successful) {
assertJSON(result, "status", "ok");
} else {
assertJSON(result, "status", "error");
}
logger.info(result.toString());
return result;
}
Aggregations