Java Code Examples for javax.servlet.http.HttpServletResponse#setContentLengthLong()

The following examples show how to use javax.servlet.http.HttpServletResponse#setContentLengthLong() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Tomcat8-Source-Read   File: Http2TestBase.java    License: MIT License 6 votes vote down vote up
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    // Generate content with a simple known format that will exceed the
    // default flow control window for a stream.
    resp.setContentType("application/octet-stream");

    int count = 128 * 1024;
    // Two bytes per entry
    resp.setContentLengthLong(count * 2);

    OutputStream os = resp.getOutputStream();
    byte[] data = new byte[2];
    for (int i = 0; i < count; i++) {
        data[0] = (byte) (i & 0xFF);
        data[1] = (byte) ((i >> 8) & 0xFF);
        os.write(data);
    }
}
 
Example 2
Source Project: sk-admin   File: FileController.java    License: Apache License 2.0 6 votes vote down vote up
@RequestMapping(value = "/view/{id}", method = RequestMethod.GET)
@ApiOperation(value = "本地存储预览文件")
public void view(@PathVariable String id, HttpServletResponse response) throws IOException {

    File file = fileService.getById(id);
    if (file == null) {
        throw new SkException("文件ID:" + id + "不存在");
    }
    response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(file.getFKey(), "UTF-8"));
    response.setContentLengthLong(file.getSize());
    response.setContentType(file.getType());
    response.addHeader("Accept-Ranges", "bytes");
    if (file.getSize() > 0) {
        response.addHeader("Content-Range", "bytes " + 0 + "-" + (file.getSize() - 1) + "/" + file.getSize());
    }
    LocalFileManage.view(file.getUrl(), response);
}
 
Example 3
private static ResponseEntity<InputStream> handleFullFileRequest(final AbstractDbArtifact artifact,
        final String filename, final HttpServletResponse response,
        final FileStreamingProgressListener progressListener, final ByteRange full) {
    final ByteRange r = full;
    response.setHeader(HttpHeaders.CONTENT_RANGE, "bytes " + r.getStart() + "-" + r.getEnd() + "/" + r.getTotal());
    response.setContentLengthLong(r.getLength());

    try (InputStream from = artifact.getFileInputStream()) {
        final ServletOutputStream to = response.getOutputStream();
        copyStreams(from, to, progressListener, r.getStart(), r.getLength(), filename);
    } catch (final IOException e) {
        throw new FileStreamingFailedException("fullfileRequest " + filename, e);
    }

    return ResponseEntity.ok().build();
}
 
Example 4
Source Project: youkefu   File: MediaController.java    License: Apache License 2.0 6 votes vote down vote up
@RequestMapping("/voice")
    @Menu(type = "resouce" , subtype = "voice" , access = true)
    public void voice(HttpServletResponse response, @Valid String id) throws IOException {
    	File file = new File(path ,id) ;
//    	response.setContentType(Files.probeContentType(Paths.get(file.getAbsolutePath())));  
    	response.setHeader("Content-Type" , "application/octet-stream");
    	response.setHeader("Content-Lenght" , String.valueOf(file.length()));
    	response.setContentLengthLong(file.length());
        response.setHeader("Content-Disposition", "attachment;filename="+java.net.URLEncoder.encode(file.getName(), "UTF-8"));  
        if(file.exists() && file.isFile()){
    		FileInputStream input = new FileInputStream(file) ;
    		OutputStream output = response.getOutputStream() ; 
    		byte[] data = new byte[1024] ;
    		int len = 0 ;
    		while((len = input.read(data))>0) {
    			output.write(data , 0  , len);
    		}
    		output.flush();
    		input.close();
    	}
    }
 
Example 5
@Override
protected void applyHeaders() {
	HttpServletResponse response = getNativeResponse();
	MediaType contentType = getHeaders().getContentType();
	if (response.getContentType() == null && contentType != null) {
		response.setContentType(contentType.toString());
	}
	Charset charset = (contentType != null ? contentType.getCharset() : null);
	if (response.getCharacterEncoding() == null && charset != null) {
		response.setCharacterEncoding(charset.name());
	}
	long contentLength = getHeaders().getContentLength();
	if (contentLength != -1) {
		response.setContentLengthLong(contentLength);
	}
}
 
Example 6
@Override
protected void applyHeaders() {
	MediaType contentType = getHeaders().getContentType();
	HttpServletResponse response = getNativeResponse();
	if (response.getContentType() == null && contentType != null) {
		response.setContentType(contentType.toString());
	}
	Charset charset = (contentType != null ? contentType.getCharset() : null);
	if (response.getCharacterEncoding() == null && charset != null) {
		response.setCharacterEncoding(charset.name());
	}
	long contentLength = getHeaders().getContentLength();
	if (contentLength != -1) {
		response.setContentLengthLong(contentLength);
	}
}
 
Example 7
/**
 * Set headers on the given servlet response.
 * Called for GET requests as well as HEAD requests.
 * @param response current servlet response
 * @param resource the identified resource (never {@code null})
 * @param mediaType the resource's media type (never {@code null})
 * @throws IOException in case of errors while setting the headers
 */
protected void setHeaders(HttpServletResponse response, Resource resource, MediaType mediaType) throws IOException {
	long length = resource.contentLength();
	if (length > Integer.MAX_VALUE) {
		if (contentLengthLongAvailable) {
			response.setContentLengthLong(length);
		}
		else {
			response.setHeader(HttpHeaders.CONTENT_LENGTH, Long.toString(length));
		}
	}
	else {
		response.setContentLength((int) length);
	}

	if (mediaType != null) {
		response.setContentType(mediaType.toString());
	}
	if (resource instanceof EncodedResource) {
		response.setHeader(HttpHeaders.CONTENT_ENCODING, ((EncodedResource) resource).getContentEncoding());
	}
	if (resource instanceof VersionedResource) {
		response.setHeader(HttpHeaders.ETAG, "\"" + ((VersionedResource) resource).getVersion() + "\"");
	}
	response.setHeader(HttpHeaders.ACCEPT_RANGES, "bytes");
}
 
Example 8
public static void streamArtifact ( final HttpServletResponse response, final ArtifactInformation artifact, final InputStream stream, final Optional<String> mimetype, final boolean download, final Function<ArtifactInformation, String> nameFunc ) throws IOException
{
    final String mt = mimetype.orElseGet ( () -> evalMimeType ( artifact ) );

    response.setStatus ( HttpServletResponse.SC_OK );
    response.setContentType ( mt );
    response.setDateHeader ( "Last-Modified", artifact.getCreationInstant ().toEpochMilli () );
    response.setContentLengthLong ( artifact.getSize () );

    if ( download )
    {
        if ( nameFunc != null )
        {
            response.setHeader ( "Content-Disposition", String.format ( "attachment; filename=%s", nameFunc.apply ( artifact ) ) );
        }
        else
        {
            response.setHeader ( "Content-Disposition", String.format ( "attachment; filename=%s", artifact.getName () ) );
        }
    }

    final long size = ByteStreams.copy ( stream, response.getOutputStream () );
    logger.debug ( "Copyied {} bytes", size );
}
 
Example 9
Source Project: Tomcat8-Source-Read   File: TestSendFile.java    License: MIT License 5 votes vote down vote up
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    resp.setContentType("'application/octet-stream");
    resp.setCharacterEncoding("ISO-8859-1");
    resp.setContentLengthLong(f.length());
    if (Boolean.TRUE.equals(req.getAttribute(Globals.SENDFILE_SUPPORTED_ATTR))) {
        req.setAttribute(Globals.SENDFILE_FILENAME_ATTR, f.getAbsolutePath());
        req.setAttribute(Globals.SENDFILE_FILE_START_ATTR, Long.valueOf(0));
        req.setAttribute(Globals.SENDFILE_FILE_END_ATTR, Long.valueOf(f.length()));
    } else {
        byte[] c = new byte[8192];
        try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(f))) {
            int len = 0;
            int written = 0;
            long start = System.currentTimeMillis();
            do {
                len = in.read(c);
                if (len > 0) {
                    resp.getOutputStream().write(c, 0, len);
                    written += len;
                }
            } while (len > 0);
            System.out.println("Server Wrote " + written + " bytes in "
                    + (System.currentTimeMillis() - start) + " ms.");
        }
    }
}
 
Example 10
protected ModelAndView performExport ( final HttpServletResponse response, final String filename, final IOConsumer<OutputStream> exporter )
{
    try
    {
        final Path tmp = Files.createTempFile ( "export-", null );

        try
        {
            try ( OutputStream tmpStream = new BufferedOutputStream ( new FileOutputStream ( tmp.toFile () ) ) )
            {
                // first we spool this out to  temp file, so that we don't block the channel for too long
                exporter.accept ( tmpStream );
            }

            response.setContentLengthLong ( tmp.toFile ().length () );
            response.setContentType ( "application/zip" );
            response.setHeader ( "Content-Disposition", String.format ( "attachment; filename=%s", filename ) );

            try ( InputStream inStream = new BufferedInputStream ( new FileInputStream ( tmp.toFile () ) ) )
            {
                ByteStreams.copy ( inStream, response.getOutputStream () );
            }

            return null;
        }
        finally
        {
            Files.deleteIfExists ( tmp );
        }
    }
    catch ( final IOException e )
    {
        return CommonController.createError ( "Failed to export", null, e );
    }
}
 
Example 11
/**
 * Set headers on the given servlet response.
 * Called for GET requests as well as HEAD requests.
 * @param response current servlet response
 * @param resource the identified resource (never {@code null})
 * @param mediaType the resource's media type (never {@code null})
 * @throws IOException in case of errors while setting the headers
 */
protected void setHeaders(HttpServletResponse response, Resource resource, @Nullable MediaType mediaType)
		throws IOException {

	long length = resource.contentLength();
	if (length > Integer.MAX_VALUE) {
		response.setContentLengthLong(length);
	}
	else {
		response.setContentLength((int) length);
	}

	if (mediaType != null) {
		response.setContentType(mediaType.toString());
	}
	if (resource instanceof HttpResource) {
		HttpHeaders resourceHeaders = ((HttpResource) resource).getResponseHeaders();
		resourceHeaders.forEach((headerName, headerValues) -> {
			boolean first = true;
			for (String headerValue : headerValues) {
				if (first) {
					response.setHeader(headerName, headerValue);
				}
				else {
					response.addHeader(headerName, headerValue);
				}
				first = false;
			}
		});
	}
	response.setHeader(HttpHeaders.ACCEPT_RANGES, "bytes");
}
 
Example 12
/**
 * Set headers on the given servlet response.
 * Called for GET requests as well as HEAD requests.
 * @param response current servlet response
 * @param resource the identified resource (never {@code null})
 * @param mediaType the resource's media type (never {@code null})
 * @throws IOException in case of errors while setting the headers
 */
protected void setHeaders(HttpServletResponse response, Resource resource, @Nullable MediaType mediaType)
		throws IOException {

	long length = resource.contentLength();
	if (length > Integer.MAX_VALUE) {
		response.setContentLengthLong(length);
	}
	else {
		response.setContentLength((int) length);
	}

	if (mediaType != null) {
		response.setContentType(mediaType.toString());
	}
	if (resource instanceof HttpResource) {
		HttpHeaders resourceHeaders = ((HttpResource) resource).getResponseHeaders();
		resourceHeaders.forEach((headerName, headerValues) -> {
			boolean first = true;
			for (String headerValue : headerValues) {
				if (first) {
					response.setHeader(headerName, headerValue);
				}
				else {
					response.addHeader(headerName, headerValue);
				}
				first = false;
			}
		});
	}
	response.setHeader(HttpHeaders.ACCEPT_RANGES, "bytes");
}
 
Example 13
Source Project: mall   File: AdminProductController.java    License: Apache License 2.0 5 votes vote down vote up
@RequestMapping(method = RequestMethod.GET, value = "/img/{filename:.+}")
public void getImage(@PathVariable(name = "filename", required = true) String filename,
                     HttpServletResponse res) throws IOException {
    File file = new File("file/" + filename);
    if (file != null && file.exists()) {
        res.setHeader("content-type", "application/octet-stream");
        res.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
        res.setContentLengthLong(file.length());
        Files.copy(Paths.get(file.toURI()), res.getOutputStream());
    }
}
 
Example 14
Source Project: buck   File: ArtifactCacheHandler.java    License: Apache License 2.0 4 votes vote down vote up
private int handleGet(Request baseRequest, HttpServletResponse response) throws IOException {
  String path = baseRequest.getHttpURI().getPath();
  String[] pathElements = path.split("/");
  if (pathElements.length != 4 || !pathElements[2].equals("key")) {
    response.getWriter().write("Incorrect url format.");
    return HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
  }

  RuleKey ruleKey = new RuleKey(pathElements[3]);

  Path temp = null;
  try {
    projectFilesystem.mkdirs(projectFilesystem.getBuckPaths().getScratchDir());
    temp =
        projectFilesystem.createTempFile(
            projectFilesystem.getBuckPaths().getScratchDir(), "outgoing_rulekey", ".tmp");
    CacheResult fetchResult =
        Futures.getUnchecked(
            artifactCache.get().fetchAsync(null, ruleKey, LazyPath.ofInstance(temp)));
    if (!fetchResult.getType().isSuccess()) {
      return HttpServletResponse.SC_NOT_FOUND;
    }

    Path tempFinal = temp;
    HttpArtifactCacheBinaryProtocol.FetchResponse fetchResponse =
        new HttpArtifactCacheBinaryProtocol.FetchResponse(
            ImmutableSet.of(ruleKey),
            fetchResult.getMetadata(),
            new ByteSource() {
              @Override
              public InputStream openStream() throws IOException {
                return projectFilesystem.newFileInputStream(tempFinal);
              }
            });
    fetchResponse.write(response.getOutputStream());
    response.setContentLengthLong(fetchResponse.getContentLength());
    return HttpServletResponse.SC_OK;
  } finally {
    if (temp != null) {
      projectFilesystem.deleteFileAtPathIfExists(temp);
    }
  }
}
 
Example 15
@EntityCustomAction(action="pronunciation",viewKey=EntityView.VIEW_SHOW)
public Object getNamePronunciation(OutputStream out, EntityView view, Map<String,Object> params, EntityReference ref) {
	if (!sakaiProxy.isLoggedIn()) {
		throw new SecurityException("You must be logged in to get the name pronunciation of the student.");
	}
	String uuid = sakaiProxy.ensureUuid(ref.getId());
	if(StringUtils.isBlank(uuid)) {
		throw new EntityNotFoundException("Invalid user.", ref.getId());
	}
	
	MimeTypeByteArray mtba = profileLogic.getUserNamePronunciation(uuid);
	if(mtba != null && mtba.getBytes() != null) {
		try {
			HttpServletResponse response = requestGetter.getResponse();
			HttpServletRequest request = requestGetter.getRequest();
			response.setHeader("Expires", "0");
			response.setHeader("Pragma", "no-cache");
			response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
			response.setContentType(mtba.getMimeType());

			// Are we processing a Range request
			if (request.getHeader(HttpHeaders.RANGE) == null) {
				// Not a Range request
				byte[] bytes = mtba.getBytes();
				response.setContentLengthLong(bytes.length);
				out.write(bytes);
				return new ActionReturn(Formats.UTF_8, mtba.getMimeType() , out);
				} else {
				// A Range request - we use springs HttpRange class
				Resource resource = new ByteArrayResource(mtba.getBytes());
				response.setHeader(HttpHeaders.ACCEPT_RANGES, "bytes");
				response.setContentLengthLong(resource.contentLength());
				response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
				try {
					ServletServerHttpRequest inputMessage = new ServletServerHttpRequest(request);
					ServletServerHttpResponse outputMessage = new ServletServerHttpResponse(response);

					List<HttpRange> httpRanges = inputMessage.getHeaders().getRange();
					ResourceRegionHttpMessageConverter messageConverter = new ResourceRegionHttpMessageConverter();

					if (httpRanges.size() == 1) {
						ResourceRegion resourceRegion = httpRanges.get(0).toResourceRegion(resource);
						messageConverter.write(resourceRegion, null, outputMessage);
					} else {
						messageConverter.write(HttpRange.toResourceRegions(httpRanges, resource), null, outputMessage);
					}
				} catch (IllegalArgumentException iae) {
					response.setHeader("Content-Range", "bytes */" + resource.contentLength());
					response.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
					log.warn("Name pronunciation request failed to send the requested range for {}, {}", ref.getReference(), iae.getMessage());
				}
			}
		} catch (Exception e) {
			throw new EntityException("Name pronunciation request failed, " + e.getMessage(), ref.getReference());
		}
	}
	return null;
}
 
Example 16
Source Project: xipki   File: CtLogServlet.java    License: Apache License 2.0 4 votes vote down vote up
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
  try {
    AddPreChainRequest req0 = parse(req.getInputStream(), AddPreChainRequest.class);
    List<byte[]> chain = req0.getChain();
    if (chain == null || chain.size() < 2) {
      resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "chain has less than two certificates");
      return;
    }

    Certificate cert = Certificate.getInstance(chain.get(0));
    Certificate caCert = Certificate.getInstance(chain.get(1));
    byte[] issuerKeyHash = HashAlgo.SHA256.hash(caCert.getSubjectPublicKeyInfo().getEncoded());
    byte[] preCertTbsCert = CtLog.getPreCertTbsCert(cert.getTBSCertificate());

    byte sctVersion = 0;
    long timestamp = System.currentTimeMillis();
    byte[] sctExtensions = null;

    Signature sig = Signature.getInstance(signatureAlgo);
    sig.initSign(signingKey);
    CtLog.update(sig, sctVersion, timestamp, sctExtensions, issuerKeyHash, preCertTbsCert);
    byte[] signature = sig.sign();

    AddPreChainResponse resp0 = new AddPreChainResponse();
    resp0.setSct_version(sctVersion);
    resp0.setId(logId);
    resp0.setTimestamp(timestamp);

    DigitallySigned digitallySigned = new DigitallySigned(signatureAndHashAlgorithm, signature);
    resp0.setSignature(digitallySigned.getEncoded());

    byte[] respContent = JSON.toJSONBytes(resp0);

    resp.setContentType("application/json");
    resp.setContentLengthLong(respContent.length);
    resp.getOutputStream().write(respContent);
    resp.setStatus(HttpServletResponse.SC_OK);
  } catch (Exception ex) {
    throw new ServletException(ex.getMessage(), ex);
  }
}
 
Example 17
Source Project: spring-boot-plus   File: DownloadUtil.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * 下载文件,使用自定义下载处理器
 *
 * @param downloadDir      文件目录
 * @param downloadFileName 文件名称
 * @throws Exception
 */
public static void download(String downloadDir, String downloadFileName, List<String> allowFileExtensions, HttpServletResponse response, DownloadHandler downloadHandler) throws Exception {
    log.info("downloadDir:{}", downloadDir);
    log.info("downloadFileName:{}", downloadFileName);

    if (StringUtils.isBlank(downloadDir)) {
        throw new IOException("文件目录不能为空");
    }
    if (StringUtils.isBlank(downloadFileName)) {
        throw new IOException("文件名称不能为空");
    }
    // 安全判断,防止../情况,防止出现类似非法文件名称:../../hello/123.txt
    if (downloadFileName.contains(CommonConstant.SPOT_SPOT) || downloadFileName.contains(CommonConstant.SPOT_SPOT_BACKSLASH)) {
        throw new IOException("非法的文件名称");
    }
    // 允许下载的文件后缀判断
    if (CollectionUtils.isEmpty(allowFileExtensions)) {
        throw new IllegalArgumentException("请设置允许下载的文件后缀");
    }
    // 获取文件名称
    String fileExtension = FilenameUtils.getExtension(downloadFileName);

    // 从服务器读取文件,然后输出
    File downloadFile = new File(downloadDir, downloadFileName);
    if (!downloadFile.exists()) {
        throw new IOException("文件不存在");
    }

    // 判断文件类型,输出对应ContentType,如果没有对应的内容类型,可在config/mime-type.properties配置
    String contentType = ContentTypeUtil.getContentType(downloadFile);
    log.info("contentType:{}", contentType);
    // 文件大小
    long length = downloadFile.length();
    log.info("length:{}", length);

    // 下载回调处理
    if (downloadHandler == null) {
        // 使用默认下载处理器
        downloadHandler = new DefaultDownloadHandler();
    }
    boolean flag = downloadHandler.handle(downloadDir, downloadFileName, downloadFile, fileExtension, contentType, length);
    if (!flag) {
        log.info("下载自定义校验失败,取消下载");
        return;
    }

    // 下载文件名称编码,Firefox中文乱码处理
    String encodeDownFileName;

    HttpServletRequest request = HttpServletRequestUtil.getRequest();

    String browser = BrowserUtil.getCurrent(request);
    if (BrowserUtil.FIREFOX.equals(browser)) {
        encodeDownFileName = "=?UTF-8?B?" + (new String(Base64Utils.encodeToString(downloadFileName.getBytes("UTF-8")))) + "?=";
    } else {
        encodeDownFileName = URLEncoder.encode(downloadFileName, "utf-8").replaceAll("\\+", "%20");
    }
    log.info("encodeDownFileName:{}", encodeDownFileName);

    log.info("下载文件:" + downloadFile.getAbsolutePath());

    response.reset();
    // 设置Content-Disposition响应头
    response.setHeader("Content-Disposition", "attachment;fileName=\"" + encodeDownFileName + "\"");
    // 设置响应Content-Type
    response.setContentType(contentType);
    // 设置响应文件大小
    response.setContentLengthLong(length);

    // 文件下载
    InputStream in = new BufferedInputStream(new FileInputStream(downloadFile));
    FileCopyUtils.copy(in, response.getOutputStream());
}
 
Example 18
private void serveFileBlocking(final HttpServletRequest req, final HttpServletResponse resp, final Resource resource, HttpServerExchange exchange) throws IOException {
    final ETag etag = resource.getETag();
    final Date lastModified = resource.getLastModified();
    if(req.getDispatcherType() != DispatcherType.INCLUDE) {
        if (!ETagUtils.handleIfMatch(req.getHeader(Headers.IF_MATCH_STRING), etag, false) ||
                !DateUtils.handleIfUnmodifiedSince(req.getHeader(Headers.IF_UNMODIFIED_SINCE_STRING), lastModified)) {
            resp.setStatus(StatusCodes.PRECONDITION_FAILED);
            return;
        }
        if (!ETagUtils.handleIfNoneMatch(req.getHeader(Headers.IF_NONE_MATCH_STRING), etag, true) ||
                !DateUtils.handleIfModifiedSince(req.getHeader(Headers.IF_MODIFIED_SINCE_STRING), lastModified)) {
            if(req.getMethod().equals(Methods.GET_STRING) || req.getMethod().equals(Methods.HEAD_STRING)) {
                resp.setStatus(StatusCodes.NOT_MODIFIED);
            } else {
                resp.setStatus(StatusCodes.PRECONDITION_FAILED);
            }
            return;
        }
    }

    //we are going to proceed. Set the appropriate headers
    if(resp.getContentType() == null) {
        if(!resource.isDirectory()) {
            final String contentType = deployment.getServletContext().getMimeType(resource.getName());
            if (contentType != null) {
                resp.setContentType(contentType);
            } else {
                resp.setContentType("application/octet-stream");
            }
        }
    }
    if (lastModified != null) {
        resp.setHeader(Headers.LAST_MODIFIED_STRING, resource.getLastModifiedString());
    }
    if (etag != null) {
        resp.setHeader(Headers.ETAG_STRING, etag.toString());
    }
    ByteRange.RangeResponseResult rangeResponse = null;
    long start = -1, end = -1;
    try {
        //only set the content length if we are using a stream
        //if we are using a writer who knows what the length will end up being
        //todo: if someone installs a filter this can cause problems
        //not sure how best to deal with this
        //we also can't deal with range requests if a writer is in use
        Long contentLength = resource.getContentLength();
        if (contentLength != null) {
            resp.getOutputStream();
            if(contentLength > Integer.MAX_VALUE) {
                resp.setContentLengthLong(contentLength);
            } else {
                resp.setContentLength(contentLength.intValue());
            }
            if(resource instanceof RangeAwareResource && ((RangeAwareResource)resource).isRangeSupported() && resource.getContentLength() != null) {
                resp.setHeader(Headers.ACCEPT_RANGES_STRING, "bytes");
                //TODO: figure out what to do with the content encoded resource manager
                final ByteRange range = ByteRange.parse(req.getHeader(Headers.RANGE_STRING));
                if(range != null) {
                    rangeResponse = range.getResponseResult(resource.getContentLength(), req.getHeader(Headers.IF_RANGE_STRING), resource.getLastModified(), resource.getETag() == null ? null : resource.getETag().getTag());
                    if(rangeResponse != null){
                        start = rangeResponse.getStart();
                        end = rangeResponse.getEnd();
                        resp.setStatus(rangeResponse.getStatusCode());
                        resp.setHeader(Headers.CONTENT_RANGE_STRING, rangeResponse.getContentRange());
                        long length = rangeResponse.getContentLength();
                        if(length > Integer.MAX_VALUE) {
                            resp.setContentLengthLong(length);
                        } else {
                            resp.setContentLength((int) length);
                        }
                        if(rangeResponse.getStatusCode() == StatusCodes.REQUEST_RANGE_NOT_SATISFIABLE) {
                            return;
                        }
                    }
                }
            }
        }
    } catch (IllegalStateException e) {

    }
    final boolean include = req.getDispatcherType() == DispatcherType.INCLUDE;
    if (!req.getMethod().equals(Methods.HEAD_STRING)) {
        if(rangeResponse == null) {
            resource.serve(exchange.getResponseSender(), exchange, completionCallback(include));
        } else {
            ((RangeAwareResource)resource).serveRange(exchange.getResponseSender(), exchange, start, end, completionCallback(include));
        }
    }
}
 
Example 19
@EntityCustomAction(action="pronunciation",viewKey=EntityView.VIEW_SHOW)
public Object getNamePronunciation(OutputStream out, EntityView view, Map<String,Object> params, EntityReference ref) {
	if (!sakaiProxy.isLoggedIn()) {
		throw new SecurityException("You must be logged in to get the name pronunciation of the student.");
	}
	String uuid = sakaiProxy.ensureUuid(ref.getId());
	if(StringUtils.isBlank(uuid)) {
		throw new EntityNotFoundException("Invalid user.", ref.getId());
	}
	
	MimeTypeByteArray mtba = profileLogic.getUserNamePronunciation(uuid);
	if(mtba != null && mtba.getBytes() != null) {
		try {
			HttpServletResponse response = requestGetter.getResponse();
			HttpServletRequest request = requestGetter.getRequest();
			response.setHeader("Expires", "0");
			response.setHeader("Pragma", "no-cache");
			response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
			response.setContentType(mtba.getMimeType());

			// Are we processing a Range request
			if (request.getHeader(HttpHeaders.RANGE) == null) {
				// Not a Range request
				byte[] bytes = mtba.getBytes();
				response.setContentLengthLong(bytes.length);
				out.write(bytes);
				return new ActionReturn(Formats.UTF_8, mtba.getMimeType() , out);
				} else {
				// A Range request - we use springs HttpRange class
				Resource resource = new ByteArrayResource(mtba.getBytes());
				response.setHeader(HttpHeaders.ACCEPT_RANGES, "bytes");
				response.setContentLengthLong(resource.contentLength());
				response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
				try {
					ServletServerHttpRequest inputMessage = new ServletServerHttpRequest(request);
					ServletServerHttpResponse outputMessage = new ServletServerHttpResponse(response);

					List<HttpRange> httpRanges = inputMessage.getHeaders().getRange();
					ResourceRegionHttpMessageConverter messageConverter = new ResourceRegionHttpMessageConverter();

					if (httpRanges.size() == 1) {
						ResourceRegion resourceRegion = httpRanges.get(0).toResourceRegion(resource);
						messageConverter.write(resourceRegion, null, outputMessage);
					} else {
						messageConverter.write(HttpRange.toResourceRegions(httpRanges, resource), null, outputMessage);
					}
				} catch (IllegalArgumentException iae) {
					response.setHeader("Content-Range", "bytes */" + resource.contentLength());
					response.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
					log.warn("Name pronunciation request failed to send the requested range for {}, {}", ref.getReference(), iae.getMessage());
				}
			}
		} catch (Exception e) {
			throw new EntityException("Name pronunciation request failed, " + e.getMessage(), ref.getReference());
		}
	}
	return null;
}
 
Example 20
@Override
public void send(@Nullable final Request request, final Response response, final HttpServletResponse httpResponse)
    throws ServletException, IOException
{
  log.debug("Sending response: {}", response);

  // add response headers
  for (Map.Entry<String, String> header : response.getHeaders()) {
    httpResponse.addHeader(header.getKey(), header.getValue());
  }

  // add status followed by payload if we have one
  Status status = response.getStatus();
  String statusMessage = status.getMessage();
  try (Payload payload = response.getPayload()) {
    if (status.isSuccessful() || payload != null) {

      if (statusMessage == null) {
        httpResponse.setStatus(status.getCode());
      }
      else {
        httpResponse.setStatus(status.getCode(), statusMessage);
      }

      if (payload != null) {
        log.trace("Attaching payload: {}", payload);

        if (payload.getContentType() != null) {
          httpResponse.setContentType(payload.getContentType());
        }
        if (payload.getSize() != Payload.UNKNOWN_SIZE) {
          httpResponse.setContentLengthLong(payload.getSize());
        }

        if (request != null && !HttpMethods.HEAD.equals(request.getAction())) {
          try (InputStream input = payload.openInputStream(); OutputStream output = httpResponse.getOutputStream()) {
            payload.copy(input, output);
          }
        }
      }
    }
    else {
      httpResponse.sendError(status.getCode(), statusMessage);
    }
  }
}