Example usage for javax.servlet.http HttpServletRequest getHeader

List of usage examples for javax.servlet.http HttpServletRequest getHeader

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest getHeader.

Prototype

public String getHeader(String name);

Source Link

Document

Returns the value of the specified request header as a String.

Usage

From source file:org.apache.atlas.web.util.Servlets.java

/**
 * Returns the user of the given request.
 *
 * @param httpRequest    an HTTP servlet request
 * @return the user// w  ww  .  j  ava 2s.co m
 */
public static String getUserFromRequest(HttpServletRequest httpRequest) {
    String user = httpRequest.getRemoteUser();
    if (!StringUtils.isEmpty(user)) {
        return user;
    }

    user = httpRequest.getParameter("user.name"); // available in query-param
    if (!StringUtils.isEmpty(user)) {
        return user;
    }

    user = httpRequest.getHeader("Remote-User"); // backwards-compatibility
    if (!StringUtils.isEmpty(user)) {
        return user;
    }

    user = getDoAsUser(httpRequest);
    if (!StringUtils.isEmpty(user)) {
        return user;
    }

    return null;
}

From source file:com.liusoft.dlog4j.util.RequestUtils.java

/**
 * ??/*from   w  w  w .  jav  a  2s . c  o  m*/
 * @param req
 * @param out
 */
public static void dumpHeaders(HttpServletRequest req, PrintStream out) {
    Enumeration hds = req.getHeaderNames();
    out.println("=============== HEADERS ===============");
    while (hds.hasMoreElements()) {
        String name = (String) hds.nextElement();
        out.println(name + "=" + req.getHeader(name));
    }
}

From source file:com.zimbra.cs.servlet.ZimbraServlet.java

public static void addUAToLoggingContext(HttpServletRequest req) {
    ZimbraLog.addUserAgentToContext(req.getHeader("User-Agent"));
}

From source file:cn.guoyukun.spring.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  ww .j  ava2s .  c o  m*/
    }

    String userAgent = request.getHeader("User-Agent");
    boolean isIE = (userAgent != null) && (userAgent.toLowerCase().indexOf("msie") != -1);

    response.reset();
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "must-revalidate, no-transform");
    response.setDateHeader("Expires", 0L);

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

    String displayFilename = displayName.substring(displayName.lastIndexOf("_") + 1);
    displayFilename = displayFilename.replace(" ", "_");
    if (isIE) {
        displayFilename = URLEncoder.encode(displayFilename, "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=\"" + displayFilename + "\"");
    } else {
        displayFilename = new String(displayFilename.getBytes("UTF-8"), "ISO8859-1");
        response.setHeader("Content-Disposition", "attachment;filename=" + 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.meltmedia.cadmium.servlets.FileServletTest.java

public static HttpServletRequest mockGetWithIfModifiedSince(String pathInfo, Long lastModified) {
    HttpServletRequest request = mockGet(pathInfo);
    when(request.getDateHeader(IF_MODIFIED_SINCE_HEADER)).thenReturn(lastModified);
    when(request.getHeader(anyString())).thenReturn(null);
    return request;
}

From source file:com.charity.core.controller.Servlets.java

/**
 * ??Header./*from   w w  w. java2s . c  om*/
 * 
 * @param fileName ???.
 */
public static void setFileDownloadHeader(HttpServletRequest request, HttpServletResponse response,
        String fileName) {
    // ???
    String encodedfileName;
    // ??firefox??,???+?
    fileName = fileName.trim().replaceAll(" ", "_");

    String agent = request.getHeader("User-Agent");
    boolean isMSIE = (agent != null && agent.toUpperCase().contains("MSIE"));
    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.ikon.util.WebUtils.java

/**
 * Prepare to send the file./* w  w w.  ja va  2  s .co  m*/
 */
public static void prepareSendFile(HttpServletRequest request, HttpServletResponse response, String fileName,
        String mimeType, boolean inline) throws UnsupportedEncodingException {
    String agent = request.getHeader("USER-AGENT");

    // Disable browser cache
    response.setHeader("Expires", "Sat, 6 May 1971 12:00:00 GMT");
    response.setHeader("Cache-Control", "must-revalidate");
    response.addHeader("Cache-Control", "no-store, no-cache, must-revalidate");
    response.addHeader("Cache-Control", "post-check=0, pre-check=0");
    response.setHeader("Pragma", "no-cache");

    // Set MIME type
    response.setContentType(mimeType);

    if (null != agent && -1 != agent.indexOf("MSIE")) {
        log.debug("Agent: Explorer ({})", request.getServerPort());
        fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", " ");

        if (request.getServerPort() == 443) {
            log.debug("HTTPS detected! Apply IE workaround...");
            response.setHeader("Cache-Control", "max-age=1");
            response.setHeader("Pragma", "public");
        }
    } else if (null != agent && -1 != agent.indexOf("Mozilla")) {
        log.debug("Agent: Mozilla");
        fileName = MimeUtility.encodeText(fileName, "UTF-8", "B");
    } else {
        log.debug("Agent: Unknown");
    }

    if (inline) {
        response.setHeader("Content-disposition", "inline; filename=\"" + fileName + "\"");
    } else {
        response.setHeader("Content-disposition", "attachment; filename=\"" + fileName + "\"");
    }
}

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

/**
 * ??Header./*from   w w w.ja v a2  s .  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.berrysys.ussdgw.HttpUtils.java

/**
 * Do post.//from w  w w  . j  a v  a 2  s .co m
 *
 * @param req the req
 * @param resp the resp
 * @param dialogListener the dialog listener
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static void doPost(HttpServletRequest req, HttpServletResponse resp, final DialogListener dialogListener)
        throws IOException {

    boolean available = GlobalNetworkInitiatedSemaphore.getInstance().getSemaphore().tryAcquire();

    if (!available) {
        queueFullLogic(resp);
        return;
    }

    final String ussdAppUrl = req.getHeader("ussd-app-url");

    log.trace(String.format("ussd-app-url: %s", ussdAppUrl));

    final String payload = getPayload(req);

    log.trace(String.format("payload: %s", payload));

    HawtDispatchUtil.getInstance().queueExecute(new Runnable() {

        @Override
        public void run() {
            processHttpRequest(payload, ussdAppUrl, dialogListener);
        }

    });

    String response = "Request received successfully.";

    resp.setContentType("text/html");
    resp.setStatus(HttpServletResponse.SC_OK);
    resp.getWriter().println(response);

}

From source file:org.codehaus.groovy.grails.plugins.springsecurity.AuthorizeTools.java

/**
 * Check if the request was triggered by an Ajax call.
 * @param request the request/*from  ww w.  j a v  a 2 s .co m*/
 * @return <code>true</code> if Ajax
 */
public static boolean isAjax(final HttpServletRequest request) {

    // look for an ajax=true parameter
    if ("true".equals(request.getParameter("ajax"))) {
        return true;
    }

    // check the current request's headers
    if (request.getHeader(_ajaxHeaderName) != null) {
        return true;
    }

    // check the SavedRequest's headers
    SavedRequest savedRequest = (SavedRequest) request.getSession()
            .getAttribute(AbstractProcessingFilter.SPRING_SECURITY_SAVED_REQUEST_KEY);
    if (savedRequest != null) {
        return savedRequest.getHeaderValues(_ajaxHeaderName).hasNext();
    }

    return false;
}