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:jp.go.nict.langrid.servicesupervisor.frontend.FrontEnd.java

/**
 * //  w w w . ja  v a 2s .  c o  m
 * 
 */
public static LogInfo createLogInfo(HttpServletRequest request, InputStream responseBody, long responseMillis,
        int responseCode, int responseBytes, String protocolId) {
    String fromAddress = request.getRemoteAddr();
    String fromHost = request.getRemoteHost();
    if (fromAddress.equals("127.0.0.1")) {
        // localhost????HTTPHEADER_FROMADDRESS???
        String fromAddressHeader = request.getHeader(LangridConstants.HTTPHEADER_FROMADDRESS);
        if (fromAddressHeader != null) {
            fromAddress = fromAddressHeader;
            fromHost = fromAddressHeader;
        }
    }
    int callNest = 0;
    String callNestString = request.getHeader(LangridConstants.HTTPHEADER_CALLNEST);
    if (callNestString != null && callNestString.length() > 0) {
        try {
            callNest = Integer.parseInt(callNestString);
        } catch (NumberFormatException e) {
            logger.warning("The value of call nest header is not a number: " + callNestString);
        }
    }
    String callTree = "";
    try {
        callTree = getCallTree(protocolId, responseBody);
    } catch (IOException e) {
    }
    return new LogInfo(fromAddress, fromHost, Calendar.getInstance(), request.getRequestURI(),
            request.getContentLength(), responseMillis, responseCode, responseBytes, protocolId,
            request.getHeader("Referrer"), request.getHeader("User-Agent"), callNest, callTree);
}

From source file:org.frontcache.core.FCUtils.java

public static String getClientIP(HttpServletRequest request) {
    for (String header : CLIENT_IP_SOURCE_HEADER_LIST) {
        String ip = request.getHeader(header);
        if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
            return ip;
        }//w w w  .  j a va  2 s .co  m
    }
    return request.getRemoteAddr();
}

From source file:org.eclipse.userstorage.tests.util.USSServer.java

private static String getETag(HttpServletRequest request, String headerName) {
    String eTag = request.getHeader(headerName);
    if (eTag != null) {
        // Remove the quotes.
        eTag = eTag.substring(1, eTag.length() - 1);
    }//www  .j  av  a  2s.c o m

    return eTag;
}

From source file:com.mnt.base.web.DigestAuthenticator.java

/**
 * WWW-Authenticate: Digest realm="testrealm@host.com",
  *                   qop="auth,auth-int",
  *                   nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
  *                   opaque="5ccc069c403ebaf9f0171e9517f40e41"
 * @param req /*from   ww  w.  j a v a  2  s. c  om*/
 *
 * @param resp
 * @param authInfoMap
 */
private static void postAuthRequired(HttpServletRequest req, HttpServletResponse resp,
        Map<String, Object> authInfoMap) {

    resp.setStatus(HttpServletResponse.SC_UNAUTHORIZED);

    StringBuilder sb = new StringBuilder();
    String siteHost;
    siteHost = req.getHeader("Host");
    if (siteHost == null) {
        siteHost = "www.mntplay.com";
    }

    sb.append("Digest realm=\"" + siteHost + "\",algorithm=\"md5\",");
    sb.append("qop=\"auth,auth-int\",");

    String nonce = (String) authInfoMap.get("nonce");

    if (nonce == null) {
        nonce = UUID.randomUUID().toString();
    }

    String opaque = UUID.randomUUID().toString();

    sb.append("nonce=\"" + nonce + "\",");
    sb.append("opaque=\"" + opaque + "\"");

    authInfoMap.put("nonce", nonce);
    resp.setHeader("WWW-Authenticate", sb.toString());

    try {
        resp.flushBuffer();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:de.betterform.agent.web.WebUtil.java

/**
 * copy all http headers from client request into betterForm context map as map HTTP_HEADERS. This map
 * will be picked up in AbstractHttpConnector to configure a proper request for the internal http-client.
 *//*from   ww  w.  j av a 2s  .com*/
public static void copyHttpHeaders(HttpServletRequest request, XFormsProcessor processor) {
    RequestHeaders httpHeaders = new RequestHeaders();

    Enumeration headerNames = request.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        String headerName = (String) headerNames.nextElement();
        httpHeaders.addHeader(headerName, request.getHeader(headerName));
        if (WebUtil.LOGGER.isDebugEnabled()) {
            WebUtil.LOGGER
                    .debug("keeping httpheader: " + headerName + " value: " + request.getHeader(headerName));
        }

    }
    processor.setContextParam(AbstractHTTPConnector.HTTP_REQUEST_HEADERS, httpHeaders);
}

From source file:com.bluexml.side.Framework.alfresco.shareLanguagePicker.CustomDispatcherServlet.java

public static void setLanguageFromLayoutParam(HttpServletRequest req) {
    String urlLang = req.getParameter("shareLang");
    HttpSession currentSession = req.getSession();
    String sessionLang = (String) currentSession.getAttribute("shareLang");

    //1st option: Select the url param shareLang
    if (urlLang != null) {
        I18NUtil.setLocale(I18NUtil.parseLocale(urlLang));
        currentSession.setAttribute("shareLang", urlLang);
    } else if (sessionLang != null) {
        I18NUtil.setLocale(I18NUtil.parseLocale(sessionLang));
    } else {//from  w  w  w  .j a  v a2  s .  c om
        // set language locale from browser header
        String acceptLang = req.getHeader("Accept-Language");
        if (acceptLang != null && acceptLang.length() != 0) {
            StringTokenizer t = new StringTokenizer(acceptLang, ",; ");
            // get language and convert to java locale format
            String language = t.nextToken().replace('-', '_');
            I18NUtil.setLocale(I18NUtil.parseLocale(language));
        }
    }
}

From source file:org.gwtspringhibernate.reference.rlogman.spring.GwtServiceExporter.java

/**
 * Return true if the response object accepts Gzip encoding. This is done by
 * checking that the accept-encoding header specifies gzip as a supported
 * encoding./*from   w w  w  . j  av  a  2s .c om*/
 */
private static boolean acceptsGzipEncoding(HttpServletRequest request) {
    assert (request != null);

    String acceptEncoding = request.getHeader(ACCEPT_ENCODING);
    if (null == acceptEncoding) {
        return false;
    }

    return (acceptEncoding.indexOf(CONTENT_ENCODING_GZIP) != -1);
}

From source file:com.github.ziplet.filter.compression.CompressingStreamFactory.java

/**
 * Determines best content encoding for the response, based on the request
 * -- in particular, based on its "Accept-Encoding" header.
 *
 * @param httpRequest request/*from  www  .  j  a v a  2s.c o m*/
 * @return best content encoding
 */
static String getBestContentEncoding(HttpServletRequest httpRequest) {

    String forcedEncoding = (String) httpRequest.getAttribute(CompressingFilter.FORCE_ENCODING_KEY);
    String bestEncoding;
    if (forcedEncoding != null) {

        bestEncoding = forcedEncoding;

    } else {

        String acceptEncodingHeader = httpRequest
                .getHeader(CompressingHttpServletResponse.ACCEPT_ENCODING_HEADER);
        if (acceptEncodingHeader == null) {

            bestEncoding = NO_ENCODING;

        } else {

            bestEncoding = bestEncodingCache.get(acceptEncodingHeader);

            if (bestEncoding == null) {

                // No cached value; must parse header to determine best encoding
                // I don't synchronize on bestEncodingCache; it's not worth it to avoid the rare case where
                // two thread get in here and both parse the header. It's only a tiny bit of extra work, and
                // avoids the synchronization overhead.

                if (acceptEncodingHeader.indexOf((int) ',') >= 0) {
                    // multiple encodings are accepted
                    bestEncoding = selectBestEncoding(acceptEncodingHeader);
                } else {
                    // one encoding is accepted
                    bestEncoding = parseBestEncoding(acceptEncodingHeader);
                }

                bestEncodingCache.put(acceptEncodingHeader, bestEncoding);
            }
        }
    }

    // User-specified encoding might not be supported
    if (!isSupportedResponseContentEncoding(bestEncoding)) {
        bestEncoding = NO_ENCODING;
    }

    return bestEncoding;
}

From source file:com.glaf.core.util.ResponseUtils.java

public static void output(HttpServletRequest request, HttpServletResponse response, InputStream inputStream,
        String filename, String contentType) {
    BufferedInputStream bis = null;
    OutputStream outputStream = null;
    String contentDisposition = null;
    String fileOrgName = filename;

    filename = filename.trim();// w w  w . j a v a  2 s.c  o  m
    String userAgent = request.getHeader("User-Agent");
    logger.debug("filename:" + filename);
    logger.debug("User-Agent:" + userAgent);
    try {
        if (userAgent != null) {
            if (userAgent.indexOf("MSIE") != -1) {
                filename = new String(filename.getBytes("GBK"), "ISO8859_1");
            } else {
                filename = new String(filename.getBytes("UTF-8"), "ISO8859_1");
            }
        } else {
            filename = new String(filename.getBytes("GBK"), "ISO8859_1");
        }

        if (userAgent != null) {
            if (userAgent.indexOf("MSIE 5.5") != -1) {
                contentDisposition = "attachment;filename=\"" + filename + "\"";
            } else if (userAgent.indexOf("MSIE 6.0b") != -1) {
                filename = new String(fileOrgName.getBytes("GBK"), "ISO8859_1");
                contentDisposition = "attachment;filename=\"" + filename + "\"";
            } else if (userAgent.indexOf("Gecko") != -1) {
                filename = new String(fileOrgName.getBytes("GBK"), "ISO8859_1");
                contentDisposition = "attachment;filename=\"" + filename + "\"";
            }
        }
        if (contentDisposition == null) {
            contentDisposition = "attachment;filename=\"" + filename + "\"";
        }

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

    logger.debug("convert filename:" + filename);
    logger.debug("content disposition:" + contentDisposition);

    response.setHeader("Content-Transfer-Encoding", "base64");
    response.setHeader("Content-Disposition", contentDisposition);
    if (contentType != null) {
        response.setContentType(contentType);
    } else {
        response.setContentType("application/octet-stream");
    }

    String encoding = request.getHeader("Accept-Encoding");

    if (encoding != null) {
        encoding = encoding.toLowerCase();
    }

    try {
        if (encoding != null && encoding.indexOf("gzip") != -1) {
            response.setHeader("Content-Encoding", "gzip");
            outputStream = new java.util.zip.GZIPOutputStream(response.getOutputStream());
        } else if (encoding != null && encoding.indexOf("compress") != -1) {
            response.setHeader("Content-Encoding", "compress");
            outputStream = new java.util.zip.ZipOutputStream(response.getOutputStream());
        } else {
            outputStream = response.getOutputStream();
        }
        request.setCharacterEncoding("UTF-8");
        bis = new BufferedInputStream(inputStream);
        int bytesRead = 0;
        byte[] buffer = new byte[256];
        while ((bytesRead = bis.read(buffer, 0, 256)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }
        outputStream.flush();
        outputStream.close();
        inputStream.close();
        bis.close();
        bis = null;
        inputStream = null;
        outputStream = null;

    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        try {
            if (bis != null) {
                bis.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
        } catch (IOException ex) {
        }
    }
}

From source file:com.att.ajsc.csilogging.common.CSILoggingUtils.java

static void setRequestAttribute(HttpServletRequest request, String name, String defaultValue) {
    String value = null;/*  w  ww.j a v a2  s .co m*/
    if (name.equals(CommonNames.CSI_CONVERSATION_ID)) {
        value = (request.getHeader(CommonNames.ATT_CONVERSATION_ID) != null)
                ? request.getHeader(CommonNames.ATT_CONVERSATION_ID)
                : request.getHeader(CommonNames.CSI_CONVERSATION_ID);
    } else if (name.equals(CommonNames.CSI_MESSAGE_ID)) {
        value = (request.getHeader(CommonNames.ATT_MESSAGE_ID) != null)
                ? request.getHeader(CommonNames.ATT_MESSAGE_ID)
                : request.getHeader(CommonNames.CSI_MESSAGE_ID);
    } else if (name.equals(CommonNames.CSI_ORIGINATOR_ID)) {
        value = (request.getHeader(CommonNames.ATT_ORIGINATOR_ID) != null)
                ? request.getHeader(CommonNames.ATT_ORIGINATOR_ID)
                : request.getHeader(CommonNames.CSI_ORIGINATOR_ID);
    } else if (name.equals(CommonNames.CSI_TIME_TO_LIVE)) {
        value = (request.getHeader(CommonNames.ATT_TIME_TO_LIVE) != null)
                ? request.getHeader(CommonNames.ATT_TIME_TO_LIVE)
                : request.getHeader(CommonNames.CSI_TIME_TO_LIVE);
    } else if (name.equals(CommonNames.CSI_UNIQUE_TXN_ID)) {
        value = (request.getAttribute(CommonNames.ATT_UNIQUE_TXN_ID) != null)
                ? request.getAttribute(CommonNames.ATT_UNIQUE_TXN_ID).toString()
                : request.getHeader(CommonNames.CSI_UNIQUE_TXN_ID);
    } else {
        value = request.getHeader(name);
    }
    if (UtilLib.isNullOrEmpty(value)) {
        request.setAttribute(name, defaultValue);
    } else {
        request.setAttribute(name, value);
    }
}