Example usage for javax.servlet.http HttpServletRequest getServerPort

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

Introduction

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

Prototype

public int getServerPort();

Source Link

Document

Returns the port number to which the request was sent.

Usage

From source file:info.magnolia.cms.util.RequestDispatchUtil.java

/**
 * Returns true if processing took place, even if it fails.
 *///from w  w w  . jav a 2s.c  o m
public static boolean dispatch(String targetUri, HttpServletRequest request, HttpServletResponse response) {

    if (targetUri == null) {
        return false;
    }

    if (targetUri.startsWith(REDIRECT_PREFIX)) {
        String redirectUrl = StringUtils.substringAfter(targetUri, REDIRECT_PREFIX);
        try {

            if (isInternal(redirectUrl)) {
                redirectUrl = request.getContextPath() + redirectUrl;
            }

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

        } catch (IOException e) {
            log.error("Failed to redirect to {}:{}", targetUri, e.getMessage());
        }
        return true;
    }

    if (targetUri.startsWith(PERMANENT_PREFIX)) {
        String permanentUrl = StringUtils.substringAfter(targetUri, PERMANENT_PREFIX);
        try {

            if (isInternal(permanentUrl)) {
                if (isUsingStandardPort(request)) {
                    permanentUrl = new URL(request.getScheme(), request.getServerName(),
                            request.getContextPath() + permanentUrl).toExternalForm();
                } else {
                    permanentUrl = new URL(request.getScheme(), request.getServerName(),
                            request.getServerPort(), request.getContextPath() + permanentUrl).toExternalForm();
                }
            }

            response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
            response.setHeader("Location", permanentUrl);

        } catch (MalformedURLException e) {
            log.error("Failed to create permanent url to redirect to {}:{}", targetUri, e.getMessage());
        }
        return true;
    }

    if (targetUri.startsWith(FORWARD_PREFIX)) {
        String forwardUrl = StringUtils.substringAfter(targetUri, FORWARD_PREFIX);
        try {
            request.getRequestDispatcher(forwardUrl).forward(request, response);
        } catch (Exception e) {
            log.error("Failed to forward to {} - {}:{}",
                    new Object[] { forwardUrl, ClassUtils.getShortClassName(e.getClass()), e.getMessage() });
        }
        return true;
    }

    return false;
}

From source file:org.apache.qpid.server.management.plugin.servlet.rest.AbstractServlet.java

protected SocketAddress getSocketAddress(HttpServletRequest request) {
    return InetSocketAddress.createUnresolved(request.getServerName(), request.getServerPort());
}

From source file:edu.indiana.d2i.sloan.ui.LoginSuccessAction.java

private String getServerContext() {
    HttpServletRequest request = getServletRequest();
    final StringBuilder serverPath = new StringBuilder();
    serverPath.append(request.getScheme() + "://");
    serverPath.append(request.getServerName());
    if (request.getServerPort() != 80) {
        serverPath.append(":" + request.getServerPort());
    }/*from  w  w  w.  ja va  2 s.  c o m*/
    serverPath.append(request.getContextPath());
    return serverPath.toString();
}

From source file:it.cnr.icar.eric.client.ui.thin.RegistryBrowser.java

private static String getRegistryUrl() {
    ProviderProperties props = ProviderProperties.getInstance();
    String registryUrl = props.getProperty("jaxr-ebxml.soap.url");

    if (registryUrl == null || registryUrl.length() == 0) {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext()
                .getRequest();/*from   w w w .  j  av a  2 s  .c  o m*/
        final String SUFFIX = "/registry/soap";
        try {
            URL url = new URL(request.getScheme(), request.getServerName(), request.getServerPort(),
                    request.getContextPath() + SUFFIX);
            registryUrl = url.toString();
        } catch (MalformedURLException e) {
            // This should not happen
            log.error(WebUIResourceBundle.getInstance().getString("message.FailedToDefineRegistrySoapURL"), e);
        }
    }
    props.put("javax.xml.registry.queryManagerURL", registryUrl);
    return registryUrl;
}

From source file:seava.j4e.web.controller.ui.extjs.UiExtjsFrameController.java

/**
 * Helper method to create , configure and return an DependencyLoader
 * instance/*from   w w  w.  j a  va  2 s . com*/
 * 
 * @return
 */
private DependencyLoader getDependencyLoader(HttpServletRequest request) {
    String protocol = "http";
    if (request.getProtocol().startsWith("HTTPS")) {
        protocol = "https";
    }
    String host = protocol + "://" + request.getServerName();
    if (request.getServerPort() != 80) {
        host += ":" + request.getServerPort();
    }
    host += "/";
    if (logger.isDebugEnabled()) {
        logger.debug("Get dependency loader for host: " + host + ", modules url: "
                + getUiExtjsSettings().getUrlModules());
    }
    DependencyLoader loader = new DependencyLoader(host);
    loader.setUrlUiExtjsModules(getUiExtjsSettings().getUrlModules());
    loader.setModuleUseBundle(getUiExtjsSettings().isModuleUseBundle());
    loader.setUrlUiExtjsModuleSubpath(getUiExtjsSettings().getModuleSubpath());
    return loader;
}

From source file:uk.ac.ebi.intact.editor.controller.misc.MyNotesController.java

public MyNotesController() {
    this.queryMacros = new ArrayList<QueryMacro>();

    FacesContext context = FacesContext.getCurrentInstance();
    HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();

    this.absoluteContextPath = request.getScheme() + "://" + request.getServerName() + ":"
            + request.getServerPort() + request.getContextPath();
}

From source file:de.ifgi.lodum.sparqlfly.SparqlFly.java

/**
 * Returns the base URL of the server //from ww w .ja  v  a 2  s  . c  o  m
 * 
 * @param request
 * @return
 */
private String getRequestBaseURL(HttpServletRequest request) {
    return ("http://" + request.getServerName() + ":" + request.getServerPort());
}

From source file:com.biblio.web.rest.UserResource.java

/**
 * POST /account/reset_password/init : Send an e-mail to reset the password
 * of the user//from  w  ww  .  ja  v  a 2  s . c om
 *
 * @param mail the mail of the user
 * @param request the HTTP request
 * @return the ResponseEntity with status 200 (OK) if the e-mail was sent,
 * or status 400 (Bad Request) if the e-mail address is not registered
 */
@RequestMapping(value = "/account/reset_password/init", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE)

public ResponseEntity<?> requestPasswordReset(@RequestBody String mail, HttpServletRequest request) {
    return userService.requestPasswordReset(mail).map(user -> {
        String baseUrl = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
                + request.getContextPath();
        // mailService.sendPasswordResetMail(user, baseUrl);
        return new ResponseEntity<>("e-mail was sent", HttpStatus.OK);
    }).orElse(new ResponseEntity<>("e-mail address not registered", HttpStatus.BAD_REQUEST));
}

From source file:org.onebusaway.webapp.actions.rss.StopProblemReportsAction.java

@Override
public String execute() {

    AgencyBean agency = _transitDataService.getAgency(_agencyId);

    if (agency == null)
        return INPUT;

    Calendar c = Calendar.getInstance();
    long timeTo = c.getTimeInMillis();
    c.add(Calendar.DAY_OF_WEEK, -_days);
    long timeFrom = c.getTimeInMillis();

    StopProblemReportQueryBean query = new StopProblemReportQueryBean();
    query.setAgencyId(_agencyId);//ww w .j a  va2 s  .com
    query.setTimeFrom(timeFrom);
    query.setTimeTo(timeTo);
    if (_status != null)
        query.setStatus(EProblemReportStatus.valueOf(_status));

    ListBean<StopProblemReportBean> result = _transitDataService.getStopProblemReports(query);
    List<StopProblemReportBean> reports = result.getList();

    _feed = new SyndFeedImpl();

    StringBuilder title = new StringBuilder();
    title.append(getText("rss.OneBusAwayStopProblemReports"));
    title.append(" - ");
    title.append(agency.getName());
    title.append(" - ");
    title.append(getText("rss.LastXDays", Arrays.asList((Object) _days)));

    HttpServletRequest request = ServletActionContext.getRequest();

    StringBuilder b = new StringBuilder();
    b.append("http://");
    b.append(request.getServerName());
    if (request.getServerPort() != 80)
        b.append(":").append(request.getServerPort());
    if (request.getContextPath() != null)
        b.append(request.getContextPath());
    String baseUrl = b.toString();

    _feed.setTitle(title.toString());
    _feed.setLink(baseUrl);
    _feed.setDescription(
            getText("rss.UserSubmittedStopProblemReports", Arrays.asList((Object) agency.getName(), _days)));

    List<SyndEntry> entries = new ArrayList<SyndEntry>();
    _feed.setEntries(entries);

    for (StopProblemReportBean report : reports) {

        StopBean stop = report.getStop();
        SyndEntry entry = new SyndEntryImpl();

        StringBuilder entryTitle = new StringBuilder();
        if (stop == null) {
            entryTitle.append("stopId=");
            entryTitle.append(report.getStopId());
            entryTitle.append(" (?)");
        } else {
            entryTitle.append(getText("StopNum", Arrays.asList(stop.getCode())));
            entryTitle.append(" - ");
            entryTitle.append(stop.getName());
            if (stop.getDirection() != null)
                entryTitle.append(" - ").append(getText("bound", Arrays.asList(stop.getDirection())));
        }

        StringBuilder entryUrl = new StringBuilder();
        entryUrl.append(baseUrl);
        entryUrl.append("/admin/problems/stop-problem-reports!edit.action?stopId=");
        entryUrl.append(report.getStopId());
        entryUrl.append("&id=");
        entryUrl.append(report.getId());

        StringBuilder entryDesc = new StringBuilder();
        entryDesc.append(getText("Data"));
        entryDesc.append(": ");
        entryDesc.append(report.getData());
        entryDesc.append("<br/>");

        if (report.getUserComment() != null) {
            entryDesc.append(getText("Comment"));
            entryDesc.append(": ");
            entryDesc.append(report.getUserComment());
            entryDesc.append("<br/>");
        }

        if (report.getStatus() != null) {
            entryDesc.append(getText("Status"));
            entryDesc.append(": ");
            entryDesc.append(report.getStatus());
            entryDesc.append("<br/>");
        }

        entry = new SyndEntryImpl();
        entry.setTitle(entryTitle.toString());
        entry.setLink(entryUrl.toString());
        entry.setPublishedDate(new Date(report.getTime()));

        SyndContent description = new SyndContentImpl();
        description.setType("text/html");
        description.setValue(entryDesc.toString());
        entry.setDescription(description);
        entries.add(entry);
    }

    return SUCCESS;
}

From source file:org.apache.struts.faces.renderer.BaseRenderer.java

/**
 * <p>Return an absolute URI for the current page suitable for use
 * in a servlet environment.</p>/*from w w  w  . jav  a  2 s .  c  o  m*/
 *
 * @param context <code>FacesContext</code> for the current request
 */
protected String servletUri(FacesContext context) {

    HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
    StringBuffer sb = new StringBuffer(request.getScheme());
    sb.append("://");
    sb.append(request.getServerName());
    if ("http".equals(request.getScheme()) && (80 == request.getServerPort())) {
        ;
    } else if ("https".equals(request.getScheme()) && (443 == request.getServerPort())) {
        ;
    } else {
        sb.append(":" + request.getServerPort());
    }
    sb.append(request.getContextPath());
    sb.append(context.getViewRoot().getViewId());
    return (sb.toString());

}