Example usage for javax.servlet ServletRequest getScheme

List of usage examples for javax.servlet ServletRequest getScheme

Introduction

In this page you can find the example usage for javax.servlet ServletRequest 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:com.springsource.hq.plugin.tcserver.serverconfig.web.support.RequestUtils.java

/**
 * Returns the url to access HQ locally, i.e. without routing through any proxy or load balancer which may be in
 * front of HQ./*from   w  w  w.  j  av a  2  s .c  om*/
 * 
 * @param request A request received by the HQ server from which the HQ URL will be determined
 * @return the local URL for the HQ server hosting the web app
 */
public static String getLocalHqUrl(ServletRequest request) {
    StringBuilder serverUrl = new StringBuilder();

    serverUrl.append(request.getScheme());
    serverUrl.append("://");
    String hostName = request.getLocalName();
    if (hostName.contains(":")) {
        hostName = "[" + hostName + "]";
    }
    serverUrl.append(hostName);
    serverUrl.append(":");
    serverUrl.append(request.getLocalPort());
    if (request.isSecure()) {
        LOGGER.debug("Registering protocol.");
        UntrustedSSLProtocolSocketFactory.register();
    }
    return serverUrl.toString();
}

From source file:de.iteratec.iteraplan.businesslogic.common.URLBuilder.java

/**
 * Retrieves the URL of the server. It is also needed in case of ServletReqeusts, where
 * ContextPath is not available, so URL is without application name. It should be something like:
 * https://localhost:8443 This method is private because the iteraplan.properties are not checked
 * to see if the user has entered an application url by hand as in the other methods. Therefore
 * there could be problems if this method is used directly and the user depends on the property
 * setting.//from ww  w . j a  va 2s .  c o  m
 * 
 * @param req
 *          The current servlet request providing the necessary information.
 * @return The constructed application URL.
 */
private static String getServerURL(ServletRequest req) {
    StringBuilder serverUrl = new StringBuilder(BUFFER_SIZE);

    serverUrl.append(req.getScheme());
    serverUrl.append(COLON);
    serverUrl.append(FORWARD_SLASH);
    serverUrl.append(FORWARD_SLASH);
    serverUrl.append(req.getServerName());

    int port = req.getServerPort();
    if (port != WEB_PORT) {
        serverUrl.append(COLON);
        serverUrl.append(Integer.toString(port));
    }

    return serverUrl.toString();
}

From source file:pt.webdetails.cdf.dd.DashboardDesignerContentGenerator.java

static public String getScheme(IParameterProvider pathParams) {
    try {/*from w  ww  .ja  va 2 s .  c  om*/
        ServletRequest req = (ServletRequest) (pathParams.getParameter("httprequest"));
        return req.getScheme();
    } catch (Exception e) {
        return "http";
    }
}

From source file:com.sourcesense.alfresco.opensso.AlfrescoOpenSSOFilter.java

protected String buildURLForRedirect(ServletRequest request) {
    String serverURL = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
    String alfrescoContext = ((HttpServletRequest) request).getContextPath();
    return getOpenSSOLoginURL().concat("?goto=").concat(serverURL).concat(alfrescoContext);
}

From source file:org.niord.core.NiordApp.java

/**
 * Registers the server name associated with the current thread (i.e. servlet request)
 * @param req the servlet request//from w  ww . ja  v  a 2s.c o m
 */
public void registerServerNameForCurrentThread(ServletRequest req) {
    String scheme = StringUtils.defaultIfBlank(req.getScheme(), "http");
    String serverName = StringUtils.defaultIfBlank(req.getServerName(), "localhost");
    String port = (scheme.equalsIgnoreCase("https"))
            ? (req.getServerPort() == 443 ? "" : ":" + req.getServerPort())
            : (req.getServerPort() == 80 ? "" : ":" + req.getServerPort());
    if (StringUtils.isNotBlank(serverName)) {
        THREAD_LOCAL_SERVER_NAME.set(scheme + "://" + serverName + port);
    }
}

From source file:com.netpace.cms.sso.filter.AlfrescoOpenSSOFilter.java

protected String buildURLForRedirect(ServletRequest request) {
    String serverURL = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
    String alfrescoContext = ((HttpServletRequest) request).getContextPath();
    System.out.println("ServerURL: " + serverURL);
    return getOpenSSOLoginURL().concat("?goto=").concat(serverURL).concat(alfrescoContext);
}

From source file:org.codehaus.groovy.grails.plugins.acegi.GrailsAccessDeniedHandlerImpl.java

public void handle(ServletRequest request, ServletResponse response,
        AccessDeniedException accessDeniedException) throws IOException, ServletException {
    HttpServletRequest req = (HttpServletRequest) request;

    if (errorPage != null || (ajaxErrorPage != null && req.getHeader(ajaxHeader) != null)) {
        boolean includePort = true;
        String scheme = request.getScheme();
        String serverName = request.getServerName();
        int serverPort = portResolver.getServerPort(request);
        String contextPath = req.getContextPath();
        boolean inHttp = "http".equals(scheme.toLowerCase());
        boolean inHttps = "https".equals(scheme.toLowerCase());

        if (inHttp && (serverPort == 80)) {
            includePort = false;//from   w ww .  j a v  a 2s  . c  o  m
        } else if (inHttps && (serverPort == 443)) {
            includePort = false;
        }

        String commonRedirectUrl = scheme + "://" + serverName + ((includePort) ? (":" + serverPort) : "")
                + contextPath;
        String redirectUrl = commonRedirectUrl + "";
        if (ajaxErrorPage != null && req.getHeader(ajaxHeader) != null) {
            redirectUrl = commonRedirectUrl + ajaxErrorPage;
        } else if (errorPage != null) {
            redirectUrl = commonRedirectUrl + errorPage;
        } else {
            ((HttpServletResponse) response).sendError(HttpServletResponse.SC_FORBIDDEN,
                    accessDeniedException.getMessage());
        }

        ((HttpServletResponse) response)
                .sendRedirect(((HttpServletResponse) response).encodeRedirectURL(redirectUrl));
    }

    if (!response.isCommitted()) {
        // Send 403 (we do this after response has been written)
        ((HttpServletResponse) response).sendError(HttpServletResponse.SC_FORBIDDEN,
                accessDeniedException.getMessage());
    }
}

From source file:org.hi.framework.acgei.AuthenticationProcessingFilterEntryPoint.java

public void commence(ServletRequest request, ServletResponse response, AuthenticationException authException)
        throws IOException, ServletException {
    HttpServletRequest req = (HttpServletRequest) request;
    String scheme = request.getScheme();
    String serverName = request.getServerName();
    int serverPort = portResolver.getServerPort(request);
    String contextPath = req.getContextPath();

    boolean inHttp = "http".equals(scheme.toLowerCase());
    boolean inHttps = "https".equals(scheme.toLowerCase());

    boolean includePort = true;

    String redirectUrl = null;/*from   w w  w. j  a v a2s .  c  o m*/
    boolean doForceHttps = false;
    Integer httpsPort = null;

    if (inHttp && (serverPort == 80)) {
        includePort = false;
    } else if (inHttps && (serverPort == 443)) {
        includePort = false;
    }

    if (forceHttps && inHttp) {
        httpsPort = (Integer) portMapper.lookupHttpsPort(new Integer(serverPort));

        if (httpsPort != null) {
            doForceHttps = true;
            if (httpsPort.intValue() == 443) {
                includePort = false;
            } else {
                includePort = true;
            }
        }

    }

    if (serverSideRedirect) {

        if (doForceHttps) {

            // before doing server side redirect, we need to do client redirect to https.

            String servletPath = req.getServletPath();
            String pathInfo = req.getPathInfo();
            String query = req.getQueryString();

            redirectUrl = "https://" + serverName + ((includePort) ? (":" + httpsPort) : "") + contextPath
                    + servletPath + (pathInfo == null ? "" : pathInfo) + (query == null ? "" : "?" + query);

        } else {

            if (logger.isDebugEnabled()) {
                logger.debug("Server side forward to: " + loginFormUrl);
            }

            RequestDispatcher dispatcher = req.getRequestDispatcher(loginFormUrl);

            dispatcher.forward(request, response);

            return;

        }

    } else {

        if (doForceHttps) {

            redirectUrl = "https://" + serverName + ((includePort) ? (":" + httpsPort) : "") + contextPath
                    + loginFormUrl;

        } else {

            redirectUrl = scheme + "://" + serverName + ((includePort) ? (":" + serverPort) : "") + contextPath
                    + loginFormUrl;

        }
    }

    if (logger.isDebugEnabled()) {
        logger.debug("Redirecting to: " + redirectUrl);
    }

    //-------------        for DWZ    ------------------//

    if ("XMLHttpRequest".equalsIgnoreCase(((HttpServletRequest) request).getHeader("X-Requested-With"))
            || request.getParameter("ajax") != null) {
        PrintWriter out = response.getWriter();
        out.println("{\"statusCode\":\"301\",\"message\":\"Session Timeout! Please re-sign in!\"}");
        throw new BusinessException("Session Timeout");
        //         throw new BusinessException("{\"statusCode\":\"301\",\"message\":\"Session Timeout! Please re-sign in!\"}");
    } else {
        ((HttpServletResponse) response)
                .sendRedirect(((HttpServletResponse) response).encodeRedirectURL(redirectUrl));
    }
}

From source file:org.jboss.dashboard.ui.components.URLMarkupGenerator.java

public String getContextHost(ServletRequest request) {
    StringBuffer sb = new StringBuffer();
    String context = ((HttpServletRequest) request).getContextPath();
    String protocol = request.getScheme();
    while (context.startsWith("/"))
        context = context.substring(1);/*from   ww w. j  av a  2 s.c om*/
    sb.append(protocol.toLowerCase()).append("://").append(request.getServerName());
    if (request.getServerPort() != 80) {
        sb.append(":").append(request.getServerPort());
    }
    return sb.toString();
}

From source file:org.jboss.dashboard.ui.components.URLMarkupGenerator.java

/**
 * Get the base href for current request
 * @param request/*from w  w  w .  jav a2 s .  c o  m*/
 * @return a String with the form http://host[:port]/[context/]
 * @throws IOException
 */
public String getBaseHref(ServletRequest request) throws IOException {
    StringBuffer sb = new StringBuffer();
    String context = ((HttpServletRequest) request).getContextPath();
    String protocol = request.getScheme();
    while (context.startsWith("/")) {
        context = context.substring(1);
    }
    sb.append(protocol.toLowerCase()).append("://").append(request.getServerName());
    if (request.getServerPort() != 80) {
        sb.append(":").append(request.getServerPort());
    }
    sb.append("/");
    if (!StringUtils.isEmpty(context)) {
        sb.append(context).append("/");
    }
    return sb.toString();
}