use of javax.servlet.ServletOutputStream in project leopard by tanhaichao.
the class FlushServlet method doGet.
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String category = request.getParameter("category");
System.out.println("size:" + response.getBufferSize());
System.out.println("response:" + response.getClass());
// response.setBufferSize(10);
System.out.println("size:" + response.getBufferSize());
// response.setHeader("Transfer-Encoding", "chunked");
response.setContentType("text/html; charset=UTF-8");
// response.setContentLength(10);
ServletOutputStream out = response.getOutputStream();
out.println("<!-- -->");
out.println("<!-- -->");
out.println("<!-- -->");
out.println("start");
for (int i = 0; i < 10; i++) {
String line = i + ":linelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelineline time:" + new Date().toString() + "\n";
out.println(line + "");
out.flush();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// out.flush();
out.close();
}
use of javax.servlet.ServletOutputStream in project leopard by tanhaichao.
the class FileCacheView method createView.
protected JstlView createView(String viewName) {
return new JstlView("/WEB-INF/jsp/" + viewName + ".jsp") {
@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
ServletOutputStream output = response.getOutputStream();
ServletContext servletContext = request.getServletContext();
super.setServletContext(servletContext);
CacheResponseWrapper cacheResponseWrapper = new CacheResponseWrapper(response);
// System.err.println("renderMergedOutputModel");
super.renderMergedOutputModel(model, request, cacheResponseWrapper);
byte[] data = cacheResponseWrapper.getResponseData();
String content = new String(data);
response(content);
output.write(data);
output.flush();
}
};
}
use of javax.servlet.ServletOutputStream in project openremote by openremote.
the class JSAPIWriter method writeJavaScript.
public void writeJavaScript(String base, HttpServletRequest req, HttpServletResponse resp, Map<String, ServiceRegistry> serviceRegistries) throws IOException {
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("Starting JS API client");
}
// RESTEASY-776
// before writing generated javascript, we generate Etag and compare it with client request.
// If nothing changed, we send back 304 Not Modified for client browser to use cached js.
String ifNoneMatch = req.getHeader("If-None-Match");
String etag = generateEtag(serviceRegistries);
resp.setHeader("Etag", etag);
if (ifNoneMatch != null && ifNoneMatch.equals(etag)) {
resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
return;
}
for (Map.Entry<String, ServiceRegistry> entry : serviceRegistries.entrySet()) {
String uri = base;
if (entry.getKey() != null)
uri += entry.getKey();
StringWriter stringWriter = new StringWriter();
PrintWriter writer = new PrintWriter(new BufferedWriter(stringWriter));
writeJavaScript(uri, writer, entry.getValue());
writer.flush();
writer.close();
if (clientIsGzipSupported(req)) {
ByteArrayOutputStream compressedContent = new ByteArrayOutputStream();
GZIPOutputStream gzipstream = new GZIPOutputStream(compressedContent);
gzipstream.write(stringWriter.toString().getBytes(Charset.forName("utf-8")));
gzipstream.finish();
// get the compressed content
byte[] compressedBytes = compressedContent.toByteArray();
// set appropriate HTTP headers
resp.setContentLength(compressedBytes.length);
resp.addHeader("Content-Encoding", "gzip");
ServletOutputStream output = resp.getOutputStream();
output.write(compressedBytes);
output.flush();
output.close();
} else {
ServletOutputStream output = resp.getOutputStream();
byte[] bytes = stringWriter.toString().getBytes(Charset.forName("utf-8"));
resp.setContentLength(bytes.length);
output.write(bytes);
output.flush();
output.close();
}
}
}
use of javax.servlet.ServletOutputStream in project pentaho-kettle by pentaho.
the class AllocateServerSocketServletTest method testAllocateServerSocketServletEncodesParametersForHmtlResponse.
@Test
@PrepareForTest({ Encode.class })
public void testAllocateServerSocketServletEncodesParametersForHmtlResponse() throws ServletException, IOException {
HttpServletRequest mockRequest = mock(HttpServletRequest.class);
HttpServletResponse mockResponse = mock(HttpServletResponse.class);
SocketPortAllocation mockSocketPortAllocation = mock(SocketPortAllocation.class);
PowerMockito.spy(Encode.class);
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ServletOutputStream servletOutputStream = new ServletOutputStream() {
@Override
public void write(int b) throws IOException {
byteArrayOutputStream.write(b);
}
};
when(mockRequest.getContextPath()).thenReturn(AllocateServerSocketServlet.CONTEXT_PATH);
when(mockRequest.getParameter(anyString())).thenReturn(ServletTestUtils.BAD_STRING_TO_TEST);
when(mockResponse.getOutputStream()).thenReturn(servletOutputStream);
when(mockTransformationMap.allocateServerSocketPort(anyInt(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString())).thenReturn(mockSocketPortAllocation);
allocateServerSocketServlet.doGet(mockRequest, mockResponse);
String response = byteArrayOutputStream.toString();
// Pull out dynamic part of body, remove hardcoded html
String dynamicBody = ServletTestUtils.getInsideOfTag("BODY", response).replaceAll("<p>", "").replaceAll("<br>", "").replaceAll("<H1>.+</H1>", "").replaceAll("--> port", "");
assertFalse(ServletTestUtils.hasBadText(dynamicBody));
PowerMockito.verifyStatic(atLeastOnce());
Encode.forHtml(anyString());
}
use of javax.servlet.ServletOutputStream in project pentaho-kettle by pentaho.
the class AllocateServerSocketServletIT method testAllocateServerSocketServletEncodesParametersForHmtlResponse.
@Test
public void testAllocateServerSocketServletEncodesParametersForHmtlResponse() throws ServletException, IOException {
HttpServletRequest mockRequest = mock(HttpServletRequest.class);
HttpServletResponse mockResponse = mock(HttpServletResponse.class);
SocketPortAllocation mockSocketPortAllocation = mock(SocketPortAllocation.class);
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ServletOutputStream servletOutputStream = new ServletOutputStream() {
@Override
public void write(int b) throws IOException {
byteArrayOutputStream.write(b);
}
};
when(mockRequest.getContextPath()).thenReturn(AllocateServerSocketServlet.CONTEXT_PATH);
when(mockRequest.getParameter(anyString())).thenReturn(ServletTestUtils.BAD_STRING);
when(mockResponse.getOutputStream()).thenReturn(servletOutputStream);
when(mockTransformationMap.allocateServerSocketPort(anyInt(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString())).thenReturn(mockSocketPortAllocation);
allocateServerSocketServlet.doGet(mockRequest, mockResponse);
String response = byteArrayOutputStream.toString();
// Pull out dynamic part of body, remove hardcoded html
String dynamicBody = ServletTestUtils.getInsideOfTag("BODY", response).replaceAll("<p>", "").replaceAll("<br>", "").replaceAll("<H1>.+</H1>", "").replaceAll("--> port", "");
assertFalse(ServletTestUtils.hasBadText(dynamicBody));
}
Aggregations