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:ee.ria.xroad.proxy.serverproxy.ServerMessageProcessor.java

private static String getHashAlgoId(HttpServletRequest servletRequest) {
    String hashAlgoId = servletRequest.getHeader(HEADER_HASH_ALGO_ID);

    if (hashAlgoId == null) {
        throw new CodedException(X_INTERNAL_ERROR, "Could not get hash algorithm identifier from message");
    }/*from   w w w.  j a  va  2 s.  c  om*/

    return hashAlgoId;
}

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

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

    String ajaxHeaderName = (String) ReflectionUtils.getConfigProperty("ajaxHeader");

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

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

    // check the SavedRequest's headers
    SavedRequest savedRequest = (SavedRequest) request.getSession().getAttribute(WebAttributes.SAVED_REQUEST);
    if (savedRequest != null) {
        return !savedRequest.getHeaderValues(ajaxHeaderName).isEmpty();
    }

    return false;
}

From source file:com.exilant.exility.core.HttpRequestHandler.java

/**
 * populate data with global/session variables, if this is an authenticated
 * session. Caller can have the CommonFieldNames.CSRF_HEADER token either in
 * the header, or a field already extracted into inData
 * //from   ww  w  .  ja  va 2  s  . c  om
 * @param req
 * @param inData
 *            into which session fields need to be extracted
 * @return true if this is an authenticated session, false otherwise
 */
public static boolean extractSessionFields(HttpServletRequest req, ServiceData inData) {
    String token = req.getHeader(CommonFieldNames.CSRF_HEADER);
    if (token == null) {
        token = inData.getValue(CommonFieldNames.CSRF_HEADER);
    }

    if (token == null || token.length() == 0) {
        return false;
    }

    HttpSession session = req.getSession(false);
    if (session == null) {
        return false;
    }

    Object obj = session.getAttribute(token);
    if (obj == null) {
        return false;
    }

    if (obj instanceof SessionData == false) {
        return false;
    }
    ((SessionData) obj).extractAll(inData);
    return true;
}

From source file:com.feilong.servlet.http.RequestUtil.java

/**
 * User Agent???, UA./*w w w  . j av a  2  s . com*/
 * 
 * <p>
 * ,????CPU ??????????.
 * </p>
 * 
 * @param request
 *            the request
 * @return request?? {@link HttpHeaders#USER_AGENT} header,null
 * @see HttpHeaders#USER_AGENT
 */
public static String getHeaderUserAgent(HttpServletRequest request) {
    return request.getHeader(USER_AGENT);
}

From source file:com.feilong.servlet.http.RequestUtil.java

/**
 * URL.//from   www . ja  v  a2 s. com
 * 
 * <pre class="code">
 * ,http???,javascript
 * 
 * :
 * ?&lt;a href=&quot;url&quot;&gt;sss&lt;/a&gt;?
 * ?location&lt;a href=&quot;javascript:location='url'&quot;&gt;sss&lt;/a&gt;?
 * 
 * referer?????,??URL???,??a.html,
 * ?b.html,?b.html?a.htmlreferer??.
 * 
 * </pre>
 * 
 * @param request
 *            the request
 * @return request?? {@link HttpHeaders#REFERER} header,null
 * @see HttpHeaders#REFERER
 */
public static String getHeaderReferer(HttpServletRequest request) {
    return request.getHeader(REFERER);
}

From source file:com.feilong.servlet.http.RequestUtil.java

/**
 * 1?Origin????,? (,URL?). <br>/* ww w .j a  v a 2 s.  c om*/
 * Referer?,Origin????URL,??. <br>
 * 2?Origin?POST,Referer.
 * 
 * @param request
 *            the request
 * @return request?? {@link HttpHeaders#ORIGIN} header,null
 * @see HttpHeaders#ORIGIN
 */
public static String getHeaderOrigin(HttpServletRequest request) {
    return request.getHeader(ORIGIN);
}

From source file:com.feilong.servlet.http.RequestUtil.java

/**
 * ?ajax./*ww  w .j  ava 2  s  . co  m*/
 * 
 * @param request
 *            the request
 * @return ajax  true
 * @see "http://en.wikipedia.org/wiki/X-Requested-With#Requested-With"
 */
public static boolean isAjaxRequest(HttpServletRequest request) {
    String header = request.getHeader(X_REQUESTED_WITH);
    return isNotNullOrEmpty(header) && header.equalsIgnoreCase(X_REQUESTED_WITH_VALUE_AJAX);
}

From source file:com.googlecode.psiprobe.Utils.java

public static void sendFile(HttpServletRequest request, HttpServletResponse response, File file)
        throws IOException {
    OutputStream out = response.getOutputStream();
    RandomAccessFile raf = new RandomAccessFile(file, "r");
    try {/*  w  w  w.j  ava  2s  . c  o m*/
        long fileSize = raf.length();
        long rangeStart = 0;
        long rangeFinish = fileSize - 1;

        // accept attempts to resume download (if any)
        String range = request.getHeader("Range");
        if (range != null && range.startsWith("bytes=")) {
            String pureRange = range.replaceAll("bytes=", "");
            int rangeSep = pureRange.indexOf("-");

            try {
                rangeStart = Long.parseLong(pureRange.substring(0, rangeSep));
                if (rangeStart > fileSize || rangeStart < 0) {
                    rangeStart = 0;
                }
            } catch (NumberFormatException e) {
                // ignore the exception, keep rangeStart unchanged
            }

            if (rangeSep < pureRange.length() - 1) {
                try {
                    rangeFinish = Long.parseLong(pureRange.substring(rangeSep + 1));
                    if (rangeFinish < 0 || rangeFinish >= fileSize) {
                        rangeFinish = fileSize - 1;
                    }
                } catch (NumberFormatException e) {
                    // ignore the exception
                }
            }
        }

        // set some headers
        response.setContentType("application/x-download");
        response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());
        response.setHeader("Accept-Ranges", "bytes");
        response.setHeader("Content-Length", Long.toString(rangeFinish - rangeStart + 1));
        response.setHeader("Content-Range", "bytes " + rangeStart + "-" + rangeFinish + "/" + fileSize);

        // seek to the requested offset
        raf.seek(rangeStart);

        // send the file
        byte[] buffer = new byte[4096];

        long len;
        int totalRead = 0;
        boolean nomore = false;
        while (true) {
            len = raf.read(buffer);
            if (len > 0 && totalRead + len > rangeFinish - rangeStart + 1) {
                // read more then required?
                // adjust the length
                len = rangeFinish - rangeStart + 1 - totalRead;
                nomore = true;
            }

            if (len > 0) {
                out.write(buffer, 0, (int) len);
                totalRead += len;
                if (nomore) {
                    break;
                }
            } else {
                break;
            }
        }
    } finally {
        raf.close();
    }
}

From source file:com.zimbra.cs.servlet.util.CsrfUtil.java

/**
 *
 * @param req/*from   ww w.  j av a2 s.co  m*/
 * @param allowedRefHost
 * @return
 * @throws MalformedURLException
 */
public static boolean isCsrfRequestBasedOnReferrer(final HttpServletRequest req, final String[] allowedRefHost)
        throws MalformedURLException {

    List<String> allowedRefHostList = Arrays.asList(allowedRefHost);
    boolean csrfReq = false;

    String method = req.getMethod();
    if (!method.equalsIgnoreCase("POST")) {
        csrfReq = false;
        return csrfReq;
    }

    String host = getRequestHost(req);
    String referrer = req.getHeader(HttpHeaders.REFERER);
    String refHost = null;

    if (!StringUtil.isNullOrEmpty(referrer)) {
        URL refURL = null;
        if (referrer.contains("http") || referrer.contains("https")) {
            refURL = new URL(referrer);
        } else {
            refURL = new URL("http://" + referrer);
        }
        refHost = refURL.getHost().toLowerCase();
    }

    if (refHost == null) {
        csrfReq = false;
    } else if (refHost.equalsIgnoreCase(host)) {
        csrfReq = false;
    } else {
        if (allowedRefHost != null && allowedRefHostList.contains(refHost)) {
            csrfReq = false;
        } else {
            csrfReq = true;
        }
    }

    if (ZimbraLog.soap.isDebugEnabled()) {
        ZimbraLog.soap.debug("Host : %s, Referrer host :%s, Allowed Hosts:[%s] Soap req is %s", host, refHost,
                Joiner.on(',').join(allowedRefHostList), (csrfReq ? " not allowed." : " allowed."));
    }

    return csrfReq;
}

From source file:com.indeed.imhotep.web.QueryServlet.java

/**
 * Gets the user name from the HTTP request if it was provided through Basic authentication.
 * /* www. j  ava 2s. c o m*/
 * @param request Http request
 * @return User name if Basic auth is used or null otherwise
 */
private static String getUserNameFromRequest(final HttpServletRequest request) {
    final String authHeader = request.getHeader("Authorization");
    if (authHeader == null) {
        // try simple
        final String rawUser = request.getRemoteUser();
        if (rawUser == null) {
            return null;
        } else {
            return rawUser;
        }
    } else {
        final String credStr;
        if (authHeader.startsWith("user ")) {
            credStr = authHeader.substring(5);
        } else {
            // try basic auth
            if (!authHeader.toUpperCase().startsWith("BASIC ")) {
                // Not basic
                return null;
            }

            // remove basic
            final String credEncoded = authHeader.substring(6); //length of 'BASIC '

            final byte[] credRaw = Base64.decodeBase64(credEncoded.getBytes());
            if (credRaw == null) {
                // invalid decoding
                return null;
            }

            credStr = new String(credRaw);
        }

        // get username part from username:password
        final String[] x = credStr.split(":");
        if (x.length < 1) {
            // bad split
            return null;
        }

        return x[0];
    }
}