List of usage examples for javax.servlet.http HttpServletRequest getContextPath
public String getContextPath();
From source file:com.runwaysdk.controller.ServletDispatcher.java
/** * This method strips the context path from the request URI and returns it. Use this method to handle URI's in a context path agnostic manner. * /* ww w .j ava 2 s. c o m*/ * @param request * @return */ private static final String getServletPath(HttpServletRequest request) { String servletPath = request.getServletPath(); if (!"".equals(servletPath)) { return servletPath; } String requestUri = request.getRequestURI(); int startIndex = request.getContextPath().equals("") ? 0 : request.getContextPath().length(); int endIndex = request.getPathInfo() == null ? requestUri.length() : requestUri.indexOf(request.getPathInfo()); return requestUri.substring(startIndex, endIndex); }
From source file:de.micromata.genome.logging.LogRequestDumpAttribute.java
/** * Gen http request dump.// w ww . j ava 2s.c o m * * @param req the req * @return the string */ @SuppressWarnings("unchecked") public static String genHttpRequestDump(HttpServletRequest req) { StringBuilder sb = new StringBuilder(); sb.append("method: ").append(req.getMethod()).append('\n')// .append("requestURL: ").append(req.getRequestURL()).append('\n')// .append("servletPath: ").append(req.getServletPath()).append('\n')// .append("requestURI: ").append(req.getRequestURI()).append('\n') // .append("queryString: ").append(req.getQueryString()).append('\n') // .append("pathInfo: ").append(req.getPathInfo()).append('\n')// .append("contextPath: ").append(req.getContextPath()).append('\n') // .append("characterEncoding: ").append(req.getCharacterEncoding()).append('\n') // .append("localName: ").append(req.getLocalName()).append('\n') // .append("contentLength: ").append(req.getContentLength()).append('\n') // ; sb.append("Header:\n"); for (Enumeration<String> en = req.getHeaderNames(); en.hasMoreElements();) { String hn = en.nextElement(); sb.append(" ").append(hn).append(": ").append(req.getHeader(hn)).append("\n"); } sb.append("Attr: \n"); Enumeration en = req.getAttributeNames(); for (; en.hasMoreElements();) { String k = (String) en.nextElement(); Object v = req.getAttribute(k); sb.append(" ").append(k).append(": ").append(Objects.toString(v, StringUtils.EMPTY)).append('\n'); } return sb.toString(); }
From source file:org.codehaus.groovy.grails.plugins.springsecurity.RedirectUtils.java
/** * Build a redirect url.//w w w . j av a 2s .co m * @param request the request * @param response the response * @param url the target url to redirect to * @return the url */ public static String buildRedirectUrl(final HttpServletRequest request, final HttpServletResponse response, final String url) { if (!url.startsWith("http://") && !url.startsWith("https://")) { String scheme = request.getScheme(); int serverPort = RESOLVER.getServerPort(request); boolean inHttp = "http".equalsIgnoreCase(scheme); boolean inHttps = "https".equalsIgnoreCase(scheme); boolean includePort = !((inHttp && serverPort == 80) || (inHttps && serverPort == 443)); String port = includePort ? ":" + serverPort : ""; return scheme + "://" + request.getServerName() + port + request.getContextPath() + url; } return url; }
From source file:de.betterform.agent.web.WebUtil.java
public static String getRequestURI(HttpServletRequest request) { StringBuffer buffer = new StringBuffer(request.getScheme()); buffer.append("://"); buffer.append(request.getServerName()); buffer.append(":"); buffer.append(request.getServerPort()); buffer.append(request.getContextPath()); return buffer.toString(); }
From source file:alpha.portal.webapp.util.RequestUtil.java
/** * Convenience method to get the application's URL based on request * variables.//from w w w .ja va2s .c o m * * @param request * the current request * @return URL to application */ public static String getAppURL(final HttpServletRequest request) { if (request == null) return ""; final StringBuffer url = new StringBuffer(); int port = request.getServerPort(); if (port < 0) { port = 80; // Work around java.net.URL bug } final String scheme = request.getScheme(); url.append(scheme); url.append("://"); url.append(request.getServerName()); if ((scheme.equals("http") && (port != 80)) || (scheme.equals("https") && (port != 443))) { url.append(':'); url.append(port); } url.append(request.getContextPath()); return url.toString(); }
From source file:com.arm.connector.bridge.core.Utils.java
public static String getBaseURL(String endpoint, HttpServletRequest request) { String url = ""; try {//from w w w . j av a2 s .co m url = request.getRequestURL().toString().replace(request.getRequestURI().substring(1), request.getContextPath()); url += "//" + endpoint; url = url.replace("://", "_TEMP_"); url = url.replace("//", "/"); url = url.replace("_TEMP_", "://"); } catch (Exception ex) { url = request.getRequestURL().toString(); } return url; }
From source file:it.greenvulcano.gvesb.adapter.http.utils.DumpUtils.java
public static void dump(HttpServletRequest request, StringBuffer log) throws IOException { String hN;/*w w w . j a va2s . c o m*/ log.append("-- DUMP HttpServletRequest START").append("\n"); log.append("Method : ").append(request.getMethod()).append("\n"); log.append("RequestedSessionId : ").append(request.getRequestedSessionId()).append("\n"); log.append("Scheme : ").append(request.getScheme()).append("\n"); log.append("IsSecure : ").append(request.isSecure()).append("\n"); log.append("Protocol : ").append(request.getProtocol()).append("\n"); log.append("ContextPath : ").append(request.getContextPath()).append("\n"); log.append("PathInfo : ").append(request.getPathInfo()).append("\n"); log.append("QueryString : ").append(request.getQueryString()).append("\n"); log.append("RequestURI : ").append(request.getRequestURI()).append("\n"); log.append("RequestURL : ").append(request.getRequestURL()).append("\n"); log.append("ContentType : ").append(request.getContentType()).append("\n"); log.append("ContentLength : ").append(request.getContentLength()).append("\n"); log.append("CharacterEncoding : ").append(request.getCharacterEncoding()).append("\n"); log.append("---- Headers START\n"); Enumeration<String> headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()) { hN = headerNames.nextElement(); log.append("[" + hN + "]="); Enumeration<String> headers = request.getHeaders(hN); while (headers.hasMoreElements()) { log.append("[" + headers.nextElement() + "]"); } log.append("\n"); } log.append("---- Headers END\n"); log.append("---- Body START\n"); log.append(IOUtils.toString(request.getInputStream())).append("\n"); log.append("---- Body END\n"); log.append("-- DUMP HttpServletRequest END \n"); }
From source file:com.rantop.web.util.web.ServletUtils.java
/** * Convenience method to get the application's URL based on request * variables.//from ww w. jav a 2 s .com */ public static String getAppURL(HttpServletRequest request) { StringBuffer url = new StringBuffer(); int port = request.getServerPort(); if (port < 0) { port = 80; // Work around java.net.URL bug } String scheme = request.getScheme(); url.append(scheme); url.append("://"); url.append(request.getServerName()); if ((scheme.equals("http") && (port != 80)) || (scheme.equals("https") && (port != 443))) { url.append(':'); url.append(port); } url.append(request.getContextPath()); return url.toString(); }
From source file:com.jpeterson.littles3.S3ObjectRequest.java
/** * Create an <code>S3Object</code> based on the request supporting virtual * hosting of buckets./*from w w w . j av a 2 s. co m*/ * * @param req * The original request. * @param baseHost * The <code>baseHost</code> is the HTTP Host header that is * "expected". This is used to help determine how the bucket name * will be interpreted. This is used to implement the "Virtual * Hosting of Buckets". * @param authenticator * The authenticator to use to authenticate this request. * @return An object initialized from the request. * @throws IllegalArgumentException * Invalid request. */ @SuppressWarnings("unchecked") public static S3ObjectRequest create(HttpServletRequest req, String baseHost, Authenticator authenticator) throws IllegalArgumentException, AuthenticatorException { S3ObjectRequest o = new S3ObjectRequest(); String pathInfo = req.getPathInfo(); String contextPath = req.getContextPath(); String requestURI = req.getRequestURI(); String undecodedPathPart = null; int pathInfoLength; String requestURL; String serviceEndpoint; String bucket = null; String key = null; String host; String value; String timestamp; baseHost = baseHost.toLowerCase(); host = req.getHeader("Host"); if (host != null) { host = host.toLowerCase(); } try { requestURL = URLDecoder.decode(req.getRequestURL().toString(), "UTF-8"); } catch (UnsupportedEncodingException e) { // should never happen e.printStackTrace(); IllegalArgumentException t = new IllegalArgumentException("Unsupport encoding: UTF-8"); t.initCause(e); throw t; } if (!requestURL.endsWith(pathInfo)) { String m = "requestURL [" + requestURL + "] does not end with pathInfo [" + pathInfo + "]"; throw new IllegalArgumentException(m); } pathInfoLength = pathInfo.length(); serviceEndpoint = requestURL.substring(0, requestURL.length() - pathInfoLength); if (debug) { System.out.println("---------------"); System.out.println("requestURI: " + requestURI); System.out.println("serviceEndpoint: " + serviceEndpoint); System.out.println("---------------"); } if ((host == null) || // http 1.0 form (host.equals(baseHost))) { // ordinary method // http 1.0 form // bucket first part of path info // key second part of path info if (pathInfoLength > 1) { int index = pathInfo.indexOf('/', 1); if (index > -1) { bucket = pathInfo.substring(1, index); if (pathInfoLength > (index + 1)) { key = pathInfo.substring(index + 1); undecodedPathPart = requestURI.substring(contextPath.length() + 1 + bucket.length(), requestURI.length()); } } else { bucket = pathInfo.substring(1); } } } else if (host.endsWith("." + baseHost)) { // bucket prefix of host // key is path info bucket = host.substring(0, host.length() - 1 - baseHost.length()); if (pathInfoLength > 1) { key = pathInfo.substring(1); undecodedPathPart = requestURI.substring(contextPath.length(), requestURI.length()); } } else { // bucket is host // key is path info bucket = host; if (pathInfoLength > 1) { key = pathInfo.substring(1); undecodedPathPart = requestURI.substring(contextPath.length(), requestURI.length()); } } // timestamp timestamp = req.getHeader("Date"); // CanonicalizedResource StringBuffer canonicalizedResource = new StringBuffer(); canonicalizedResource.append('/'); if (bucket != null) { canonicalizedResource.append(bucket); } if (undecodedPathPart != null) { canonicalizedResource.append(undecodedPathPart); } if (req.getParameter(PARAMETER_ACL) != null) { canonicalizedResource.append("?").append(PARAMETER_ACL); } // CanonicalizedAmzHeaders StringBuffer canonicalizedAmzHeaders = new StringBuffer(); Map<String, String> headers = new TreeMap<String, String>(); String headerName; String headerValue; for (Enumeration headerNames = req.getHeaderNames(); headerNames.hasMoreElements();) { headerName = ((String) headerNames.nextElement()).toLowerCase(); if (headerName.startsWith("x-amz-")) { for (Enumeration headerValues = req.getHeaders(headerName); headerValues.hasMoreElements();) { headerValue = (String) headerValues.nextElement(); String currentValue = headers.get(headerValue); if (currentValue != null) { // combine header fields with the same name headers.put(headerName, currentValue + "," + headerValue); } else { headers.put(headerName, headerValue); } if (headerName.equals("x-amz-date")) { timestamp = headerValue; } } } } for (Iterator<String> iter = headers.keySet().iterator(); iter.hasNext();) { headerName = iter.next(); headerValue = headers.get(headerName); canonicalizedAmzHeaders.append(headerName).append(":").append(headerValue).append("\n"); } StringBuffer stringToSign = new StringBuffer(); stringToSign.append(req.getMethod()).append("\n"); value = req.getHeader("Content-MD5"); if (value != null) { stringToSign.append(value); } stringToSign.append("\n"); value = req.getHeader("Content-Type"); if (value != null) { stringToSign.append(value); } stringToSign.append("\n"); value = req.getHeader("Date"); if (value != null) { stringToSign.append(value); } stringToSign.append("\n"); stringToSign.append(canonicalizedAmzHeaders); stringToSign.append(canonicalizedResource); if (debug) { System.out.println(":v:v:v:v:"); System.out.println("undecodedPathPart: " + undecodedPathPart); System.out.println("canonicalizedAmzHeaders: " + canonicalizedAmzHeaders); System.out.println("canonicalizedResource: " + canonicalizedResource); System.out.println("stringToSign: " + stringToSign); System.out.println(":^:^:^:^:"); } o.setServiceEndpoint(serviceEndpoint); o.setBucket(bucket); o.setKey(key); try { if (timestamp == null) { o.setTimestamp(null); } else { o.setTimestamp(DateUtil.parseDate(timestamp)); } } catch (DateParseException e) { o.setTimestamp(null); } o.setStringToSign(stringToSign.toString()); o.setRequestor(authenticate(req, o)); return o; }
From source file:net.oauth.example.consumer.webapp.CookieConsumer.java
/** * Get a fresh access token from the service provider. * /*www . j a va 2s. c o m*/ * @throws RedirectException * to obtain authorization */ private static void getAccessToken(HttpServletRequest request, CookieMap cookies, OAuthAccessor accessor) throws Exception { CLIENT.getRequestToken(accessor); String consumerName = (String) accessor.consumer.getProperty("name"); cookies.put(consumerName + ".requestToken", accessor.requestToken); cookies.put(consumerName + ".tokenSecret", accessor.tokenSecret); String authorizationURL = accessor.consumer.serviceProvider.userAuthorizationURL; if (authorizationURL.startsWith("/")) { authorizationURL = (new URL(new URL(request.getRequestURL().toString()), request.getContextPath() + authorizationURL)).toString(); } URL callbackURL = new URL(new URL(request.getRequestURL().toString()), request.getContextPath() + Callback.PATH); throw new RedirectException(OAuth.addParameters(authorizationURL // , "oauth_token", accessor.requestToken // , "oauth_callback", OAuth.addParameters(callbackURL.toString() // , "consumer", consumerName // , "returnTo", getRequestPath(request) // ))); }