Example usage for javax.servlet.http HttpServletResponse setHeader

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

Introduction

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

Prototype

public void setHeader(String name, String value);

Source Link

Document

Sets a response header with the given name and value.

Usage

From source file:cc.sion.core.web.Servlets.java

/**
 * ??Header./*from www  .  j a  v  a 2s  . c o  m*/
 *
 * @param fileName ???.
 */
public static void setFileDownloadHeader(HttpServletRequest request, HttpServletResponse response,
        String fileName) {
    // ???
    String encodedfileName = null;
    // ??firefox??,???+?
    encodedfileName = fileName.trim().replaceAll(" ", "_");
    String agent = request.getHeader("User-Agent");
    boolean isMSIE = (agent != null && agent.toUpperCase().indexOf("MSIE") != -1);
    if (isMSIE) {
        encodedfileName = Encodes.urlEncode(fileName);
    } else {
        encodedfileName = new String(fileName.getBytes(), Charsets.ISO_8859_1);
    }

    response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + encodedfileName + "\"");

}

From source file:com.xpn.xwiki.user.impl.xwiki.MyBasicAuthenticator.java

public static void showLogin(HttpServletRequest request, HttpServletResponse response, String realmName)
        throws IOException {
    // save this request
    SecurityFilter.saveRequestInformation(request);

    // determine the number of login attempts
    int loginAttempts;
    if (request.getSession().getAttribute(LOGIN_ATTEMPTS) != null) {
        loginAttempts = ((Integer) request.getSession().getAttribute(LOGIN_ATTEMPTS)).intValue();
        loginAttempts += 1;//from w  w  w  .j av a2 s  . c  om
    } else {
        loginAttempts = 1;
    }
    request.getSession().setAttribute(LOGIN_ATTEMPTS, new Integer(loginAttempts));

    if (loginAttempts <= MAX_ATTEMPTS) {
        response.setHeader("WWW-Authenticate", "BASIC realm=\"" + realmName + "\"");
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
    } else {
        request.getSession().removeAttribute(LOGIN_ATTEMPTS);
        response.sendError(HttpServletResponse.SC_UNAUTHORIZED, LOGIN_FAILED_MESSAGE);
    }
}

From source file:ispyb.client.common.util.FileUtil.java

/**
 * downloadFile// ww  w .j  a  v a 2 s . c om
 * 
 * @param fullFilePath
 * @param mimeType
 * @param response
 */
public static void DownloadFile(String fullFilePath, String mimeType, String attachmentFilename,
        HttpServletResponse response) {
    try {
        byte[] imageBytes = FileUtil.readBytes(fullFilePath);
        response.setContentLength(imageBytes.length);
        ServletOutputStream out = response.getOutputStream();
        response.setHeader("Pragma", "public");
        response.setHeader("Cache-Control", "max-age=0");
        response.setContentType(mimeType);
        response.setHeader("Content-Disposition", "attachment; filename=" + attachmentFilename);

        out.write(imageBytes);
        out.flush();
        out.close();

    } catch (FileNotFoundException fnf) {
        LOG.debug("[DownloadFile] File not found: " + fullFilePath);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.weixin.core.util.Struts2Utils.java

/**
 * ?contentTypeheaders./*from  w  w w . j  a va  2  s . com*/
 */
private static HttpServletResponse initResponseHeader(final String contentType, final String... headers) {
    // ?headers?
    String encoding = DEFAULT_ENCODING;
    boolean noCache = DEFAULT_NOCACHE;
    HttpServletResponse response = ServletActionContext.getResponse();
    for (String header : headers) {
        String headerName = StringUtils.substringBefore(header, ":");
        String headerValue = StringUtils.substringAfter(header, ":");

        if (StringUtils.equalsIgnoreCase(headerName, HEADER_ENCODING)) {
            encoding = headerValue;
        } else if (StringUtils.equalsIgnoreCase(headerName, HEADER_NOCACHE)) {
            noCache = Boolean.parseBoolean(headerValue);
        } else if (StringUtils.equalsIgnoreCase(headerName, "Cache-Control")
                || StringUtils.equalsIgnoreCase(headerName, "Pragma")) {
            response.setHeader(headerName, headerValue);
            noCache = false;
        } else {
            throw new IllegalArgumentException(headerName + "??header");
        }
    }

    // headers?
    String fullContentType = contentType + ";charset=" + encoding;
    response.setContentType(fullContentType);

    response.setHeader("Pragma:", "public");
    if (noCache) {
        ServletUtils.setDisableCacheHeader(response);
    }

    return response;
}

From source file:com.mxep.web.web.Servlets.java

/**
 * ??Header./*from   w w  w  .ja v  a 2  s .  co m*/
 *
 * @param fileName ???.
 */
public static void setFileDownloadHeader(HttpServletRequest request, HttpServletResponse response,
        String fileName) {
    // ???
    String encodedfileName = null;
    // ??firefox??,???+?
    encodedfileName = fileName.trim().replaceAll(" ", "_");
    String agent = request.getHeader("User-Agent");
    boolean isMSIE = (agent != null && agent.toUpperCase().indexOf("MSIE") != -1);
    if (isMSIE) {
        encodedfileName = EncodeUtils.urlEncode(fileName);
    } else {
        encodedfileName = new String(fileName.getBytes(), Charsets.ISO_8859_1);
    }

    response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + encodedfileName + "\"");

}

From source file:com.adito.vfs.webdav.DAVServlet.java

/**
 * Add the headers required for the browser to popup an authentication
 * dialog for a specified realm, and send the
 * {@link HttpServletResponse.SC_UNAUTHORIZED} HTTP response code.
 * // w w w . ja  v  a 2  s.c  o m
 * @param request request
 * @param response response
 * @param realm realm.
 * @throws IOException
 */
public static void sendAuthorizationError(HttpServletRequest request, HttpServletResponse response,
        String realm) throws IOException {
    /*
     * If this is for the default realm (i.e Adito Authentication, we
     * need to set up an authentication scheme
     */
    if (realm.equals(WebDAVAuthenticationModule.DEFAULT_REALM)) {
        configureAuthenticationScheme(request, response);
    }

    // Configure the response

    if (log.isDebugEnabled())
        log.debug("Sending auth request for realm " + realm);
    response.setHeader("WWW-Authenticate", "Basic realm=\"" + realm + "\"");
    response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
    request.getSession().setAttribute(DAVTransaction.ATTR_EXPECTING_REALM_AUTHENTICATION, realm);

}

From source file:com.aurel.track.exchange.latex.exporter.LaTeXExportBL.java

/**
 * Serializes the docx content into the response's output stream
 * @param response//from   w ww .j  ava 2 s.c o  m
 * @param wordMLPackage
 * @return
 */
public static String prepareReportResponse(HttpServletResponse response, TWorkItemBean workItem,
        ReportBeans reportBeans, TPersonBean user, Locale locale, String templateDir, String templateFile) {
    ReportBeansToLaTeXConverter rl = new ReportBeansToLaTeXConverter();
    File pdf = rl.generatePdf(workItem, reportBeans.getItems(), true, locale, user, "", "", false,
            new File(templateFile), new File(templateDir));
    String fileName = workItem.getSynopsis() + ".pdf";
    String contentType = "application/pdf";
    if (pdf.length() < 10) {
        pdf = new File(pdf.getParent() + "/errors.txt");
        fileName = workItem.getSynopsis() + ".txt";
        contentType = "text";
    }
    OutputStream outputStream = null;
    try {
        response.reset();
        response.setHeader("Content-Type", contentType);
        response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
        DownloadUtil.prepareCacheControlHeader(ServletActionContext.getRequest(), response);
        outputStream = response.getOutputStream();
        InputStream is = new FileInputStream(pdf);
        IOUtils.copy(is, outputStream);
        is.close();
    } catch (FileNotFoundException e) {
        LOGGER.error(ExceptionUtils.getStackTrace(e));
    } catch (IOException e) {
        LOGGER.error("Getting the output stream failed with " + e.getMessage());
        LOGGER.error(ExceptionUtils.getStackTrace(e));
    } catch (Exception e) {
        LOGGER.error(ExceptionUtils.getStackTrace(e));
    }

    //            Docx4J.save(wordMLPackage, outputStream, Docx4J.FLAG_NONE);
    //            //wordMLPackage.save(outputStream);
    //            /*SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
    //         saver.save(outputStream);*/
    //         } catch (Exception e) {
    //            LOGGER.error("Exporting the docx failed with throwable " + e.getMessage());
    //            LOGGER.debug(ExceptionUtils.getStackTrace(e));
    //         }
    return null;
}

From source file:com.sslexplorer.vfs.webdav.DAVServlet.java

/**
 * Add the headers required for the browser to popup an authentication
 * dialog for a specified realm, and send the
 * {@link HttpServletResponse.SC_UNAUTHORIZED} HTTP response code.
 * /*from   ww w . j  a va2  s  .  c  om*/
 * @param request request
 * @param response response
 * @param realm realm.
 * @throws IOException
 */
public static void sendAuthorizationError(HttpServletRequest request, HttpServletResponse response,
        String realm) throws IOException {
    /*
     * If this is for the default realm (i.e SSL-Explorer Authentication, we
     * need to set up an authentication scheme
     */
    if (realm.equals(WebDAVAuthenticationModule.DEFAULT_REALM)) {
        configureAuthenticationScheme(request, response);
    }

    // Configure the response

    if (log.isDebugEnabled())
        log.debug("Sending auth request for realm " + realm);
    response.setHeader("WWW-Authenticate", "Basic realm=\"" + realm + "\"");
    response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
    request.getSession().setAttribute(DAVTransaction.ATTR_EXPECTING_REALM_AUTHENTICATION, realm);

}

From source file:com.ziduye.base.web.Servlets.java

/**
 * ??Header.//from www  .j  a  v a 2s .c om
 * 
 * @param fileName ???.
 */
public static void setFileDownloadHeader(HttpServletRequest request, HttpServletResponse response,
        String fileName) {
    // ???
    String encodedfileName = null;
    // ??firefox??,???+?
    encodedfileName = fileName.trim().replaceAll(" ", "_");
    String agent = request.getHeader("User-Agent");
    boolean isMSIE = (agent != null && agent.toUpperCase().indexOf("MSIE") != -1);
    if (isMSIE) {
        encodedfileName = Encodes.urlEncode(fileName);
    } else {
        encodedfileName = new String(fileName.getBytes(), Charsets.ISO_8859_1);
    }
    response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + encodedfileName + "\"");
}

From source file:com.jsmartframework.web.manager.WebContext.java

private static void setResponseAsError(String errorCode) {
    // Case response error is called we need to call onError on client side
    HttpServletResponse response = getResponse();
    if (response != null) {
        response.setHeader("Error-Ajax", StringUtils.isNotBlank(errorCode) ? errorCode : ERROR_CODE);
    }/*from  w  ww.jav a 2s .c o  m*/
}