List of usage examples for com.liferay.portal.kernel.servlet ServletResponseUtil sendFile
public static void sendFile(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String fileName, InputStream inputStream, long contentLength, String contentType, String contentDispositionType) throws IOException
From source file:com.liferay.adaptive.media.web.internal.servlet.AdaptiveMediaServlet.java
License:Open Source License
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { try {/*from ww w . ja v a 2s. c o m*/ AdaptiveMediaRequestHandler requestHandler = _adaptiveMediaRequestHandlerLocator .locateForPattern(_getRequestHandlerPattern(request)); if (requestHandler == null) { response.sendError(HttpServletResponse.SC_NOT_FOUND, request.getRequestURI()); return; } Optional<AdaptiveMedia<?>> adaptiveMediaOptional = requestHandler.handleRequest(request); AdaptiveMedia<?> adaptiveMedia = adaptiveMediaOptional .orElseThrow(AdaptiveMediaException.AdaptiveMediaNotFound::new); Optional<Integer> contentLengthOptional = adaptiveMedia .getValueOptional(AdaptiveMediaAttribute.contentLength()); Integer contentLength = contentLengthOptional.orElse(0); Optional<String> contentTypeOptional = adaptiveMedia .getValueOptional(AdaptiveMediaAttribute.contentType()); String contentType = contentTypeOptional.orElse(ContentTypes.APPLICATION_OCTET_STREAM); Optional<String> fileNameOptional = adaptiveMedia.getValueOptional(AdaptiveMediaAttribute.fileName()); String fileName = fileNameOptional.orElse(null); boolean download = ParamUtil.getBoolean(request, "download"); if (download) { ServletResponseUtil.sendFile(request, response, fileName, adaptiveMedia.getInputStream(), contentLength, contentType, HttpHeaders.CONTENT_DISPOSITION_ATTACHMENT); } else { ServletResponseUtil.sendFile(request, response, fileName, adaptiveMedia.getInputStream(), contentLength, contentType); } } catch (AdaptiveMediaException.AdaptiveMediaNotFound amnf) { response.sendError(HttpServletResponse.SC_NOT_FOUND, request.getRequestURI()); } catch (Exception e) { Throwable cause = e.getCause(); if (cause instanceof PrincipalException) { response.sendError(HttpServletResponse.SC_FORBIDDEN, request.getRequestURI()); } else { response.sendError(HttpServletResponse.SC_BAD_REQUEST, request.getRequestURI()); } } }
From source file:com.liferay.adaptive.media.web.internal.servlet.AMServlet.java
License:Open Source License
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { try {/*w w w. ja v a 2s . c o m*/ AMRequestHandler amRequestHandler = _amRequestHandlerLocator .locateForPattern(_getRequestHandlerPattern(request)); if (amRequestHandler == null) { response.sendError(HttpServletResponse.SC_NOT_FOUND, request.getRequestURI()); return; } Optional<AdaptiveMedia<?>> adaptiveMediaOptional = amRequestHandler.handleRequest(request); AdaptiveMedia<?> adaptiveMedia = adaptiveMediaOptional.orElseThrow(AMException.AMNotFound::new); Optional<Long> contentLengthOptional = adaptiveMedia .getValueOptional(AMAttribute.getContentLengthAMAttribute()); long contentLength = contentLengthOptional.orElse(0L); Optional<String> contentTypeOptional = adaptiveMedia .getValueOptional(AMAttribute.getContentTypeAMAttribute()); String contentType = contentTypeOptional.orElse(ContentTypes.APPLICATION_OCTET_STREAM); Optional<String> fileNameOptional = adaptiveMedia .getValueOptional(AMAttribute.getFileNameAMAttribute()); String fileName = fileNameOptional.orElse(null); boolean download = ParamUtil.getBoolean(request, "download"); if (download) { ServletResponseUtil.sendFile(request, response, fileName, adaptiveMedia.getInputStream(), contentLength, contentType, HttpHeaders.CONTENT_DISPOSITION_ATTACHMENT); } else { ServletResponseUtil.sendFile(request, response, fileName, adaptiveMedia.getInputStream(), contentLength, contentType); } } catch (AMException.AMNotFound amnf) { response.sendError(HttpServletResponse.SC_NOT_FOUND, request.getRequestURI()); } catch (Exception e) { Throwable cause = e.getCause(); if (cause instanceof PrincipalException) { response.sendError(HttpServletResponse.SC_FORBIDDEN, request.getRequestURI()); } else { response.sendError(HttpServletResponse.SC_BAD_REQUEST, request.getRequestURI()); } } }