@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Assert.state(this.liveBeansView != null, "No LiveBeansView available"); String content = this.liveBeansView.getSnapshotAsJson(); response.setContentType("application/json"); response.setContentLength(content.length()); response.getWriter().write(content); }
/** * Write the given temporary OutputStream to the HTTP response. * @param response current HTTP response * @param baos the temporary OutputStream to write * @throws IOException if writing/flushing failed */ protected void writeToResponse(HttpServletResponse response, ByteArrayOutputStream baos) throws IOException { // Write content type and also length (determined via byte array). response.setContentType(getContentType()); response.setContentLength(baos.size()); // Flush byte array to servlet output stream. ServletOutputStream out = response.getOutputStream(); baos.writeTo(out); out.flush(); }
@Override protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception { Object toBeMarshalled = locateToBeMarshalled(model); if (toBeMarshalled == null) { throw new IllegalStateException("Unable to locate object to be marshalled in model: " + model); } Assert.state(this.marshaller != null, "No Marshaller set"); ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); this.marshaller.marshal(toBeMarshalled, new StreamResult(baos)); setResponseContentType(request, response); response.setContentLength(baos.size()); baos.writeTo(response.getOutputStream()); }
/** * Redirect to HTTP port. */ @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String host = System.getProperty("http.host"); String port = System.getProperty("http.port"); if ("0.0.0.0".equals(host)) { host = "127.0.0.1"; } resp.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); resp.addHeader("Location", "http://" + host + ":" + port); resp.setContentType("text/plain"); String message = "Relocated to http://" + host + ":" + port; resp.setContentLength(message.length()); resp.getWriter().write(message); resp.flushBuffer(); }
resp.setContentType("message/http"); resp.setContentLength(responseLength); ServletOutputStream out = resp.getOutputStream(); out.print(responseString); out.close();
/** * Prepares response for various provided data. * * @param response http response * @param fileName file name, if full path then file name will be stripped, if null, will be ignored. * @param mimeType mime type with optional charset, may be <code>null</code> * @param fileSize if less then 0 it will be ignored */ public static void prepareResponse(final HttpServletResponse response, final String fileName, String mimeType, final int fileSize) { if ((mimeType == null) && (fileName != null)) { String extension = FileNameUtil.getExtension(fileName); mimeType = MimeTypes.getMimeType(extension); } if (mimeType != null) { response.setContentType(mimeType); } if (fileSize >= 0) { response.setContentLength(fileSize); } // support internationalization // See https://tools.ietf.org/html/rfc6266#section-5 for more information. if (fileName != null) { String name = FileNameUtil.getName(fileName); String encodedFileName = URLCoder.encode(name); response.setHeader(CONTENT_DISPOSITION, "attachment;filename=\"" + name + "\";filename*=utf8''" + encodedFileName); } }
response.setContentLength((int) length); response.setContentType(mediaType.toString()); for (String headerValue : headerValues) { if (first) { response.setHeader(headerName, headerValue); response.setHeader(HttpHeaders.ACCEPT_RANGES, "bytes");
public void service(HttpServletRequest req, HttpServletResponse res) throws IOException { String xmlToReturn = String.format(xml, new Date().toString()); res.setStatus(200); res.addHeader("Content-Type", contentType); res.addHeader("X-Method", req.getMethod()); res.addHeader("MultiValue", "1"); res.addHeader("MultiValue", "2"); res.addHeader("MultiValue", "3"); OutputStream os = res.getOutputStream(); byte[] retVal = xmlToReturn.getBytes(); res.setContentLength(retVal.length); os.write(retVal); os.close(); } }
@Override protected void renderMergedOutputModel(Map<String, Object> model, // HttpServletRequest request, // HttpServletResponse response) throws Exception { Object value = filterModel(model); String jsonpParameterValue = getJsonpParameterValue(request); if(jsonpParameterValue != null) { JSONPObject jsonpObject = new JSONPObject(jsonpParameterValue); jsonpObject.addParameter(value); value = jsonpObject; } ByteArrayOutputStream outnew = new ByteArrayOutputStream(); int len = JSON.writeJSONString(outnew, // fastJsonConfig.getCharset(), // value, // fastJsonConfig.getSerializeConfig(), // fastJsonConfig.getSerializeFilters(), // fastJsonConfig.getDateFormat(), // JSON.DEFAULT_GENERATE_FEATURE, // fastJsonConfig.getSerializerFeatures()); if (this.updateContentLength) { // Write content length (determined via byte array). response.setContentLength(len); } // Flush byte array to servlet output stream. ServletOutputStream out = response.getOutputStream(); outnew.writeTo(out); outnew.close(); out.flush(); }
response.setHeader("Content-type", "text/javascript"); response.setHeader("Content-type", "text/css"); response.setHeader("Vary", "Accept-Encoding"); // Handle proxies response.setContentLength(content.length); if (compress) { response.setHeader("Content-Encoding", "gzip"); try (OutputStream out = response.getOutputStream()) {
private void injectHtmlToOutputStream(HttpServletResponse response, HttpServletRequest httpServletRequest, HttpServletResponseBufferWrapper httpServletResponseBufferWrapper) throws IOException { logger.debug("injectHtmlToOutputStream - encoding={}", response.getCharacterEncoding()); String content = new String(httpServletResponseBufferWrapper.getOutputStream().getOutput().toByteArray(), response.getCharacterEncoding()); if (content.contains("</body>")) { httpServletRequest.setAttribute("stagemonitorInjected", true); content = getContentToInject(httpServletRequest, content); final byte[] bytes = content.getBytes(response.getCharacterEncoding()); response.setContentLength(bytes.length); response.getOutputStream().write(bytes); } else { // this is no html passthrough(response, httpServletResponseBufferWrapper); } }
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) { resp.setStatus(404); resp.setContentLength(0); } };
protected void setDownloadResponse(String downloadFile, final HttpServletResponse response) { File file = new File(downloadFile); try (InputStream fileInputStream = new FileInputStream(file); OutputStream output = response.getOutputStream()) { response.reset(); response.setContentType("application/octet-stream"); response.setContentLength((int) (file.length())); response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\""); IOUtils.copyLarge(fileInputStream, output); output.flush(); } catch (IOException e) { throw new InternalErrorException("Failed to download file: " + e.getMessage(), e); } }
@Override public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { baseRequest.setHandled(true); if ("DELETE".equalsIgnoreCase(request.getMethod())) { final int status = Integer.valueOf(target.substring("/status".length() + 1)); response.setStatus(status); response.setContentLength(0); } else { response.setStatus(404); response.setContentType("text/plain"); response.setContentLength(0); } } }
if (resultSet.next()) { byte[] content = resultSet.getBytes("content"); response.setContentType(getServletContext().getMimeType(imageName)); response.setContentLength(content.length); response.getOutputStream().write(content); } else {
response.setContentType("application/octet-stream"); response.setHeader("Cache-Control", "no-cache"); response.setContentLength(0);
protected void emitStaticFileToResponse(HttpServletRequest request, HttpServletResponse response, File targetFile) throws IOException { long byteSize = targetFile.length(); response.addHeader("Content-Encoding", "gzip"); response.setContentLength(Long.valueOf(byteSize).intValue()); if (sendFileSize > byteSize || !useSendFile) { //don't use sendFile for small files StreamUtils.copy(new FileInputStream(targetFile), response.getOutputStream()); } else { request.setAttribute("org.apache.tomcat.sendfile.filename", targetFile.getAbsolutePath()); request.setAttribute("org.apache.tomcat.sendfile.start", 0L); request.setAttribute("org.apache.tomcat.sendfile.end", byteSize); } }
response.setContentLength((int) length); response.setContentType(mediaType.toString()); for (String headerValue : headerValues) { if (first) { response.setHeader(headerName, headerValue); response.setHeader(HttpHeaders.ACCEPT_RANGES, "bytes");
protected void writeResponseMessage(StreamResponseMessage responseMessage) throws IOException { if (log.isLoggable(Level.FINER)) log.finer("Sending HTTP response status: " + responseMessage.getOperation().getStatusCode()); getResponse().setStatus(responseMessage.getOperation().getStatusCode()); // Headers for (Map.Entry<String, List<String>> entry : responseMessage.getHeaders().entrySet()) { for (String value : entry.getValue()) { getResponse().addHeader(entry.getKey(), value); } } // The Date header is recommended in UDA getResponse().setDateHeader("Date", System.currentTimeMillis()); // Body byte[] responseBodyBytes = responseMessage.hasBody() ? responseMessage.getBodyBytes() : null; int contentLength = responseBodyBytes != null ? responseBodyBytes.length : -1; if (contentLength > 0) { getResponse().setContentLength(contentLength); log.finer("Response message has body, writing bytes to stream..."); IO.writeBytes(getResponse().getOutputStream(), responseBodyBytes); } }
/** * Sends response to client * * @param resp * Response * @param packet * Remoting packet * @throws Exception * General exception */ protected void sendResponse(HttpServletResponse resp, RemotingPacket packet) throws Exception { log.debug("Sending response"); IoBuffer respBuffer = codecFactory.getRemotingEncoder().encode(packet); if (respBuffer != null) { final ServletOutputStream out = resp.getOutputStream(); resp.setContentLength(respBuffer.limit()); ServletUtils.copy(respBuffer.asInputStream(), out); out.flush(); out.close(); respBuffer.free(); respBuffer = null; } else { log.info("Response buffer was null after encoding"); } }