Example usage for javax.servlet.http HttpServletRequest getScheme

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

Introduction

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

Prototype

public String getScheme();

Source Link

Document

Returns the name of the scheme used to make this request, for example, <code>http</code>, <code>https</code>, or <code>ftp</code>.

Usage

From source file:net.siegmar.japtproxy.JaptProxy.java

/**
 * Analyzes (validates) request url and extract required information.
 *
 * @param request the object to populate with extracted information.
 * @throws net.siegmar.japtproxy.exception.InvalidRequestException is thrown if requested url is invalid.
 *///w ww  .  j a  va2s  .c  o  m
private static RequestedData buildRequestedData(final HttpServletRequest request,
        final Configuration configuration) throws InvalidRequestException {
    final String resource = request.getPathInfo();
    final RequestedData requestedData = new RequestedData();
    requestedData.setRequestedResource(resource);
    requestedData.setRequestModifiedSince(request.getDateHeader(HttpHeaderConstants.IF_MODIFIED_SINCE));

    requestedData.setUserAgent(request.getHeader(HttpHeaderConstants.USER_AGENT));
    requestedData.setUrl(getURL(request, configuration));
    requestedData.setHostUrl(getURL(request, true, configuration));
    requestedData.setScheme(request.getScheme());
    requestedData.setServerName(request.getServerName());
    requestedData.setServerPort(request.getServerPort());

    final String requestedResource = requestedData.getRequestedResource();

    // Reject if no requested resource is specified
    if (requestedResource == null) {
        throw new InvalidRequestException("Rejected request because it doesn't contain a resource request");
    }

    // Reject requests that contain /../ for security reason
    if (requestedResource.contains("/../")) {
        throw new InvalidRequestException(
                "Rejected request '" + requestedResource + "' because it contains /../");
    }

    // Reject requests that doesn't contain a backend
    final int endIdx = requestedResource.indexOf('/', 1);
    if (endIdx <= 1) {
        throw new InvalidRequestException(
                "Rejected request '" + requestedResource + "' because it doesn't specify a backend");
    }

    // Reject requests that doesn't contain a target resource
    if (requestedResource.length() == endIdx + 1) {
        throw new InvalidRequestException("Rejected request '" + requestedResource
                + "' because it doesn't specify a target " + "resource");
    }

    // Extract the backend and target resource parts of the request
    final String requestedBackend = requestedResource.substring(1, endIdx);
    final String requestedTarget = requestedResource.substring(endIdx);

    // Set requestedData
    requestedData.setRequestedTarget(requestedTarget);
    requestedData.setRequestedBackend(requestedBackend);

    return requestedData;
}

From source file:com.tc.utils.XSPUtils.java

public static String buildHostAndWebPath(FacesContext context, Database db) throws NotesException {
    HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
    final StringBuilder url = new StringBuilder(48);
    String webPath = db.getFilePath().replace("\\", "/");

    String scheme = request.getScheme();

    url.append(scheme);//  w ww . j av  a 2s.c o  m
    url.append("://");
    url.append(request.getServerName());

    int port = request.getServerPort();
    if (port > 0 && (("http".equalsIgnoreCase(scheme) && port != 80)
            || ("https".equalsIgnoreCase(scheme) && port != 443))) {
        url.append(':');
        url.append(port);
    }

    url.append('/');
    url.append(webPath);
    return url.toString();
}

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

/**
 * scheme+serverName+port+getContextPath.
 * /*from w  ww  .  j a  v a  2s  . c o  m*/
 * <p>
 *  http https.
 * <p>
 * 
 * @param request
 *            the request
 * @return :http://localhost:8080/feilong/
 * @see "org.apache.catalina.connector.Request#getRequestURL()"
 * @see "org.apache.catalina.realm.RealmBase#hasUserDataPermission(Request, Response, SecurityConstraint[])"
 * @see javax.servlet.http.HttpUtils#getRequestURL(HttpServletRequest)
 */
public static String getServerRootWithContextPath(HttpServletRequest request) {
    String scheme = request.getScheme();
    int port = request.getServerPort() < 0 ? 80 : request.getServerPort();// Work around java.net.URL bug
    //*************************************************************************************
    StringBuilder sb = new StringBuilder();
    sb.append(scheme);
    sb.append("://");
    sb.append(request.getServerName());

    if ((scheme.equals(SCHEME_HTTP) && (port != 80)) || (scheme.equals(SCHEME_HTTPS) && (port != 443))) {
        sb.append(':');
        sb.append(port);
    }

    sb.append(request.getContextPath());
    return sb.toString();
}

From source file:com.tc.utils.XSPUtils.java

public static String buildXpageUrl(HttpServletRequest request, String xpage, Document doc)
        throws NotesException {
    final StringBuilder url = new StringBuilder(48);

    String webPath = doc.getParentDatabase().getFilePath().replace("\\", "/");

    String scheme = request.getScheme();

    url.append(scheme);/*from w  w  w.  j a  v a 2s.  com*/
    url.append("://");
    url.append(request.getServerName());

    int port = request.getServerPort();
    if (port > 0 && (("http".equalsIgnoreCase(scheme) && port != 80)
            || ("https".equalsIgnoreCase(scheme) && port != 443))) {
        url.append(':');
        url.append(port);
    }

    url.append(webPath);
    url.append('/');
    url.append(xpage);
    url.append("?documentId=" + doc.getUniversalID());
    url.append("&action=openDocument");

    return url.toString();
}

From source file:org.alfresco.web.forms.xforms.XFormsBean.java

private static void rewriteInlineURIs(final Document schemaDocument, final String cwdAvmPath) {
    final NodeList nl = XMLUtil.combine(
            schemaDocument.getElementsByTagNameNS(NamespaceConstants.XMLSCHEMA_NS, "include"),
            schemaDocument.getElementsByTagNameNS(NamespaceConstants.XMLSCHEMA_NS, "import"));

    if (LOGGER.isDebugEnabled())
        LOGGER.debug("rewriting " + nl.getLength() + " includes");

    for (int i = 0; i < nl.getLength(); i++) {
        final Element includeEl = (Element) nl.item(i);
        if (includeEl.hasAttribute("schemaLocation")) {
            String uri = includeEl.getAttribute("schemaLocation");
            String finalURI = null;

            if (uri == null || uri.startsWith("http://") || uri.startsWith("https://")) {
                if (LOGGER.isDebugEnabled())
                    LOGGER.debug("not rewriting " + uri);

                continue;
            }//  ww  w .j  a  v  a2  s  .c  o m

            if (uri.startsWith("webscript://")) {
                // It's a web script include / import
                final FacesContext facesContext = FacesContext.getCurrentInstance();
                final ExternalContext externalContext = facesContext.getExternalContext();
                final HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();

                final String baseURI = (request.getScheme() + "://" + request.getServerName() + ':'
                        + request.getServerPort() + request.getContextPath() + "/wcservice");
                String rewrittenURI = uri;

                if (uri.contains("${storeid}")) {
                    final String storeId = AVMUtil.getStoreName(cwdAvmPath);
                    rewrittenURI = uri.replace("${storeid}", storeId);
                } else if (uri.contains("{storeid}")) {
                    final String storeId = AVMUtil.getStoreName(cwdAvmPath);
                    rewrittenURI = uri.replace("{storeid}", storeId);
                } else {
                    if (LOGGER.isDebugEnabled())
                        LOGGER.debug("no store id specified in webscript URI " + uri);
                }

                if (uri.contains("${ticket}")) {
                    AuthenticationService authenticationService = Repository.getServiceRegistry(facesContext)
                            .getAuthenticationService();
                    final String ticket = authenticationService.getCurrentTicket();
                    rewrittenURI = rewrittenURI.replace("${ticket}", ticket);
                } else if (uri.contains("{ticket}")) {
                    AuthenticationService authenticationService = Repository.getServiceRegistry(facesContext)
                            .getAuthenticationService();
                    final String ticket = authenticationService.getCurrentTicket();
                    rewrittenURI = rewrittenURI.replace("{ticket}", ticket);
                } else {
                    if (LOGGER.isDebugEnabled())
                        LOGGER.debug("no ticket specified in webscript URI " + uri);
                }

                rewrittenURI = rewrittenURI.replaceAll("%26", "&");

                finalURI = baseURI + rewrittenURI.replace("webscript://", "/");

                if (LOGGER.isDebugEnabled())
                    LOGGER.debug("Final URI " + finalURI);
            } else {
                // It's a web project asset include / import
                final String baseURI = (uri.charAt(0) == '/'
                        ? AVMUtil.getPreviewURI(AVMUtil.getStoreName(cwdAvmPath))
                        : AVMUtil.getPreviewURI(cwdAvmPath));

                finalURI = baseURI + uri;
            }

            if (LOGGER.isDebugEnabled())
                LOGGER.debug("rewriting " + uri + " as " + finalURI);

            includeEl.setAttribute("schemaLocation", finalURI);
        }
    }
}

From source file:org.jahia.utils.Url.java

public static String appendServerNameIfNeeded(JCRNodeWrapper node, String nodeURL, HttpServletRequest request)
        throws RepositoryException, MalformedURLException {
    if (!SettingsBean.getInstance().isUrlRewriteUseAbsoluteUrls()) {
        return nodeURL;
    }/*from  w ww .  j  a v a  2 s.c o m*/

    String requestServerName = request.getServerName();
    if (isLocalhost(requestServerName)) {
        return nodeURL;
    }

    String serverName = node.getResolveSite().getServerName();
    if (!StringUtils.isEmpty(serverName) && !isLocalhost(serverName) && !requestServerName.equals(serverName)) {
        int serverPort = SettingsBean.getInstance().getSiteURLPortOverride();

        if (serverPort == 0) {
            serverPort = request.getServerPort();
        }
        if (serverPort == 80 && "http".equals(request.getScheme())
                || serverPort == 443 && "https".equals(request.getScheme())) {
            serverPort = -1;
        }
        nodeURL = new URL(request.getScheme(), serverName, serverPort, nodeURL).toString();
    }
    return nodeURL;
}

From source file:org.artifactory.util.HttpUtils.java

public static String getServletContextUrl(HttpServletRequest httpRequest) {
    String origUrl = httpRequest.getHeader(ArtifactoryRequest.ARTIFACTORY_OVERRIDE_BASE_URL);
    if (StringUtils.isNotBlank(origUrl)) {
        // original artifactory request url overrides request and base url
        return origUrl;
    }/*from w ww  .j  a  v  a  2s .  c  om*/
    CentralConfigService centralConfigService = ContextHelper.get().getCentralConfig();
    String baseUrl = centralConfigService.getDescriptor().getUrlBase();
    if (!StringUtils.isEmpty(baseUrl)) {
        String scheme = httpRequest.getScheme();
        if (baseUrl.startsWith(scheme)) {
            return baseUrl;
        } else {
            int idx = baseUrl.indexOf("://");
            if (idx > 0) {
                return scheme + "://" + baseUrl.substring(idx + 3);
            } else {
                return scheme + "://" + baseUrl;
            }
        }
    }
    return getServerUrl(httpRequest) + httpRequest.getContextPath();
}

From source file:fr.paris.lutece.util.http.SecurityUtil.java

/**
 * Write request variables into the dump stringbuffer
 * @param sb The dump stringbuffer/*from w w  w.  ja v a  2s .  c o m*/
 * @param request The HTTP request
 */
private static void dumpVariables(StringBuffer sb, HttpServletRequest request) {
    dumpVariable(sb, "AUTH_TYPE", request.getAuthType());
    dumpVariable(sb, "REQUEST_METHOD", request.getMethod());
    dumpVariable(sb, "PATH_INFO", request.getPathInfo());
    dumpVariable(sb, "PATH_TRANSLATED", request.getPathTranslated());
    dumpVariable(sb, "QUERY_STRING", request.getQueryString());
    dumpVariable(sb, "REQUEST_URI", request.getRequestURI());
    dumpVariable(sb, "SCRIPT_NAME", request.getServletPath());
    dumpVariable(sb, "LOCAL_ADDR", request.getLocalAddr());
    dumpVariable(sb, "SERVER_PROTOCOL", request.getProtocol());
    dumpVariable(sb, "REMOTE_ADDR", request.getRemoteAddr());
    dumpVariable(sb, "REMOTE_HOST", request.getRemoteHost());
    dumpVariable(sb, "HTTPS", request.getScheme());
    dumpVariable(sb, "SERVER_NAME", request.getServerName());
    dumpVariable(sb, "SERVER_PORT", String.valueOf(request.getServerPort()));
}

From source file:com.madadipouya.neatgeoip.rest.HomeRestController.java

@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView redirectToDocPage(HttpServletRequest request) {
    return new ModelAndView("redirect:" + request.getScheme() + HOMEPAGE_URL);
}

From source file:org.simbasecurity.core.service.http.ChangePasswordController.java

private String reconstructSimbaWebURL(HttpServletRequest request) {
    return request.getScheme() + "://" + request.getServerName()
            + (request.getServerPort() != 80 ? ":" + request.getServerPort() : "") + request.getContextPath();
}