Example usage for javax.servlet.http HttpServletResponse setIntHeader

List of usage examples for javax.servlet.http HttpServletResponse setIntHeader

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse setIntHeader.

Prototype

public void setIntHeader(String name, int value);

Source Link

Document

Sets a response header with the given name and integer value.

Usage

From source file:com.novartis.pcs.ontology.rest.servlet.OntologiesServlet.java

@Override
protected void doOptions(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String mediaType = getExpectedMediaType(request);

    OntologyFormat format = mediaTypes.get(mediaType);
    if (format != null) {
        // Preflight CORS support
        response.setStatus(HttpServletResponse.SC_OK);
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET,PUT");
        response.setIntHeader("Access-Control-Max-Age", 60 * 60 * 24);
        response.setContentType(mediaType + ";charset=utf-8");
        response.setContentLength(0);/*from w w  w.  ja  va2  s. c om*/
    } else {
        response.setStatus(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
        response.setContentLength(0);
    }
}

From source file:com.haulmont.cuba.core.controllers.FileDownloadController.java

@RequestMapping(value = "/download", method = RequestMethod.GET)
public void download(HttpServletRequest request, HttpServletResponse response) throws IOException {
    UserSession userSession = getSession(request, response);
    if (userSession == null)
        return;/*w w w  . j  a  va  2  s. com*/

    AppContext.setSecurityContext(new SecurityContext(userSession));
    try {
        File file = null;
        FileDescriptor fd = null;
        if (request.getParameter("p") != null)
            file = getFile(request, response);
        else
            fd = getFileDescriptor(request, response);
        if (fd == null && file == null)
            return;

        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        response.setIntHeader("Expires", -1);
        response.setHeader("Content-Type", FileTypesHelper.DEFAULT_MIME_TYPE);

        InputStream is = null;
        ServletOutputStream os = null;
        try {
            is = fd != null ? fileStorage.openStream(fd) : FileUtils.openInputStream(file);
            os = response.getOutputStream();
            IOUtils.copy(is, os);
            os.flush();
        } catch (FileStorageException e) {
            log.error("Unable to download file", e);
            response.sendError(e.getType().getHttpStatus());
        } catch (Exception ex) {
            log.error("Unable to download file", ex);
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        } finally {
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(os);
        }
    } finally {
        AppContext.setSecurityContext(null);
    }
}

From source file:password.pwm.http.servlet.resource.ResourceFileServlet.java

private void writeConfigSettingToBody(final PwmApplication pwmApplication, final PwmSetting pwmSetting,
        final HttpServletResponse response, final ResourceServletConfiguration resourceServletConfiguration)
        throws PwmUnrecoverableException, IOException {
    final String bodyText = pwmApplication.getConfig().readSettingAsString(pwmSetting);
    try {/*from w  ww  . j  a  v a2s .  c o m*/
        response.setContentType("text/css");
        addExpirationHeaders(resourceServletConfiguration, response);
        if (bodyText != null && bodyText.length() > 0) {
            response.setIntHeader("Content-Length", bodyText.length());
            copy(new ByteArrayInputStream(bodyText.getBytes()), response.getOutputStream());
        } else {
            response.setIntHeader("Content-Length", 0);
        }
    } finally {
        close(response.getOutputStream());
    }
}

From source file:net.sourceforge.subsonic.controller.DownloadController.java

public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {

    TransferStatus status = null;//from  ww w . j  a va2 s.  c o  m
    try {

        status = statusService.createDownloadStatus(playerService.getPlayer(request, response, false, false));

        MediaFile mediaFile = getSingleFile(request);
        String dir = request.getParameter("dir");
        String playlistName = request.getParameter("playlist");
        String playerId = request.getParameter("player");
        int[] indexes = ServletRequestUtils.getIntParameters(request, "i");

        if (mediaFile != null) {
            response.setIntHeader("ETag", mediaFile.getId());
            response.setHeader("Accept-Ranges", "bytes");
        }

        LongRange range = StringUtil.parseRange(request.getHeader("Range"));
        if (range != null) {
            response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
            LOG.info("Got range: " + range);
        }

        if (mediaFile != null) {
            File file = mediaFile.getFile();
            if (!securityService.isReadAllowed(file)) {
                response.sendError(HttpServletResponse.SC_FORBIDDEN);
                return null;
            }

            if (file.isFile()) {
                downloadFile(response, status, file, range);
            } else {
                downloadDirectory(response, status, file, range);
            }
        } else if (dir != null) {
            File file = new File(dir);
            if (!securityService.isReadAllowed(file)) {
                response.sendError(HttpServletResponse.SC_FORBIDDEN);
                return null;
            }
            downloadFiles(response, status, file, indexes);

        } else if (playlistName != null) {
            Playlist playlist = new Playlist();
            playlistService.loadPlaylist(playlist, playlistName);
            downloadPlaylist(response, status, playlist, null, range);

        } else if (playerId != null) {
            Player player = playerService.getPlayerById(playerId);
            Playlist playlist = player.getPlaylist();
            playlist.setName("Playlist");
            downloadPlaylist(response, status, playlist, indexes.length == 0 ? null : indexes, range);
        }

    } finally {
        if (status != null) {
            statusService.removeDownloadStatus(status);
            User user = securityService.getCurrentUser(request);
            securityService.updateUserByteCounts(user, 0L, status.getBytesTransfered(), 0L);
        }
    }

    return null;
}

From source file:org.madsonic.controller.DownloadController.java

public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {

    TransferStatus status = null;//  ww  w.  j av  a2  s  . com
    try {

        status = statusService.createDownloadStatus(playerService.getPlayer(request, response, false, false));

        MediaFile mediaFile = getMediaFile(request);
        Integer playlistId = ServletRequestUtils.getIntParameter(request, "playlist");
        String playerId = request.getParameter("player");
        int[] indexes = request.getParameter("i") == null ? null
                : ServletRequestUtils.getIntParameters(request, "i");

        if (mediaFile != null) {
            response.setIntHeader("ETag", mediaFile.getId());
            response.setHeader("Accept-Ranges", "bytes");
        }

        HttpRange range = HttpRange.valueOf(request.getHeader("Range"));
        if (range != null) {
            response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
            LOG.info("Got HTTP range: " + range);
        }

        if (mediaFile != null) {
            if (mediaFile.isFile()) {
                downloadFile(response, status, mediaFile.getFile(), range);
            } else {
                List<MediaFile> children = mediaFileService.getChildrenOf(mediaFile, true, true, true);
                String zipFileName = FilenameUtils.getBaseName(mediaFile.getPath()) + ".zip";
                downloadFiles(response, status, children, indexes, range, zipFileName);
            }

        } else if (playlistId != null) {
            List<MediaFile> songs = playlistService.getFilesInPlaylist(playlistId);
            Playlist playlist = playlistService.getPlaylist(playlistId);
            downloadFiles(response, status, songs, null, range, playlist.getName() + ".zip");

        } else if (playerId != null) {
            Player player = playerService.getPlayerById(playerId);
            PlayQueue playQueue = player.getPlayQueue();
            playQueue.setName("Playlist");
            downloadFiles(response, status, playQueue.getFiles(), indexes, range, "download.zip");
        }

    } finally {
        if (status != null) {
            statusService.removeDownloadStatus(status);
            User user = securityService.getCurrentUser(request);
            securityService.updateUserByteCounts(user, 0L, status.getBytesTransfered(), 0L);
        }
    }

    return null;
}

From source file:net.lightbody.bmp.proxy.jetty.jetty.servlet.ServletHandler.java

protected void handleOptions(HttpServletRequest request, HttpServletResponse response) throws IOException {
    // Handle OPTIONS request for entire server
    if ("*".equals(request.getRequestURI())) {
        // 9.2/*from w ww.  j  a va2 s.  c om*/
        response.setIntHeader(HttpFields.__ContentLength, 0);
        response.setHeader(HttpFields.__Allow, __AllowString);
        response.flushBuffer();
    } else
        response.sendError(HttpResponse.__404_Not_Found);
}

From source file:org.seasar.s2click.S2ClickPage.java

/**
 * ????/*w ww.ja va2s.  co  m*/
 *
 * @param contentType 
 * @param contents ??
 */
protected void renderResponse(String contentType, InputStream contents) {
    HttpServletResponse res = getContext().getResponse();

    // ????????
    Map<String, Object> headers = getHeaders();

    for (Iterator<Map.Entry<String, Object>> i = headers.entrySet().iterator(); i.hasNext();) {
        Map.Entry<String, Object> entry = i.next();
        String name = entry.getKey().toString();
        Object value = entry.getValue();

        if (value instanceof String) {
            String strValue = (String) value;
            if (!strValue.equalsIgnoreCase("Content-Encoding")) {
                res.setHeader(name, strValue);
            }

        } else if (value instanceof Date) {
            long time = ((Date) value).getTime();
            res.setDateHeader(name, time);

        } else {
            int intValue = ((Integer) value).intValue();
            res.setIntHeader(name, intValue);
        }
    }

    OutputStream out = null;

    try {
        res.setContentLength(contents.available());
        res.setContentType(contentType);
        out = res.getOutputStream();
        IOUtils.copy(contents, out);
        res.flushBuffer();

    } catch (Exception ex) {
        throw new RuntimeException(ex);

    } finally {
        IOUtils.closeQuietly(contents);
        IOUtils.closeQuietly(out);
        getContext().setRequestAttribute(SKIP_RENDERING, "true");
    }
}

From source file:com.scooter1556.sms.server.service.AdaptiveStreamingService.java

public void sendSubtitleSegment(HttpServletResponse response) throws IOException {
    List<String> segment = new ArrayList<>();
    segment.add("WEBVTT");

    // Write segment to buffer so we can get the content length
    StringWriter segmentWriter = new StringWriter();
    for (String line : segment) {
        segmentWriter.write(line + "\n");
    }/*from  w  ww .j  a  v a  2 s.c  o m*/

    // Set Header Parameters
    response.reset();
    response.setContentType("text/vtt");
    response.setContentLength(segmentWriter.toString().length());

    // Enable CORS
    response.setHeader(("Access-Control-Allow-Origin"), "*");
    response.setHeader("Access-Control-Allow-Methods", "GET");
    response.setIntHeader("Access-Control-Max-Age", 3600);

    // Write segment out to the client
    response.getWriter().write(segmentWriter.toString());
}

From source file:org.openqa.grid.internal.TestSession.java

private void writeRawBody(HttpServletResponse response, byte[] rawBody) throws IOException {
    OutputStream out = response.getOutputStream();
    try {//from   w  w  w  .  j  ava  2 s.  c  o  m
        // We need to set the Content-Length header before we write to the output stream. Usually
        // the
        // Content-Length header is already set because we take it from the proxied request. But, it
        // won't
        // be set when we consume chunked content, since that doesn't use Content-Length. As we're
        // not
        // going to send a chunked response, we need to set the Content-Length in order for the
        // response
        // to be valid.
        if (!response.containsHeader("Content-Length")) {
            response.setIntHeader("Content-Length", rawBody.length);
        }

        out.write(rawBody);
    } catch (IOException e) {
        throw new ClientGoneException(e);
    } finally {
        IOUtils.closeQuietly(out);
    }
}

From source file:net.lightbody.bmp.proxy.jetty.jetty.servlet.ServletHandler.java

protected void handleTrace(HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.setHeader(HttpFields.__ContentType, HttpFields.__MessageHttp);
    OutputStream out = response.getOutputStream();
    ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer();
    writer.write(request.toString());//from  w  w w .  j a v a2  s .c o m
    writer.flush();
    response.setIntHeader(HttpFields.__ContentLength, writer.size());
    writer.writeTo(out);
    out.flush();
}