Example usage for javax.servlet ServletRequest getServerName

List of usage examples for javax.servlet ServletRequest getServerName

Introduction

In this page you can find the example usage for javax.servlet ServletRequest getServerName.

Prototype

public String getServerName();

Source Link

Document

Returns the host name of the server to which the request was sent.

Usage

From source file:org.everit.jetty.server.ecm.tests.EchoRemoteInfoServlet.java

@Override
public void service(final ServletRequest req, final ServletResponse res) throws ServletException, IOException {
    PrintWriter writer = res.getWriter();

    JSONObject jsonObject = new JSONObject();
    jsonObject.put("remoteAddr", req.getRemoteAddr());
    jsonObject.put("remoteHost", req.getRemoteHost());
    jsonObject.put("remotePort", req.getRemotePort());
    jsonObject.put("serverName", req.getServerName());
    jsonObject.put("serverPort", req.getServerPort());
    jsonObject.put("protocol", req.getProtocol());
    jsonObject.put("secure", req.isSecure());

    writer.write(jsonObject.toString());

}

From source file:cn.vlabs.duckling.vwb.PromitionLogFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    if (!(request instanceof HttpServletRequest)) {
        chain.doFilter(request, response);
        return;// w  ww .j a  va2s.  co  m
    }

    HttpServletRequest httpRequest = (HttpServletRequest) request;
    String serverName = request.getServerName();
    if (!StringUtils.equals(serverName,
            VWBContext.getContainer().getDomainService().getSiteDefaultDomain(ADMIN_SITE_ID))) {
        chain.doFilter(request, response);
        return;
    }

    HttpSession session = httpRequest.getSession();
    String promotionHost = (String) session.getAttribute(PROMOTION_HOST);

    String requestUrl = httpRequest.getRequestURI();
    if (StringUtils.equals(requestUrl, createUrl)) {
        logCreateJson(promotionHost, requestUrl);
    }

    if (StringUtils.isNotBlank(promotionHost)) {
        chain.doFilter(request, response);
        return;
    }

    String paramRef = request.getParameter(PARAM_REF);

    if (StringUtils.isNotBlank(paramRef)) {
        session.setAttribute(PROMOTION_HOST, paramRef);
        logAccessJson("", paramRef, paramRef, requestUrl);
        chain.doFilter(request, response);
        return;
    }

    String referer = httpRequest.getHeader("Referer");
    String referHost = getRefererHost(referer);
    if (StringUtils.isNotBlank(referHost)) {
        session.setAttribute(PROMOTION_HOST, getRefererNameByHost(referHost));
        logAccessJson(referer, referHost, getRefererNameByHost(referHost), requestUrl);
        chain.doFilter(request, response);
    }

}

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;/*w ww  . j ava2s  .  c  om*/
        } 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;// w w  w  .  ja  v  a 2s. 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.sakaiproject.nakamura.http.cache.StaticContentResponseCacheImpl.java

/**
 * {@inheritDoc}/* w w w .ja v a 2  s  .  c o  m*/
 *
 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
 *      javax.servlet.ServletResponse, javax.servlet.FilterChain)
 */
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    HttpServletRequest srequest = (HttpServletRequest) request;
    HttpServletResponse sresponse = (HttpServletResponse) response;

    if (bypassForLocalhost
            && ("localhost".equals(request.getServerName()) || "127.0.0.1".equals(request.getServerName()))) {
        // bypass for developer convenience
        chain.doFilter(request, response);
        return;
    }

    String path = srequest.getPathInfo();

    if (HttpConstants.METHOD_GET.equals(srequest.getMethod())) {
        if (!responseWasFiltered(srequest, sresponse, chain, getCacheConfig(path))) {
            chain.doFilter(request, response);
        }
    } else {
        chain.doFilter(request, response);
    }
}

From source file:org.acegisecurity.ui.webapp.AuthenticationProcessingFilterEntryPoint.java

public void commence(ServletRequest request, ServletResponse response, AuthenticationException authException)
        throws IOException, ServletException {
    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse resp = (HttpServletResponse) response;
    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;//www .  j  a va2s.  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;
            }
        }

    }

    String loginForm = determineUrlToUseForThisRequest(req, resp, authException);

    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: " + loginForm);
            }

            RequestDispatcher dispatcher = req.getRequestDispatcher(loginForm);

            dispatcher.forward(request, response);

            return;

        }

    } else {

        if (doForceHttps) {

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

        } else {

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

        }
    }

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

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

From source file:pt.webdetails.cdv.api.CdvApi.java

private String getRoot(ServletRequest wrapper) {
    return wrapper.getScheme() + "://" + wrapper.getServerName() + ":" + wrapper.getServerPort();
}

From source file:pt.webdetails.cfr.CfrContentGenerator.java

private String getRoot() {

    ServletRequest wrapper = getRequest();
    String root = wrapper.getScheme() + "://" + wrapper.getServerName() + ":" + wrapper.getServerPort();

    return root;/*from   ww w .j a  va2s  .c  om*/
}

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);// www  .  ja  v a 2s.  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:pt.webdetails.cfr.CfrContentGenerator.java

@Exposed(accessLevel = AccessLevel.PUBLIC)
public void home(OutputStream out) throws IOException {

    IParameterProvider requestParams = getRequestParameters();
    // IParameterProvider pathParams = getPathParameters();
    ServletRequest wrapper = getRequest();
    String root = wrapper.getScheme() + "://" + wrapper.getServerName() + ":" + wrapper.getServerPort();

    Map<String, Object> params = new HashMap<String, Object>();
    params.put("solution", "system");
    params.put("path", "cfr/presentation/");
    params.put("file", "cfr.wcdf");
    params.put("absolute", "false");
    params.put("inferScheme", "false");
    params.put("root", root);

    // add request parameters
    PluginUtils.copyParametersFromProvider(params, WrapperUtils.wrapParamProvider(requestParams));

    if (requestParams.hasParameter("mode")
            && requestParams.getStringParameter("mode", "Render").equals("edit")) {

        // Send this to CDE

        redirectToCdeEditor(out, params);
        return;/*from w  w w .jav a2s  .  c  o m*/
    }

    InterPluginCall pluginCall = new InterPluginCall(InterPluginCall.CDE, "Render", params);
    pluginCall.setResponse(getResponse());
    pluginCall.setRequest(getRequest());
    pluginCall.setOutputStream(out);
    pluginCall.run();

}