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:controllers.UrlController.java

@RequestMapping(value = { "/*", "/*/*", "/*/*/*" })
public void AnyURL(@PathVariable String link, ModelMap model, HttpServletRequest request,
        HttpServletResponse response) throws SQLException, IOException, ServletException {
    String url1 = request.getRequestURI();
    String url2 = request.getScheme() + "://" + // "http" + "://
            request.getServerName() + // "myhost"
            ":" + request.getServerPort() + // ":" + "8080"
            request.getRequestURI() + // "/people"
            (request.getQueryString() != null ? "?" + request.getQueryString() : "");
    //request.getRequestDispatcher("").forward(request, response);
    // response.sendRedirect("");
}

From source file:org.imsglobal.lti2.LTI2Servlet.java

public String getServiceURL(HttpServletRequest request) {
    String scheme = request.getScheme(); // http
    String serverName = request.getServerName(); // localhost
    int serverPort = request.getServerPort(); // 80
    String contextPath = request.getContextPath(); // /imsblis
    String servletPath = request.getServletPath(); // /ltitest
    String url = scheme + "://" + serverName + ":" + serverPort + contextPath + servletPath + "/";
    return url;//from w ww.  j a  va  2 s.  co m
}

From source file:org.hubiquitus.hubotsdk.adapters.HHttpAdapterInbox.java

@Override
public void process(Exchange exchange) throws Exception {
    Message in = exchange.getIn();/*from   w  w  w  .  j  a  va2s. c om*/

    HttpServletRequest request = exchange.getIn().getBody(HttpServletRequest.class);

    //Gather data to send through an hmessage
    byte[] rawBody = (byte[]) in.getBody(byte[].class);
    Map<String, Object> headers = in.getHeaders();
    Map<String, DataHandler> attachments = in.getAttachments();

    String method = request.getMethod().toLowerCase();
    String queryArgs = request.getQueryString();
    String queryPath = request.getRequestURI();
    String serverName = request.getServerName();
    Integer serverPort = request.getServerPort();

    //create message to send
    HMessage message = new HMessage();
    message.setAuthor(this.actor);
    if (headers != null) {
        JSONObject jsonHeaders = new JSONObject();
        for (String key : headers.keySet()) {
            Object header = headers.get(key);
            String value = null;
            if (header != null) {
                value = header.toString();
            }
            jsonHeaders.put(key, value);
        }
    }

    message.setPublished(new DateTime());

    message.setType("hHttpData");
    //create payload
    HHttpData httpData = new HHttpData();
    httpData.setMethod(method);
    httpData.setQueryArgs(queryArgs);
    httpData.setQueryPath(queryPath);
    httpData.setServerName(serverName);
    httpData.setServerPort(serverPort);
    httpData.setRawBody(rawBody);

    //create attachements
    JSONObject hattachements = new JSONObject();
    for (String key : attachments.keySet()) {
        DataHandler attachement = attachments.get(key);
        if (attachement != null) {
            HHttpAttachement hattachement = new HHttpAttachement();
            hattachement.setContentType(attachement.getContentType());
            hattachement.setName(attachement.getName());

            //read attachement
            byte[] buffer = new byte[8 * 1024];
            InputStream input = attachement.getInputStream();

            ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
            int bytesRead;
            while ((bytesRead = input.read(buffer)) != -1) {
                byteOutputStream.write(buffer, 0, bytesRead);
            }
            hattachement.setData(byteOutputStream.toByteArray());
            try {
                hattachements.put(key, hattachement);
            } catch (JSONException e) {
                logger.debug(e.toString());
            }
        } else {
            //if attachment is null, do not put the key in hattachments.
        }
    }

    httpData.setAttachments(hattachements);
    message.setPayload(httpData);

    //finally send message to actor
    this.put(message);
}

From source file:cn.itganhuo.app.web.controller.UserController.java

/**
 * ???/*from   w  w  w .  java2  s .c  o  m*/
 *
 * @return ???
 * @version 0.0.1-SNAPSHOT
 * @author ?-?
 */
@RequiresAuthentication
@RequestMapping(value = "/upload", method = RequestMethod.GET)
public String refurlUpload(Model model, HttpServletRequest request) {
    model.addAttribute("path",
            "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath());
    return "user/upload";
}

From source file:at.gv.egiz.pdfas.web.helper.PdfAsHelper.java

private static String generateURL(HttpServletRequest request, HttpServletResponse response, String Servlet) {
    HttpSession session = request.getSession();
    String publicURL = WebConfiguration.getPublicURL();
    String dataURL = null;/* www.j a  v a  2 s.co m*/
    if (publicURL != null) {
        dataURL = publicURL + Servlet + ";jsessionid=" + session.getId();
    } else {
        if ((request.getScheme().equals("http") && request.getServerPort() == 80)
                || (request.getScheme().equals("https") && request.getServerPort() == 443)) {
            dataURL = request.getScheme() + "://" + request.getServerName() + request.getContextPath() + Servlet
                    + ";jsessionid=" + session.getId();
        } else {
            dataURL = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
                    + request.getContextPath() + Servlet + ";jsessionid=" + session.getId();
        }
    }
    logger.debug("Generated URL: " + dataURL);
    return dataURL;
}

From source file:org.sample.link.TestServlet.java

/**
 * Processes requests for both HTTP//from   w  w  w . ja va  2s .  c om
 * <code>GET</code> and
 * <code>POST</code> methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Servlet TestServlet</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h1>Servlet TestServlet at " + request.getContextPath() + "</h1>");
    Client client = ClientBuilder.newClient();
    //        client.register(LoggingFilter.class);
    WebTarget target = client.target("http://" + request.getServerName() + ":" + request.getServerPort()
            + request.getContextPath() + "/webresources/fruits");
    String result = target.request().get(String.class);

    Response r = target.path("link").request().get();
    out.println("Received response: " + result);
    out.println("</body>");
    out.println("</html>");
}

From source file:org.sample.moxy.TestServlet.java

/**
 * Processes requests for both HTTP/*from   www.  j a  v  a 2s  . com*/
 * <code>GET</code> and
 * <code>POST</code> methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Servlet TestServlet</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h1>Servlet TestServlet at " + request.getContextPath() + "</h1>");
    Client client = ClientBuilder.newClient();
    client.register(MoxyJsonFeature.class);

    WebTarget target = client.target("http://" + request.getServerName() + ":" + request.getServerPort()
            + request.getContextPath() + "/webresources/endpoint");
    System.out.println("POST request");
    MyObject mo = target.request().post(Entity.entity(new MyObject("Duke", 18), MediaType.APPLICATION_JSON),
            MyObject.class);
    out.println("Received response: " + mo.getName() + ", " + mo.getAge() + "<br><br>");

    out.println("Check server.log for client/server interceptor output.");
    out.println("</body>");
    out.println("</html>");
}

From source file:org.sample.filter.TestServlet.java

/**
 * Processes requests for both HTTP//from w  ww. ja v a2 s. co m
 * <code>GET</code> and
 * <code>POST</code> methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Servlet TestServlet</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h1>Servlet TestServlet at " + request.getContextPath() + "</h1>");
    Client client = ClientBuilder.newClient();
    client.register(ClientLoggingFilter.class);
    WebTarget target = client.target("http://" + request.getServerName() + ":" + request.getServerPort()
            + request.getContextPath() + "/webresources/fruits");
    out.println(target.getUri() + "<br><br>");
    out.println("GET request" + "<br><br>");
    String result = target.request().get(String.class);
    out.println("Received response (GET): " + result + "<br><br>");

    System.out.println("**** POST request");
    result = target.request().post(Entity.text("1"), String.class);
    out.println("Received response (POST): " + result + "<br><br>");

    out.println("Received response: " + result + "<br><br>");
    out.println("Check server.log for client/server filter output.");
    out.println("</body>");
    out.println("</html>");
}

From source file:fr.xebia.cocktail.CocktailRssView.java

@Override
protected List<Item> buildFeedItems(Map<String, Object> model, final HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    Function<Cocktail, Item> cocktailToRssItem = new Function<Cocktail, Item>() {
        @Override/*from   w w w. j  a  v a 2  s .co  m*/
        public Item apply(@Nullable Cocktail cocktail) {
            if (cocktail == null) {
                return null;
            }

            Item item = new Item();
            item.setTitle(cocktail.getName());
            Content content = new Content();
            content.setValue(cocktail.getInstructionsAsHtml());
            content.setType(Content.HTML);
            item.setContent(content);
            item.setLink(request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
                    + request.getContextPath() + "/cocktail/" + cocktail.getId());
            return item;
        }
    };

    return Lists.newArrayList(Iterables.transform(cocktails, cocktailToRssItem));
}

From source file:org.sample.readerwriter.TestServlet.java

/**
 * Processes requests for both HTTP//from   w ww  .  ja  va 2 s .c o m
 * <code>GET</code> and
 * <code>POST</code> methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Servlet TestServlet</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h1>Servlet TestServlet at " + request.getContextPath() + "</h1>");
    Client client = ClientBuilder.newClient();
    client.register(MyReader.class).register(MyWriter.class);

    WebTarget target = client.target("http://" + request.getServerName() + ":" + request.getServerPort()
            + request.getContextPath() + "/webresources/fruits");
    System.out.println("POST request");
    String fruit = target.request().post(Entity.entity(new MyObject(1), MyObject.MIME_TYPE), String.class);
    out.println("Received response: " + fruit + "<br><br>");
    fruit = target.path("fruitInt").request().post(Entity.text("1"), String.class);
    out.println("Received response: " + fruit + "<br><br>");

    out.println("Check server.log for client/server interceptor output.");
    out.println("</body>");
    out.println("</html>");
}