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:com.spstudio.session.filter.SessionAOP.java

@Around(value = "@annotation(com.spstudio.session.UserSession)")
public Object aroundManager(ProceedingJoinPoint pj) throws Exception {
    HttpServletRequest request = SysContent.getRequest();
    HttpServletResponse response = SysContent.getResponse();
    HttpSession session = SysContent.getSession();

    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + path + "/";

    UserSessionType type = this.getSessionType(pj);
    if (type == null) {
        throw new Exception("The value of NeedSession is must.");
    }// ww  w .ja  v  a 2  s.c om

    Object uobj = session.getAttribute("user");
    Object mobj = session.getAttribute("manager");

    boolean isUser = type == UserSessionType.USER && uobj != null;
    boolean isManager = type == UserSessionType.MANAGER && mobj != null;
    boolean isUserOrManager = type == UserSessionType.OR && (mobj != null || uobj != null);
    try {
        if (isUser || isManager || isUserOrManager) {
            return pj.proceed();
        } else { // ?session  
            if (request.getHeader("x-requested-with") != null
                    && request.getHeader("x-requested-with").equalsIgnoreCase( //ajax?       
                            "XMLHttpRequest")) {
                response.addHeader("sessionstatus", "timeout");
                // EasyUi  
                //response.getWriter().print("{\"rows\":[],\"success\":false,\"total\":0}");       
            } else {//http?       
                response.sendRedirect(basePath + "error/nosession");
            }
        }
    } catch (Throwable e) {
        // TODO Auto-generated catch block  
        e.printStackTrace();
    }
    return null;
}

From source file:com.qperior.gsa.oneboxprovider.QPOneBoxProviderServlet.java

/**
 * Called by the application server's servlet runner when GET method
 * requests are made for this servlet.  OneBox clients (such as the
 * Google Search Appliance) make HTTP requests to OneBox providers
 * exclusivley using the GET method./*w  ww .  j  a v  a  2 s  . c o  m*/
 */
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    logHeaders(request);
    this.webAppBaseURL = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + request.getContextPath() + "/";

    processRequest(request, response);
}

From source file:org.exoplatform.platform.portlet.juzu.notificationsAdmin.NotificationsAdministration.java

private void redirectToHomePage() {
    PortalRequestContext portalRequestContext = Util.getPortalRequestContext();
    HttpServletRequest currentServletRequest = portalRequestContext.getRequest();
    StringBuilder sb = new StringBuilder();
    sb.append(currentServletRequest.getScheme()).append("://").append(currentServletRequest.getServerName())
            .append(":").append(currentServletRequest.getServerPort()).append("/")
            .append(PortalContainer.getCurrentPortalContainerName()).append("/")
            .append(Util.getPortalRequestContext().getPortalOwner());

    WebuiRequestContext ctx = WebuiRequestContext.getCurrentInstance();
    JavascriptManager jsManager = ctx.getJavascriptManager();
    jsManager.addJavascript("try { window.location.href='" + sb.toString() + "' } catch(e) {"
            + "window.location.href('" + sb.toString() + "') }");
}

From source file:be.fedict.eid.dss.webapp.DocumentViewerServlet.java

private void setResponseHeaders(HttpServletRequest request, HttpServletResponse response) {

    response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate, max-age=-1"); // http
    // 1.1/*ww w.j  a  v a2  s .c  o m*/
    if (!request.getScheme().equals("https")) {
        // else the download fails in IE
        response.setHeader("Pragma", "no-cache"); // http 1.0
    } else {
        response.setHeader("Pragma", "public");
    }
    response.setDateHeader("Expires", -1);

}

From source file:grails.plugin.springsecurity.web.GrailsRedirectStrategy.java

protected String calculateRedirectUrl(HttpServletRequest request, String url) {
    if (UrlUtils.isAbsoluteUrl(url)) {
        return url;
    }//from  ww w. j av  a 2  s .  c  o m

    url = request.getContextPath() + url;

    if (!useHeaderCheckChannelSecurity) {
        return url;
    }

    return UrlUtils.buildFullRequestUrl(request.getScheme(), request.getServerName(),
            portResolver.getServerPort(request), url, null);
}

From source file:info.joseluismartin.gtc.mvc.CacheController.java

/**
 * @param req/*from  ww w .j  a  va 2  s  .  c  o m*/
 * @return
 */
private String getContextUrl(HttpServletRequest req) {

    StringBuffer url = new StringBuffer();
    String scheme = req.getScheme();
    int port = req.getServerPort();
    String servletPath = req.getServletPath();
    String contextPaht = req.getContextPath();
    url.append(scheme);
    url.append("://");
    url.append(req.getServerName());

    if ((scheme.equals("http") && port != 80) || (scheme.equals("https") && port != 443)) {
        url.append(':');
        url.append(req.getServerPort());
    }

    if (contextPaht != null)
        url.append(contextPaht);

    if (servletPath != null)
        url.append(servletPath);

    return url.toString();
}

From source file:org.broadleafcommerce.core.web.controller.account.BroadleafLoginController.java

public String getResetPasswordUrl(HttpServletRequest request) {
    String url = request.getScheme() + "://" + request.getServerName()
            + getResetPasswordPort(request, request.getScheme() + "/");

    if (request.getContextPath() != null && !"".equals(request.getContextPath())) {
        url = url + request.getContextPath() + getResetPasswordView();
    } else {/*from   ww w  .ja va  2  s .  co  m*/
        url = url + getResetPasswordView();
    }
    return url;
}

From source file:mashups.eventpub.EventPublisherServlet.java

private String getCurrentUrl(HttpServletRequest request) throws MalformedURLException {
    URL currentUrl = new URL(request.getScheme(), request.getServerName(), request.getServerPort(),
            request.getRequestURI());/* w w w .  j  ava2s .  co  m*/
    return currentUrl.toString();
}

From source file:org.apache.shindig.gadgets.servlet.ServletUtilTest.java

@Test
public void testFromHttpServletRequest() throws Exception {
    HttpServletRequest original = EasyMock.createMock(HttpServletRequest.class);
    EasyMock.expect(original.getScheme()).andReturn("https");
    EasyMock.expect(original.getServerName()).andReturn("www.example.org");
    EasyMock.expect(original.getServerPort()).andReturn(444);
    EasyMock.expect(original.getRequestURI()).andReturn("/path/foo");
    EasyMock.expect(original.getQueryString()).andReturn("one=two&three=four");
    Vector<String> headerNames = new Vector<String>();
    headerNames.add("Header1");
    headerNames.add("Header2");
    EasyMock.expect(original.getHeaderNames()).andReturn(headerNames.elements());
    EasyMock.expect(original.getHeaders("Header1")).andReturn(makeEnumeration("HVal1", "HVal3"));
    EasyMock.expect(original.getHeaders("Header2")).andReturn(makeEnumeration("HVal2", "HVal4"));
    EasyMock.expect(original.getMethod()).andReturn("post");
    final ByteArrayInputStream bais = new ByteArrayInputStream("post body".getBytes());
    ServletInputStream sis = new ServletInputStream() {
        @Override/*from  w  w  w  . j a v  a2 s . c o  m*/
        public int read() throws IOException {
            return bais.read();
        }
    };
    EasyMock.expect(original.getInputStream()).andReturn(sis);
    EasyMock.expect(original.getRemoteAddr()).andReturn("1.2.3.4");

    EasyMock.replay(original);
    HttpRequest request = ServletUtil.fromHttpServletRequest(original);
    EasyMock.verify(original);

    assertEquals(Uri.parse("https://www.example.org:444/path/foo?one=two&three=four"), request.getUri());
    assertEquals(3, request.getHeaders().size());
    assertEquals("HVal1", request.getHeaders("Header1").get(0));
    assertEquals("HVal3", request.getHeaders("Header1").get(1));
    assertEquals("HVal2", request.getHeaders("Header2").get(0));
    assertEquals("HVal4", request.getHeaders("Header2").get(1));
    assertEquals("post", request.getMethod());
    assertEquals("post body", request.getPostBodyAsString());
    assertEquals("1.2.3.4", request.getParam(ServletUtil.REMOTE_ADDR_KEY));
}

From source file:org.apache.struts2.views.util.UrlHelper.java

public static String buildUrl(String action, HttpServletRequest request, HttpServletResponse response,
        Map params, String scheme, boolean includeContext, boolean encodeResult,
        boolean forceAddSchemeHostAndPort, boolean escapeAmp) {
    StringBuilder link = new StringBuilder();

    boolean changedScheme = false;

    // FIXME: temporary hack until class is made a properly injected bean
    Container cont = ActionContext.getContext().getContainer();
    int httpPort = Integer.parseInt(cont.getInstance(String.class, StrutsConstants.STRUTS_URL_HTTP_PORT));
    int httpsPort = Integer.parseInt(cont.getInstance(String.class, StrutsConstants.STRUTS_URL_HTTPS_PORT));

    // only append scheme if it is different to the current scheme *OR*
    // if we explicity want it to be appended by having forceAddSchemeHostAndPort = true
    if (forceAddSchemeHostAndPort) {
        String reqScheme = request.getScheme();
        changedScheme = true;//from w ww . j  a  v  a  2  s  .  c o  m
        link.append(scheme != null ? scheme : reqScheme);
        link.append("://");
        link.append(request.getServerName());

        if (scheme != null) {
            // If switching schemes, use the configured port for the particular scheme.
            if (!scheme.equals(reqScheme)) {
                if ((scheme.equals("http") && (httpPort != DEFAULT_HTTP_PORT))
                        || (scheme.equals("https") && httpsPort != DEFAULT_HTTPS_PORT)) {
                    link.append(":");
                    link.append(scheme.equals("http") ? httpPort : httpsPort);
                }
                // Else use the port from the current request.
            } else {
                int reqPort = request.getServerPort();

                if ((scheme.equals("http") && (reqPort != DEFAULT_HTTP_PORT))
                        || (scheme.equals("https") && reqPort != DEFAULT_HTTPS_PORT)) {
                    link.append(":");
                    link.append(reqPort);
                }
            }
        }
    } else if ((scheme != null) && !scheme.equals(request.getScheme())) {
        changedScheme = true;
        link.append(scheme);
        link.append("://");
        link.append(request.getServerName());

        if ((scheme.equals("http") && (httpPort != DEFAULT_HTTP_PORT))
                || (scheme.equals("https") && httpsPort != DEFAULT_HTTPS_PORT)) {
            link.append(":");
            link.append(scheme.equals("http") ? httpPort : httpsPort);
        }
    }

    if (action != null) {
        // Check if context path needs to be added
        // Add path to absolute links
        if (action.startsWith("/") && includeContext) {
            String contextPath = request.getContextPath();
            if (!contextPath.equals("/")) {
                link.append(contextPath);
            }
        } else if (changedScheme) {

            // (Applicable to Servlet 2.4 containers)
            // If the request was forwarded, the attribute below will be set with the original URL
            String uri = (String) request.getAttribute("javax.servlet.forward.request_uri");

            // If the attribute wasn't found, default to the value in the request object
            if (uri == null) {
                uri = request.getRequestURI();
            }

            link.append(uri.substring(0, uri.lastIndexOf('/') + 1));
        }

        // Add page
        link.append(action);
    } else {
        // Go to "same page"
        String requestURI = (String) request.getAttribute("struts.request_uri");

        // (Applicable to Servlet 2.4 containers)
        // If the request was forwarded, the attribute below will be set with the original URL
        if (requestURI == null) {
            requestURI = (String) request.getAttribute("javax.servlet.forward.request_uri");
        }

        // If neither request attributes were found, default to the value in the request object
        if (requestURI == null) {
            requestURI = request.getRequestURI();
        }

        link.append(requestURI);
    }

    //if the action was not explicitly set grab the params from the request
    if (escapeAmp) {
        buildParametersString(params, link);
    } else {
        buildParametersString(params, link, "&");
    }

    String result = link.toString();

    while (result.indexOf("<script>") > 0) {
        result = result.replaceAll("<script>", "script");
    }
    try {
        result = encodeResult ? response.encodeURL(result) : result;
    } catch (Exception ex) {
        // Could not encode the URL for some reason
        // Use it unchanged
        result = link.toString();
    }

    return result;
}