Example usage for javax.servlet.http HttpServletRequest getServerName

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

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest 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:com.acc.storefront.interceptors.beforeview.AnalyticsPropertiesBeforeViewHandler.java

@Override
public void beforeView(final HttpServletRequest request, final HttpServletResponse response,
        final ModelAndView modelAndView) {
    // Create the change listener and register it to listen when the config properties are changed in the platform
    if (cfgChangeListener == null) {
        registerConfigChangeLister();//  www  .j  a  v  a  2s.c  o  m
    }
    final String serverName = request.getServerName();
    // Add config properties for google analytics
    addHostProperty(serverName, modelAndView, ThirdPartyConstants.Google.ANALYTICS_TRACKING_ID,
            ANALYTICS_TRACKING_ID);
    // Add config properties for jirafe analytics
    addHostProperty(serverName, modelAndView, ThirdPartyConstants.Jirafe.API_URL, JIRAFE_API_URL);
    addHostProperty(serverName, modelAndView, ThirdPartyConstants.Jirafe.API_TOKEN, JIRAFE_API_TOKEN);
    addHostProperty(serverName, modelAndView, ThirdPartyConstants.Jirafe.APPLICATION_ID, JIRAFE_APPLICATION_ID);
    addHostProperty(serverName, modelAndView, ThirdPartyConstants.Jirafe.VERSION, JIRAFE_VERSION);
    addHostProperty(serverName, modelAndView, ThirdPartyConstants.Jirafe.DATA_URL, JIRAFE_DATA_URL);

    // Lookup a currency specific jirafe site id first, and only if it is missing fallback to the default site id
    final String currencyIso = commonI18NService.getCurrentCurrency().getIsocode().toLowerCase();
    final String currSpecKey = ThirdPartyConstants.Jirafe.SITE_ID + "." + currencyIso;
    final String nonSpecKey = ThirdPartyConstants.Jirafe.SITE_ID;
    if (jirafeMapCache.get(currSpecKey) == null) {
        final String currencySpecificJirafeSiteId = hostConfigService.getProperty(currSpecKey, serverName);
        jirafeMapCache.put(currSpecKey, currencySpecificJirafeSiteId);
    }
    if (jirafeMapCache.get(currSpecKey) != null
            && org.apache.commons.lang.StringUtils.isNotBlank(jirafeMapCache.get(currSpecKey).toString())) {
        modelAndView.addObject(JIRAFE_SITE_ID, jirafeMapCache.get(currSpecKey));
    } else {
        // Fallback to the non-currency specific value
        if (jirafeMapCache.get(nonSpecKey) == null) {
            final String jirafeSiteId = hostConfigService.getProperty(ThirdPartyConstants.Jirafe.SITE_ID,
                    serverName);
            jirafeMapCache.put(nonSpecKey, jirafeSiteId);
        }
        modelAndView.addObject(JIRAFE_SITE_ID, jirafeMapCache.get(nonSpecKey));
    }
}

From source file:com.oscgc.security.saml.idp.web.contoller.MetadataController.java

protected String getBaseURL(HttpServletRequest request) {

    StringBuffer sb = new StringBuffer();
    sb.append(request.getScheme()).append("://").append(request.getServerName()).append(":")
            .append(request.getServerPort());
    sb.append(request.getContextPath());

    String baseURL = sb.toString();
    log.debug("Base URL {}", baseURL);
    return baseURL;

}

From source file:company.gonapps.loghut.controller.LoginController.java

@RequestMapping(value = "/login.do", method = RequestMethod.POST)
public String login(HttpServletRequest request, String id, String password, String request_path)
        throws Exception {
    if (id.equals(getSettingDao().getSetting("admin.id"))
            && password.equals(getSettingDao().getSetting("admin.password"))) {

        request.getSession().setMaxInactiveInterval(new Integer(getSettingDao().getSetting("session.timeout")));
        if (request_path != null)
            return "redirect:" + request.getScheme() + "://" + request.getServerName()
                    + getSettingDao().getSetting("admin.url") + request_path;
        return "redirect:" + request.getScheme() + "://" + request.getServerName()
                + getSettingDao().getSetting("admin.url");
    }//from   www  .j  a v a2  s. co  m
    return "redirect:" + request.getScheme() + "://" + request.getServerName()
            + getSettingDao().getSetting("admin.url") + "/login_form.do";
}

From source file:org.edeoliveira.oauth2.dropwizard.oauth2.auth.OAuth2Resource.java

@GET
@Timed/*from   w w w  .j ava  2 s.  com*/
@Path(value = "/logout")
public Response logout(@Context final HttpServletRequest request) {
    // invalidate cookie if exists
    ResponseBuilder reply = Response.ok();

    for (Cookie c : request.getCookies()) {
        if (OAuth2AuthFilter.AUTH_COOKIE_NAME.equals(c.getName())) {
            reply.cookie(new NewCookie(OAuth2AuthFilter.AUTH_COOKIE_NAME, null, "/", request.getServerName(),
                    null, 0, true));
            break;
        }
    }

    return reply.build();
}

From source file:cn.guoyukun.spring.web.interceptor.SetCommonDataInterceptor.java

private String getBasePath(HttpServletRequest req) {
    StringBuffer baseUrl = new StringBuffer();
    String scheme = req.getScheme();
    int port = req.getServerPort();

    //String      servletPath = req.getServletPath ();
    //String      pathInfo = req.getPathInfo ();

    baseUrl.append(scheme); // http, https
    baseUrl.append("://");
    baseUrl.append(req.getServerName());
    if ((scheme.equals("http") && port != 80) || (scheme.equals("https") && port != 443)) {
        baseUrl.append(':');
        baseUrl.append(req.getServerPort());
    }//  w  w w. j  ava2 s  . c  o m
    return baseUrl.toString();
}

From source file:org.chtijbug.drools.platform.web.filter.DynamicBaseFilter.java

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
        FilterChain filterChain) throws ServletException, IOException {
    logger.debug(">> doFilterInternal()");
    try {/*w w  w . j  ava2  s  . co m*/
        PrintWriter out = response.getWriter();
        CharResponseWrapper wrapper = new CharResponseWrapper(response);
        filterChain.doFilter(request, wrapper);

        String baseHref = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
                + request.getContextPath() + "/";
        logger.debug("About to replace the base href element with {}", baseHref);
        String modifiedHtml = baseElementPattern.matcher(wrapper.toString())
                .replaceAll("<base href=\"" + baseHref + "\"");
        logger.debug("Modified HTML content : {}", modifiedHtml);
        // Write our modified text to the real response
        response.setContentLength(modifiedHtml.getBytes().length);
        out.write(modifiedHtml);
        out.close();
    } catch (Throwable t) {
        logger.error(t.getMessage());
    } finally {
        logger.debug("<< doFilterInternal()");
    }
}

From source file:com.cloudbees.demo.beesshop.web.ProductController.java

@RequestMapping(value = "/product/{id}/mail", method = RequestMethod.POST)
public String sendEmail(@PathVariable long id, @RequestParam("recipientEmail") String recipientEmail,
        HttpServletRequest request, RedirectAttributes redirectAttributes) {
    Product product = productRepository.get(id);
    if (product == null) {
        throw new ProductNotFoundException(id);
    }//from  ww w . j  a  v  a 2s.  co m
    try {
        String productPageUrl = request.getScheme() + "://" + request.getServerName() + ":"
                + request.getServerPort() + request.getContextPath() + "/product/" + id;
        mailService.sendProductEmail(product, recipientEmail, productPageUrl);

        redirectAttributes.addFlashAttribute("mailSuccessMessage", "Email sent!");

    } catch (MessagingException e) {
        redirectAttributes.addFlashAttribute("mailFailureMessage", "Failure sending email: " + e.getMessage());
        logger.warn("Failure sending message to " + recipientEmail, e);
    }
    return "redirect:/product/" + product.getId();
}

From source file:com.cpjit.swagger4j.support.internal.DefaultApiViewWriter.java

@Deprecated
@Override/*from ww  w. j  a v  a  2  s  .c  o  m*/
public void writeIndex(HttpServletRequest request, HttpServletResponse response, String lang, Properties props)
        throws IOException {
    Map<String, Object> root = new HashMap<String, Object>();
    root.put("lang", lang);
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
    root.put("basePath", basePath);
    String host = request.getServerName() + ":" + request.getServerPort() + path;
    String suffix = props.getProperty("suffix");
    if (StringUtils.isBlank(suffix)) {
        suffix = "";
    }
    root.put("getApisUrl", "http://" + host + "/api" + suffix);
    root.put("apiDescription", props.getProperty("apiDescription"));
    root.put("apiTitle", props.getProperty("apiTitle"));
    root.put("apiVersion", props.getProperty("apiVersion"));
    root.put("suffix", suffix);
    Template template = FreemarkerUtils.getTemplate(getTemplateName());
    response.setContentType("text/html;charset=utf-8");
    Writer out = response.getWriter();
    try {
        template.process(root, out);
    } catch (TemplateException e) {
        throw new IOException(e);
    }
    out.flush();
    out.close();
}

From source file:dk.teachus.frontend.TeachUsApplication.java

public String getServerUrl() {
    String serverUrl = getConfiguration().getConfiguration(ApplicationConfiguration.SERVER_URL);

    /*//w  w  w . j  a  v a 2s .  c om
     * If the server URL is empty, then the administrator have misconfigured the system (forgot to set the
     * server URL in the settings). We will get the server URL from the current server, but we will also
     * warn the administrator by adding an entry to the log.
     */
    if (Strings.isEmpty(serverUrl)) {
        log.error("No server url is set for the system. It's very important that you set it."); //$NON-NLS-1$

        RequestCycle cycle = RequestCycle.get();
        ServletWebRequest request = (ServletWebRequest) cycle.getRequest();
        HttpServletRequest httpServletRequest = request.getContainerRequest();

        StringBuilder b = new StringBuilder();
        b.append(httpServletRequest.getScheme()).append("://"); //$NON-NLS-1$
        b.append(httpServletRequest.getServerName());
        if (httpServletRequest.getServerPort() != 80 && httpServletRequest.getServerPort() != 443) {
            b.append(":").append(httpServletRequest.getServerPort()); //$NON-NLS-1$
        }

        serverUrl = b.toString();
    }

    return serverUrl;
}

From source file:com.sun.socialsite.web.rest.servlets.ProxyServlet.java

private URL getURL(HttpServletRequest req, String uriString) throws MalformedURLException {
    String s = uriString.toLowerCase();
    if ((s.startsWith("http://")) || (s.startsWith("https://"))) {
        return new URL(uriString);
    } else if (s.startsWith("/")) {
        return new URL(req.getScheme(), req.getServerName(), req.getServerPort(), uriString);
    } else {/*  w  w w. j av  a  2  s.c  om*/
        return new URL(req.getScheme(), req.getServerName(), req.getServerPort(),
                (req.getContextPath() + "/" + uriString));
    }
}