Java Code Examples for javax.servlet.http.HttpServletRequest.getLocalAddr()

The following are Jave code examples for showing how to use getLocalAddr() of the javax.servlet.http.HttpServletRequest class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: ramus   File: JNLPSeasonInternetServlet.java   View Source Code Vote up 7 votes
private void accept(HttpServletRequest req, HttpServletResponse resp) {
    try {
        String localAddr = req.getLocalAddr();
        Properties properties = EngineFactory.getPropeties();
        if (properties.getProperty("hostname") != null) {
            localAddr = properties.getProperty("hostname");
        }
        String path = "http://" + localAddr + ":" + req.getLocalPort()
                + req.getContextPath();
        InputStream is = getClass().getResourceAsStream(
                "/com/ramussoft/jnlp/season-internet-client.jnlp");
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int r;
        while ((r = is.read()) >= 0)
            out.write(r);
        String string = MessageFormat.format(new String(out.toByteArray(),
                "UTF8"), path);
        resp.setContentType("application/x-java-jnlp-file");
        OutputStream o = resp.getOutputStream();
        o.write(string.getBytes("UTF8"));
        o.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
 
Example 2
Project: DKJavaWebApiDemo   File: APIRequest.java   View Source Code Vote up 6 votes
public APIRequest(HttpServletRequest request) {

        this.request = request;

        header = new HashMap<String, Object>();
        Enumeration e = request.getHeaderNames();
        while (e.hasMoreElements()) {
            String name = (String) e.nextElement();
            Object value = request.getHeader(name);
            header.put(name, value);
        }

        params = getParamesMap(request.getParameterMap());
        url = request.getRequestURL().toString();
        uri = request.getRequestURI();
        remoteAddr = request.getRemoteAddr();
        remoteHost = request.getRemoteHost();
        remotePort = request.getRemotePort();
        contextPath = request.getContextPath();
        localAddr = request.getLocalAddr();
        characterEncoding = request.getCharacterEncoding();
        localName = request.getLocalName();
        localPort = request.getLocalPort();
        method = request.getMethod();
        queryString = request.getQueryString();
    }
 
Example 3
Project: ats-framework   File: MonitoringServiceImpl.java   View Source Code Vote up 6 votes
@POST
@Path( "stopMonitoring")
@Produces( MediaType.APPLICATION_JSON)
public Response stopMonitoring(
                                @Context HttpServletRequest request,
                                BasePojo basePojo ) {

    final String caller = getCaller(request, basePojo, false);
    ThreadsPerCaller.registerThread(caller);
    try {
        SessionData sd = getSessionData(request, basePojo);

        RestSystemMonitor restSystemMonitor = sd.getSystemMonitor();

        String agent = request.getLocalAddr() + ":" + request.getLocalPort();

        restSystemMonitor.stopMonitoring(agent);
    } catch (Exception e) {
        return Response.serverError().entity(new ErrorPojo(e)).build();
    } finally {
        ThreadsPerCaller.unregisterThread();
    }

    return Response.ok("{\"status\":\"monitoring stopped.\"}").build();
}
 
Example 4
Project: ramus   File: JNLPRMIServlet.java   View Source Code Vote up 6 votes
private void accept(HttpServletRequest req, HttpServletResponse resp) {
    try {
        String localAddr = req.getLocalAddr();
        Properties properties = EngineFactory.getPropeties();
        if (properties.getProperty("hostname") != null) {
            localAddr = properties.getProperty("hostname");
        }
        String path = "http://" + localAddr + ":" + req.getLocalPort() + req.getContextPath();
        InputStream is = getClass().getResourceAsStream(
                "/com/ramussoft/jnlp/ramus-rmi-client.jnlp");
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int r;
        while ((r = is.read()) >= 0)
            out.write(r);
        String string = MessageFormat.format(new String(out.toByteArray(),
                "UTF8"), path);
        resp.setContentType("application/x-java-jnlp-file");
        OutputStream o = resp.getOutputStream();
        o.write(string.getBytes("UTF8"));
        o.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
 
Example 5
Project: ramus   File: JNLPHTTPServlet.java   View Source Code Vote up 6 votes
private void accept(HttpServletRequest req, HttpServletResponse resp) {
    try {
        String localAddr = req.getLocalAddr();
        Properties properties = EngineFactory.getPropeties();
        if (properties.getProperty("hostname") != null) {
            localAddr = properties.getProperty("hostname");
        }
        String path = "http://" + localAddr + ":" + req.getLocalPort() + req.getContextPath();
        InputStream is = getClass().getResourceAsStream(
                "/com/ramussoft/jnlp/ramus-http-client.jnlp");
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int r;
        while ((r = is.read()) >= 0)
            out.write(r);
        String string = MessageFormat.format(new String(out.toByteArray(),
                "UTF8"), path);
        resp.setContentType("application/x-java-jnlp-file");
        OutputStream o = resp.getOutputStream();
        o.write(string.getBytes("UTF8"));
        o.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
 
Example 6
Project: ramus   File: JNLPInternetServlet.java   View Source Code Vote up 6 votes
private void accept(HttpServletRequest req, HttpServletResponse resp) {
    try {
        String localAddr = req.getLocalAddr();
        Properties properties = EngineFactory.getPropeties();
        if (properties.getProperty("hostname") != null) {
            localAddr = properties.getProperty("hostname");
        }
        String path = "http://" + localAddr + ":" + req.getLocalPort() + req.getContextPath();
        InputStream is = getClass().getResourceAsStream(
                "/com/ramussoft/jnlp/ramus-internet-client.jnlp");
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int r;
        while ((r = is.read()) >= 0)
            out.write(r);
        String string = MessageFormat.format(new String(out.toByteArray(),
                "UTF8"), path);
        resp.setContentType("application/x-java-jnlp-file");
        OutputStream o = resp.getOutputStream();
        o.write(string.getBytes("UTF8"));
        o.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
 
Example 7
Project: ramus   File: JNLPSeasonTcpServlet.java   View Source Code Vote up 6 votes
private void accept(HttpServletRequest req, HttpServletResponse resp) {
    try {
        String localAddr = req.getLocalAddr();
        Properties properties = EngineFactory.getPropeties();
        if (properties.getProperty("hostname") != null) {
            localAddr = properties.getProperty("hostname");
        }
        String path = "http://" + localAddr + ":" + req.getLocalPort()
                + req.getContextPath();
        InputStream is = getClass().getResourceAsStream(
                "/com/ramussoft/jnlp/season-tcp-client.jnlp");
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int r;
        while ((r = is.read()) >= 0)
            out.write(r);
        String string = MessageFormat.format(new String(out.toByteArray(),
                "UTF8"), path);
        resp.setContentType("application/x-java-jnlp-file");
        OutputStream o = resp.getOutputStream();
        o.write(string.getBytes("UTF8"));
        o.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
 
Example 8
Project: ramus   File: JNLPLocalServlet.java   View Source Code Vote up 6 votes
private void accept(HttpServletRequest req, HttpServletResponse resp) {
    try {
        String path = "http://" + req.getLocalAddr() + ":" + req.getLocalPort() + req.getContextPath();
        InputStream is = getClass().getResourceAsStream(
                "/com/ramussoft/jnlp/ramus-local.jnlp");
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int r;
        while ((r = is.read()) >= 0)
            out.write(r);
        String string = MessageFormat.format(new String(out.toByteArray(),
                "UTF8"), path);
        resp.setContentType("application/x-java-jnlp-file");
        OutputStream o = resp.getOutputStream();
        o.write(string.getBytes("UTF8"));
        o.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
 
Example 9
Project: ramus   File: JNLPServlet.java   View Source Code Vote up 6 votes
private void accept(HttpServletRequest req, HttpServletResponse resp) {
    try {
        String localAddr = req.getLocalAddr();
        Properties properties = EngineFactory.getPropeties();
        if (properties.getProperty("hostname") != null) {
            localAddr = properties.getProperty("hostname");
        }
        String path = "http://" + localAddr + ":" + req.getLocalPort() + req.getContextPath();
        InputStream is = getClass().getResourceAsStream(
                "/com/ramussoft/jnlp/ramus-client.jnlp");
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int r;
        while ((r = is.read()) >= 0)
            out.write(r);
        String string = MessageFormat.format(new String(out.toByteArray(),
                "UTF8"), path);
        resp.setContentType("application/x-java-jnlp-file");
        OutputStream o = resp.getOutputStream();
        o.write(string.getBytes("UTF8"));
        o.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
 
Example 10
Project: ats-framework   File: MonitoringServiceImpl.java   View Source Code Vote up 5 votes
/**
 * Initialize Monitoring context Must be called before calling any
 * scheduleXYZMonitoring REST method
 */
@POST
@Path( "initializeMonitoring")
@Consumes( MediaType.APPLICATION_JSON)
@Produces( MediaType.APPLICATION_JSON)
public Response initializeMonitoring(
                                      @Context HttpServletRequest request,
                                      BasePojo basePojo ) {

    final String caller = getCaller(request, basePojo, false);
    ThreadsPerCaller.registerThread(caller);

    try {

        SessionData sd = getSessionData(request, basePojo);

        RestSystemMonitor restSystemMonitor = sd.getSystemMonitor();

        String agent = request.getLocalAddr() + ":" + request.getLocalPort();

        restSystemMonitor.initializeMonitoringContext(agent);

        return Response.ok("{\"status\":\"monitoring context initialized.\"}").build();

    } catch (Exception e) {
        return Response.serverError().entity(new ErrorPojo(e)).build();
    } finally {
        ThreadsPerCaller.unregisterThread();
    }

}
 
Example 11
Project: ats-framework   File: MonitoringServiceImpl.java   View Source Code Vote up 5 votes
@POST
@Path( "scheduleSystemMonitoring")
@Consumes( MediaType.APPLICATION_JSON)
@Produces( MediaType.APPLICATION_JSON)
public Response scheduleSystemMonitoring(
                                          @Context HttpServletRequest request,
                                          ScheduleSystemMonitoringPojo monitoringPojo ) {

    final String caller = getCaller(request, monitoringPojo, false);
    ThreadsPerCaller.registerThread(caller);

    try {

        SessionData sd = getSessionData(request, monitoringPojo);

        RestSystemMonitor restSystemMonitor = sd.getSystemMonitor();

        String agent = request.getLocalAddr() + ":" + request.getLocalPort();

        Set<ReadingBean> readings = restSystemMonitor.scheduleSystemMonitoring(agent,
                                                                               monitoringPojo.getReadings());

        restSystemMonitor.setScheduledReadingTypes(readings);

        return Response.ok("{\"status\":\"scheduled system monitoring for readings '"
                           + Arrays.toString(monitoringPojo.getReadings()) + "'\"}")
                       .build();
    } catch (Exception e) {
        return Response.serverError().entity(new ErrorPojo(e)).build();
    } finally {
        ThreadsPerCaller.unregisterThread();
    }
}
 
Example 12
Project: ats-framework   File: MonitoringServiceImpl.java   View Source Code Vote up 5 votes
@POST
@Path( "scheduleMonitoring")
@Consumes( MediaType.APPLICATION_JSON)
@Produces( MediaType.APPLICATION_JSON)
public Response scheduleMonitoring(
                                    @Context HttpServletRequest request,
                                    ScheduleMonitoringPojo monitoringPojo ) {

    final String caller = getCaller(request, monitoringPojo, false);
    ThreadsPerCaller.registerThread(caller);

    try {

        SessionData sd = getSessionData(request, monitoringPojo);

        RestSystemMonitor restSystemMonitor = sd.getSystemMonitor();

        String agent = request.getLocalAddr() + ":" + request.getLocalPort();

        Set<ReadingBean> readings = restSystemMonitor.scheduleMonitoring(agent,
                                                                         monitoringPojo.getReading(),
                                                                         monitoringPojo.getReadingParametersAsMap());

        restSystemMonitor.setScheduledReadingTypes(readings);

        String readingParametersAsString = entrySetAsString(monitoringPojo.getReadingParametersAsMap());

        return Response.ok("{\"status\":\"scheduled monitoring for reading '"
                           + monitoringPojo.getReading() + "' and readingParameters '"
                           + readingParametersAsString + "'\"}")
                       .build();

    } catch (Exception e) {
        return Response.serverError().entity(new ErrorPojo(e)).build();
    } finally {
        ThreadsPerCaller.unregisterThread();
    }

}
 
Example 13
Project: ats-framework   File: MonitoringServiceImpl.java   View Source Code Vote up 5 votes
@POST
@Path( "scheduleProcessMonitoring")
@Consumes( MediaType.APPLICATION_JSON)
@Produces( MediaType.APPLICATION_JSON)
public Response scheduleProcessMonitoring(
                                           @Context HttpServletRequest request,
                                           ScheduleProcessMonitoringPojo monitoringPojo ) {

    final String caller = getCaller(request, monitoringPojo, false);
    ThreadsPerCaller.registerThread(caller);

    try {
        SessionData sd = getSessionData(request, monitoringPojo);

        RestSystemMonitor restSystemMonitor = sd.getSystemMonitor();

        String agent = request.getLocalAddr() + ":" + request.getLocalPort();

        Set<ReadingBean> readings = restSystemMonitor.scheduleProcessMonitoring(agent,
                                                                                monitoringPojo.getProcessPattern(),
                                                                                monitoringPojo.getProcessAlias(),
                                                                                monitoringPojo.getProcessUsername(),
                                                                                monitoringPojo.getProcessReadingTypes());

        restSystemMonitor.setScheduledReadingTypes(readings);

    } catch (Exception e) {
        return Response.serverError().entity(new ErrorPojo(e)).build();
    } finally {
        ThreadsPerCaller.unregisterThread();
    }

    String statusMessage = "{\"status \": \"scheduled process monitoring with parameters '"
                           + monitoringPojo.toString() + "'\"}";

    return Response.ok(statusMessage).build();
}
 
Example 14
Project: ats-framework   File: MonitoringServiceImpl.java   View Source Code Vote up 5 votes
@POST
@Path( "scheduleJvmMonitoring")
@Consumes( MediaType.APPLICATION_JSON)
@Produces( MediaType.APPLICATION_JSON)
public Response scheduleJvmMonitoring(
                                       @Context HttpServletRequest request,
                                       ScheduleJvmMonitoringPojo monitoringPojo ) {

    final String caller = getCaller(request, monitoringPojo, false);
    ThreadsPerCaller.registerThread(caller);

    try {

        SessionData sd = getSessionData(request, monitoringPojo);

        RestSystemMonitor restSystemMonitor = sd.getSystemMonitor();

        String agent = request.getLocalAddr() + ":" + request.getLocalPort();

        Set<ReadingBean> readings = restSystemMonitor.scheduleJvmMonitoring(agent,
                                                                            monitoringPojo.getJvmPort(),
                                                                            (monitoringPojo.getAlias() == null)
                                                                                                                ? ""
                                                                                                                : monitoringPojo.getAlias(),
                                                                            monitoringPojo.getJvmReadingTypes());

        restSystemMonitor.setScheduledReadingTypes(readings);

    } catch (Exception e) {
        return Response.serverError().entity(new ErrorPojo(e)).build();
    } finally {
        ThreadsPerCaller.unregisterThread();
    }

    String statusMessage = "{\"status \": \"scheduled JVM monitoring with parameters '"
                           + monitoringPojo.toString() + "'\"}";

    return Response.ok(statusMessage).build();
}
 
Example 15
Project: ats-framework   File: MonitoringServiceImpl.java   View Source Code Vote up 5 votes
@POST
@Path( "scheduleCustomJvmMonitoring")
@Consumes( MediaType.APPLICATION_JSON)
@Produces( MediaType.APPLICATION_JSON)
public Response scheduleCustomJvmMonitoring(
                                             @Context HttpServletRequest request,
                                             ScheduleCustomJvmMonitoringPojo monitoringPojo ) {

    final String caller = getCaller(request, monitoringPojo, false);
    ThreadsPerCaller.registerThread(caller);

    try {

        SessionData sd = getSessionData(request, monitoringPojo);

        RestSystemMonitor restSystemMonitor = sd.getSystemMonitor();

        String agent = request.getLocalAddr() + ":" + request.getLocalPort();

        Set<ReadingBean> readings = restSystemMonitor.scheduleCustomJvmMonitoring(agent,
                                                                                  monitoringPojo.getJmxPort(),
                                                                                  monitoringPojo.getAlias(),
                                                                                  monitoringPojo.getMbeanName(),
                                                                                  monitoringPojo.getUnit(),
                                                                                  monitoringPojo.getMbeanAttributes());

        restSystemMonitor.setScheduledReadingTypes(readings);

    } catch (Exception e) {
        return Response.serverError().entity(new ErrorPojo(e)).build();
    } finally {
        ThreadsPerCaller.unregisterThread();
    }

    String statusMessage = "{\"status \": \"scheduled custom JVM monitoring with parameters '"
                           + monitoringPojo.toString() + "'\"}";

    return Response.ok(statusMessage).build();
}
 
Example 16
Project: ats-framework   File: MonitoringServiceImpl.java   View Source Code Vote up 5 votes
@POST
@Path( "scheduleUserActivity")
@Consumes( MediaType.APPLICATION_JSON)
@Produces( MediaType.APPLICATION_JSON)
public Response scheduleUserActivity(
                                      @Context HttpServletRequest request,
                                      BasePojo basePojo ) {

    final String caller = getCaller(request, basePojo, false);
    ThreadsPerCaller.registerThread(caller);

    try {

        SessionData sd = getSessionData(request, basePojo);

        RestSystemMonitor restSystemMonitor = sd.getSystemMonitor();

        String agent = request.getLocalAddr() + ":" + request.getLocalPort();

        restSystemMonitor.scheduleUserActivity(agent);

    } catch (Exception e) {
        return Response.serverError().entity(new ErrorPojo(e)).build();
    } finally {
        ThreadsPerCaller.unregisterThread();
    }

    String statusMessage = "{\"status \": \"scheduled user activity monitoring.\"}";

    return Response.ok(statusMessage).build();
}
 
Example 17
Project: ats-framework   File: MonitoringServiceImpl.java   View Source Code Vote up 5 votes
@POST
@Path( "startMonitoring")
@Consumes( MediaType.APPLICATION_JSON)
@Produces( MediaType.APPLICATION_JSON)
public Response startMonitoring(
                                 @Context HttpServletRequest request,
                                 StartMonitoringPojo monitoringPojo ) {

    final String caller = getCaller(request, monitoringPojo, false);
    ThreadsPerCaller.registerThread(caller);

    try {

        SessionData sd = getSessionData(request, monitoringPojo);

        RestSystemMonitor restSystemMonitor = sd.getSystemMonitor();

        String agent = request.getLocalAddr() + ":" + request.getLocalPort();

        // calculate the time offset between the agent and the test executor
        long timeOffset = System.currentTimeMillis() - monitoringPojo.getStartTimestamp();

        restSystemMonitor.startMonitoring(agent,
                                          monitoringPojo.getStartTimestamp(),
                                          monitoringPojo.getPollingInterval(),
                                          timeOffset);

        return Response.ok("{\"status\":\"monitoring started on every "
                           + monitoringPojo.getPollingInterval() + " seconds.\"}")
                       .build();
    } catch (Exception e) {
        return Response.serverError().entity(new ErrorPojo(e)).build();
    } finally {
        ThreadsPerCaller.unregisterThread();
    }

}
 
Example 18
Project: ctsms   File: Settings.java   View Source Code Vote up 5 votes
public static ArrayList<String> getTrustetRefererHosts(HttpServletRequest request) {
	ArrayList<String> referers = CommonUtil.getValueStringList(SettingCodes.TRUSTED_REFERER_HOSTS, getBundle(Bundle.SETTINGS), DefaultSettings.TRUSTED_REFERER_HOSTS);
	ArrayList<String> result = new ArrayList<String>(referers.size());
	Iterator<String> it = referers.iterator();
	while (it.hasNext()) {
		String referer = it.next();
		String substitution;
		if (referer.equalsIgnoreCase(DefaultSettings.LOCAL_ADDR_REFERER)) {
			if (request != null) {
				substitution = request.getLocalAddr();
				if (!CommonUtil.isEmptyString(substitution)) {
					result.add(substitution);
				}
			}
		} else if (referer.equalsIgnoreCase(DefaultSettings.LOCAL_NAME_REFERER)) {
			if (request != null) {
				substitution = request.getLocalName();
				if (!CommonUtil.isEmptyString(substitution)) {
					result.add(substitution);
				}
			}
		} else if (referer.equalsIgnoreCase(DefaultSettings.HTTP_DOMAIN_REFERER)) {
			substitution = WebUtil.getHttpDomainName();
			if (!CommonUtil.isEmptyString(substitution)) {
				result.add(substitution);
			}
		} else {
			result.add(referer);
		}
	}
	return result;
}
 
Example 19
Project: gitplex-mit   File: ServletRequestCopy.java   View Source Code Vote up 4 votes
public ServletRequestCopy(HttpServletRequest request) {
	this.servletPath = request.getServletPath();
	this.contextPath = request.getContextPath();
	this.pathInfo = request.getPathInfo();
	this.requestUri = request.getRequestURI();
	this.requestURL = request.getRequestURL();
	this.method = request.getMethod();
	this.serverName = request.getServerName();
	this.serverPort = request.getServerPort();
	this.protocol = request.getProtocol();
	this.scheme = request.getScheme();
	
	
	/*
	 * have to comment out below two lines as otherwise web socket will
	 * report UnSupportedOperationException upon connection
	 */
	//this.characterEncoding = request.getCharacterEncoding();
	//this.contentType = request.getContentType();
	//this.requestedSessionId = request.getRequestedSessionId();
	this.characterEncoding = null;
	this.contentType = null;
	this.requestedSessionId = null;
	
	this.locale = request.getLocale();
	this.locales = request.getLocales();
	this.isSecure = request.isSecure();
	this.remoteUser = request.getRemoteUser();
	this.remoteAddr = request.getRemoteAddr();
	this.remoteHost = request.getRemoteHost();
	this.remotePort = request.getRemotePort();
	this.localAddr = request.getLocalAddr();
	this.localName = request.getLocalName();
	this.localPort = request.getLocalPort();
	this.pathTranslated = request.getPathTranslated();
	this.principal = request.getUserPrincipal();

	HttpSession session = request.getSession(true);
	httpSession = new HttpSessionCopy(session);

	String s;
	Enumeration<String> e = request.getHeaderNames();
	while (e != null && e.hasMoreElements()) {
		s = e.nextElement();
		Enumeration<String> headerValues = request.getHeaders(s);
		this.headers.put(s, headerValues);
	}

	e = request.getAttributeNames();
	while (e != null && e.hasMoreElements()) {
		s = e.nextElement();
		attributes.put(s, request.getAttribute(s));
	}

	e = request.getParameterNames();
	while (e != null && e.hasMoreElements()) {
		s = e.nextElement();
		parameters.put(s, request.getParameterValues(s));
	}
}
 
Example 20
Project: xproject   File: NetUtils.java   View Source Code Vote up 3 votes
/**
 * 获取服务器的ip地址:端口号
 * @param request
 * @return
 */
public static String getLocalIpAddr(HttpServletRequest request) {
	return request.getLocalAddr() + ":" + request.getLocalPort();
}