Example usage for javax.servlet.http HttpServletResponse setContentType

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

Introduction

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

Prototype


public void setContentType(String type);

Source Link

Document

Sets the content type of the response being sent to the client, if the response has not been committed yet.

Usage

From source file:com.github.buildnum.servlet.VersionServlet.java

protected static void writeOutput(String s, HttpServletResponse resp)
        throws UnsupportedEncodingException, IOException {
    resp.setContentType("text/plain; encoding=ISO-8859-1");
    IOUtils.write(s.getBytes("8859_1"), resp.getOutputStream());
}

From source file:com.liferay.util.servlet.ServletResponseUtil.java

public static void sendFile(HttpServletResponse res, String fileName, byte[] byteArray, String contentType)
        throws IOException {

    _log.debug("Sending file of type " + contentType);

    res.setContentType(contentType);

    res.setHeader(HttpHeaders.CACHE_CONTROL, HttpHeaders.PUBLIC);
    res.setHeader(HttpHeaders.PRAGMA, HttpHeaders.PUBLIC);

    if (Validator.isNotNull(fileName)) {
        res.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"");
    }/*w ww  . j  a  v a2s.  c o m*/

    res.getOutputStream().write(byteArray);
    res.getOutputStream().flush();
}

From source file:com.eryansky.common.web.utils.DownloadUtils.java

/**
 * //ww  w  .  j  a  v  a  2  s. c  om
 * @param request
 * @param response
 * @param displayName ??
 * @param bytes 
 * @throws IOException
 */
public static void download(HttpServletRequest request, HttpServletResponse response, byte[] bytes,
        String displayName) throws IOException {
    if (ArrayUtils.isEmpty(bytes)) {
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        response.getWriter().write("??");
        return;
    }
    download(request, response, new ByteArrayInputStream(bytes), displayName);
}

From source file:com.google.appinventor.server.GalleryServlet.java

/**
 * Set a default http header to avoid security vulnerabilities.
 *//*  w  w  w .j  a v  a2s  .  c om*/
private static void setDefaultHeader(HttpServletResponse resp) {
    CACHE_HEADERS.setNotCacheable(resp);
    resp.setContentType(CONTENT_TYPE);
}

From source file:com.lushapp.common.web.utils.DownloadUtils.java

public static void download(HttpServletRequest request, HttpServletResponse response, String displayName,
        byte[] bytes) throws IOException {

    if (ArrayUtils.isEmpty(bytes)) {
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        response.getWriter().write("??");
        return;//from   w w w .  j  a v a 2 s .c o  m
    }

    response.reset();
    WebUtils.setNoCacheHeader(response);

    response.setContentType("application/x-download");
    response.setContentLength((int) bytes.length);

    String displayFilename = displayName.substring(displayName.lastIndexOf("_") + 1);
    displayFilename = displayFilename.replace(" ", "_");
    WebUtils.setDownloadableHeader(request, response, displayFilename);
    BufferedInputStream is = null;
    OutputStream os = null;
    try {

        os = response.getOutputStream();
        is = new BufferedInputStream(new ByteArrayInputStream(bytes));
        IOUtils.copy(is, os);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:com.platform.BRHTTPHelper.java

public static boolean handleSuccess(int code, byte[] body, Request baseRequest, HttpServletResponse resp,
        String contentType) {//from ww  w .ja  v  a2 s.c om
    try {
        resp.setStatus(code);
        if (contentType != null && !contentType.isEmpty())
            resp.setContentType(contentType);
        if (body != null)
            resp.getOutputStream().write(body);
        baseRequest.setHandled(true);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return true;
}

From source file:nl.dtls.fairdatapoint.api.controller.utils.HttpHeadersUtils.java

/**
 * Set response header for the successful call
 * //  w  w  w. ja v a2  s .c  o  m
 * @param responseBody ResponseBody
 * @param response  Http response
 * @param requesetedContentType Requeseted ContentType(i.e Accept header)
 */
public static void set200ResponseHeaders(String responseBody, HttpServletResponse response,
        RDFFormat requesetedContentType) {
    if (responseBody == null) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        response.setContentType(MediaType.TEXT_PLAIN_VALUE);
    } else {
        response.setStatus(HttpServletResponse.SC_OK);
        response.setContentType(requesetedContentType.getDefaultMIMEType());
    }
}

From source file:de.thischwa.pmcms.server.ServletUtils.java

public static void establishContentType(String fileName, HttpServletResponse resp) {
    if (StringUtils.isBlank(fileName))
        return;//from   www  . j  a va2 s .c  o  m
    String contentType = contentTypes.getContentTypeFor(fileName);
    if (StringUtils.isNotBlank(contentType))
        resp.setContentType(contentType);
}

From source file:ee.ria.xroad.common.request.DummyCentralServiceHandler.java

private static void sendResponse(SoapMessageImpl responseSoap, HttpServletResponse response) throws Exception {
    log.info("Sending response: {}", responseSoap.getXml());
    try {/*from  w ww .j  av a 2s.com*/
        response.setContentType(MimeTypes.TEXT_XML);
        response.getWriter().write(responseSoap.getXml());
    } finally {
        response.getWriter().close();
    }
}

From source file:cz.cas.lib.proarc.authentication.utils.AuthUtils.java

/**
 * Writes the authentication required status to the HTTP response.
 * @param response response// w  w w . j  av  a 2  s.  com
 * @throws IOException failure
 * @see #HEADER_AUTHENTICATE_TYPE
 * @see <a href='http://www.smartclient.com/smartgwt/javadoc/com/smartgwt/client/docs/Relogin.html'>SmartGWT Relogin</a>
 */
public static void setLoginRequiredResponse(HttpServletResponse response) throws IOException {
    response.setHeader(HEADER_AUTHENTICATE_TYPE, Authenticators.getInstance().getLoginType());
    response.setStatus(HttpServletResponse.SC_FORBIDDEN);
    response.setContentType(MediaType.TEXT_HTML);
    InputStream res = ProarcAuthFilter.class.getResourceAsStream("loginRequiredMarker.html");
    try {
        IOUtils.copy(res, response.getOutputStream());
        res.close();
    } finally {
        IOUtils.closeQuietly(res);
    }
}