Example usage for javax.servlet.http HttpServletRequest getRemoteAddr

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

Introduction

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

Prototype

public String getRemoteAddr();

Source Link

Document

Returns the Internet Protocol (IP) address of the client or last proxy that sent the request.

Usage

From source file:cn.sel.wetty.interceptor.AccessLogger.java

private void o(HttpServletRequest request, HttpServletResponse response) {
    String msg = String.format("%s\t[%s] -> %s \tStatus:%s\tHeaders:%s", request.getRequestURI(),
            request.getMethod(), request.getRemoteAddr(), response.getStatus(), getHeaders(response));
    LOGGER.info(msg);/*  ww  w  .ja  v a 2s  . c om*/
}

From source file:RestrictUserIP.java

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    PrintWriter out;//from www  . ja  va 2 s  . co  m
    /**
     * Status code (401) indicating that the request requires HTTP
     * authentication.
     */
    if (req.getRemoteAddr().equals("142.3.28.87")) {
        resp.sendError(HttpServletResponse.SC_UNAUTHORIZED);
    }
    resp.setContentType("text/html");
    out = resp.getWriter();
    out.println("<HTML>");
    out.println("<BODY>");
    out.println("<H1>");
    out.println("Hello!");
    out.println("<BR>");
    out.println("Your IP Address: " + req.getRemoteAddr());
    out.println("</H1>");
    out.println("</body>");
    out.println("</html>");
    out.close();
}

From source file:com.googlecode.psiprobe.controllers.wrapper.RestartJvmController.java

protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    boolean done = false;
    try {//from w  w  w .  j  ava2s. c  o m
        Class.forName("org.tanukisoftware.wrapper.WrapperManager");
        logger.info("JVM is RESTARTED by " + request.getRemoteAddr());
        WrapperManager.restartAndReturn();
        done = true;
    } catch (ClassNotFoundException e) {
        logger.info("WrapperManager not found. Do you have wrapper.jar in the classpath?");
    }
    return new ModelAndView(getViewName(), "done", Boolean.valueOf(done));
}

From source file:net.testdriven.psiprobe.controllers.wrapper.RestartJvmController.java

protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    boolean done = false;
    try {/*  ww w  . j av  a2  s  . c  om*/
        Class.forName("org.tanukisoftware.wrapper.WrapperManager");
        logger.info("JVM is RESTARTED by " + request.getRemoteAddr());
        WrapperManager.restartAndReturn();
        done = true;
    } catch (ClassNotFoundException e) {
        logger.info("WrapperManager not found. Do you have wrapper.jar in the classpath?");
    }
    return new ModelAndView(getViewName(), "done", done);
}

From source file:org.openmrs.module.kenyaemr.SystemMonitorController.java

@RequestMapping(value = "/sysmon.form", method = RequestMethod.GET)
public void get(HttpServletRequest request, HttpServletResponse response) throws IOException {
    SimpleObject output;/* w ww  .j av a2s .  c  om*/

    if (checkAccess(request)) {
        log.info("Accepting system monitoring request from " + request.getRemoteAddr());
        output = getMonitoredData();
    } else {
        log.warn("Rejecting system monitoring request from " + request.getRemoteAddr() + " (server address is "
                + request.getLocalAddr() + ")");
        response.setStatus(403);
        output = SimpleObject.create("error", "access denied");
    }

    response.setContentType("application/json");
    response.getWriter().write(output.toJson());
}

From source file:com.haulmont.restapi.auth.CubaUserAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
            .currentRequestAttributes();
    HttpServletRequest request = attributes.getRequest();

    String ipAddress = request.getRemoteAddr();

    if (authentication instanceof UsernamePasswordAuthenticationToken) {
        RestApiConfig config = configuration.getConfig(RestApiConfig.class);
        if (!config.getStandardAuthenticationEnabled()) {
            log.debug(/*from  ww  w . j  a  v  a  2s. c  o  m*/
                    "Standard authentication is disabled. Property cuba.rest.standardAuthenticationEnabled is false");

            throw new InvalidGrantException("Authentication disabled");
        }

        UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) authentication;

        String login = (String) token.getPrincipal();

        UserSession session;
        try {
            String passwordHash = passwordEncryption.getPlainHash((String) token.getCredentials());

            LoginPasswordCredentials credentials = new LoginPasswordCredentials(login, passwordHash);
            credentials.setIpAddress(ipAddress);
            credentials.setClientType(ClientType.REST_API);
            credentials.setClientInfo(makeClientInfo(request.getHeader(HttpHeaders.USER_AGENT)));

            //if the locale value is explicitly passed in the Accept-Language header then set its value to the
            //credentials. Otherwise, the locale of the user should be used
            Locale locale = restAuthUtils.extractLocaleFromRequestHeader(request);
            if (locale != null) {
                credentials.setLocale(locale);
                credentials.setOverrideLocale(true);
            } else {
                credentials.setOverrideLocale(false);
            }

            session = authenticationService.login(credentials).getSession();
        } catch (AccountLockedException le) {
            log.info("Blocked user login attempt: login={}, ip={}", login, ipAddress);
            throw new LockedException("User temporarily blocked");
        } catch (RestApiAccessDeniedException ex) {
            log.info("User is not allowed to use the REST API {}", login);
            throw new BadCredentialsException("User is not allowed to use the REST API");
        } catch (LoginException e) {
            log.info("REST API authentication failed: {} {}", login, ipAddress);
            throw new BadCredentialsException("Bad credentials");
        }

        AppContext.setSecurityContext(new SecurityContext(session));

        UsernamePasswordAuthenticationToken result = new UsernamePasswordAuthenticationToken(
                authentication.getPrincipal(), authentication.getCredentials(),
                getRoleUserAuthorities(authentication));
        @SuppressWarnings("unchecked")
        Map<String, String> details = (Map<String, String>) authentication.getDetails();
        details.put(SESSION_ID_DETAILS_ATTRIBUTE, session.getId().toString());
        result.setDetails(details);
        return result;
    }

    return null;
}

From source file:fr.scc.elo.controller.EloController.java

@GET
@Path("/match/winners/{w}/loosers/{l}/score/{s}/type/{t}/create/{c}")
@ApiOperation(value = "gerer le score d'un match", notes = "calcule l'evolution du elo de chaque joueur suite a un match", response = MatchResponse.class)
public Response manageMatch(@BeanParam MatchRequest request, @Context HttpServletRequest http)
        throws EloException {
    try {/*from ww  w. j a  v a  2 s.c om*/
        if (!connectionService.acceptIPUpdate(http.getRemoteAddr())) {
            throw new EloException("Unauthorized IP: " + http.getRemoteAddr());
        }
        MatchResponse response = eloManager.manageMatch(StringUtils.lowerCase(request.getWinnerList()),
                StringUtils.lowerCase(request.getLooserList()), request.getScore(), request.getTypeMatch(),
                request.getCreatePlayers());
        return Response.ok().entity(response.toString()).build();
    } catch (Exception e) {
        return Response.ok().entity(e.getMessage()).build();
    }
}

From source file:spring.travel.site.request.RequestInfoInterceptor.java

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception {
    request.setAttribute(requestAttributeName,
            new Request(Optional.empty(), getSessionCookie(request), request.getRemoteAddr()));
    return true;//from   w  w w  .  ja v  a 2 s  .c  om
}

From source file:com.googlecode.psiprobe.controllers.wrapper.ThreadDumpController.java

protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    boolean done = false;
    try {/*from  w  w  w.  jav a 2s .c o m*/
        Class.forName("org.tanukisoftware.wrapper.WrapperManager");
        logger.info("ThreadDump requested by " + request.getRemoteAddr());
        WrapperManager.requestThreadDump();
        done = true;
    } catch (ClassNotFoundException e) {
        logger.info("WrapperManager not found. Do you have wrapper.jar in the classpath?");
    }
    return new ModelAndView(getViewName(), "done", Boolean.valueOf(done));
}

From source file:net.testdriven.psiprobe.controllers.wrapper.ThreadDumpController.java

protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    boolean done = false;
    try {//from   ww w  . j av a2  s .co  m
        Class.forName("org.tanukisoftware.wrapper.WrapperManager");
        logger.info("ThreadDump requested by " + request.getRemoteAddr());
        WrapperManager.requestThreadDump();
        done = true;
    } catch (ClassNotFoundException e) {
        logger.info("WrapperManager not found. Do you have wrapper.jar in the classpath?");
    }
    return new ModelAndView(getViewName(), "done", done);
}