Example usage for javax.servlet.http HttpServletRequest getLocalAddr

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

Introduction

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

Prototype

public String getLocalAddr();

Source Link

Document

Returns the Internet Protocol (IP) address of the interface on which the request was received.

Usage

From source file:com.betel.flowers.web.bean.CredencialBean.java

public void handleSaveFoto(FileUploadEvent event) {
    this.setFile(event.getFile());
    if (this.getFile() != null) {
        if ((getFile().getFileName().endsWith(".png") || getFile().getFileName().endsWith(".PNG")
                || getFile().getFileName().endsWith(".jpg") || getFile().getFileName().endsWith(".JPG")
                || getFile().getFileName().endsWith(".jpg") || getFile().getFileName().endsWith(".JPEG")
                || getFile().getFileName().endsWith(".jpeg")) && this.getFile().getSize() < sizeImage) {
            try {
                HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance()
                        .getExternalContext().getRequest();
                String ipAdress = request.getLocalAddr();
                String filepath = "http://" + ipAdress + "/usuarios/";
                String url = "/var/www/html/usuarios/";
                String ext = getFile().getContentType();
                DeleteFileRun deleteFile = new DeleteFileRun(url,
                        this.userSession.getInfoPersonal().getCodigoFoto(), ext.replace("image/", ""));
                deleteFile.run();//  ww w  .  jav  a2s . co  m
                this.userSession.getInfoPersonal().setCodigoFoto(this.codigoFoto());
                UploadFileRun upload = new UploadFileRun(url,
                        this.userSession.getInfoPersonal().getCodigoFoto(), ext.replace("image/", ""),
                        getFile().getInputstream());
                upload.run();
                if (upload.getExito()) {
                    this.userSession.getInfoPersonal()
                            .setUrlFoto(filepath + this.userSession.getInfoPersonal().getCodigoFoto() + "."
                                    + ext.replace("image/", ""));
                    LOG.log(Level.INFO, "dir IP save: " + filepath);
                    this.userSession.getInfoPersonal().setLoadFoto(Boolean.TRUE);
                    this.usuarioService.update(this.userSession);
                    FacesUtil.addMessageInfo("Se ha agregado la imagen.");
                }
            } catch (IOException ex) {
                //log.level.error("Error al subir la imagen", ex);
            }
        } else {
            FacesUtil.addMessageInfo("Tamao maximo de imagen 5.5 MB.");
        }

    }
}

From source file:com.predic8.membrane.servlet.embedded.HttpServletHandler.java

public HttpServletHandler(HttpServletRequest request, HttpServletResponse response, Transport transport)
        throws IOException {
    super(transport);
    this.request = request;
    this.response = response;
    remoteAddr = InetAddress.getByName(request.getRemoteAddr());
    localAddr = InetAddress.getByName(request.getLocalAddr());
    exchange = new Exchange(this);

    exchange.setProperty(Exchange.HTTP_SERVLET_REQUEST, request);
}

From source file:org.apache.atlas.web.filters.AuditFilter.java

private void recordAudit(HttpServletRequest httpRequest, String whenISO9601, String who) {
    final String fromHost = httpRequest.getRemoteHost();
    final String fromAddress = httpRequest.getRemoteAddr();
    final String whatRequest = httpRequest.getMethod();
    final String whatURL = Servlets.getRequestURL(httpRequest);
    final String whatAddrs = httpRequest.getLocalAddr();

    final String whatUrlPath = httpRequest.getRequestURL().toString();//url path without query string

    if (!isOperationExcludedFromAudit(whatRequest, whatUrlPath.toLowerCase(), null)) {
        audit(who, fromAddress, whatRequest, fromHost, whatURL, whatAddrs, whenISO9601);
    } else {/*from   ww  w .j  a  va 2s .co m*/
        if (LOG.isDebugEnabled()) {
            LOG.debug(" Skipping Audit for {} ", whatURL);
        }
    }
}

From source file:com.att.nsa.cambria.service.impl.EventsServiceImpl.java

private static LogDetails generateLogDetails(final String topicName, HttpServletRequest request,
        final String messageTimestamp, int messageSequence, Long batchId, final boolean transactionEnabled) {
    LogDetails logDetails = new LogDetails();
    logDetails.setTopicId(topicName);/*from  w  w  w  .j a  v  a  2  s . c om*/
    logDetails.setMessageTimestamp(messageTimestamp);
    logDetails.setPublisherId(Utils.getUserApiKey(request));
    logDetails.setPublisherIp(request.getRemoteHost());
    logDetails.setMessageBatchId(batchId);
    logDetails.setMessageSequence(String.valueOf(messageSequence));
    logDetails.setTransactionEnabled(transactionEnabled);
    logDetails.setTransactionIdTs(Utils.getFormattedDate(new Date()));
    logDetails.setServerIp(request.getLocalAddr());
    return logDetails;
}

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;/*from w  w  w .ja  v  a  2 s.  com*/

    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.betel.flowers.web.bean.VariedadBean.java

public void savefoto(ActionEvent evt) {
    if (this.getFile() != null) {
        if ((getFile().getFileName().endsWith(".png") || getFile().getFileName().endsWith(".PNG")
                || getFile().getFileName().endsWith(".jpg") || getFile().getFileName().endsWith(".JPG")
                || getFile().getFileName().endsWith(".jpg") || getFile().getFileName().endsWith(".JPEG")
                || getFile().getFileName().endsWith(".jpeg")) && this.getFile().getSize() < sizeImage) {
            try {
                HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance()
                        .getExternalContext().getRequest();
                String ipAdress = request.getLocalAddr();
                String filepath = "http://" + ipAdress + "/variedades/";
                String url = "/var/www/html/variedades/";
                String ext = getFile().getContentType();
                UploadFileRun upload = new UploadFileRun(url, this.nuevo.getCodigoFoto(),
                        ext.replace("image/", ""), getFile().getInputstream());
                upload.run();//from w w w  .  j  av a2s  . com
                if (upload.getExito()) {
                    this.nuevo.setUrlFoto(
                            filepath + this.nuevo.getCodigoFoto() + "." + ext.replace("image/", ""));
                    LOG.log(Level.INFO, "dir IP save: " + filepath);
                }
            } catch (IOException ex) {
                //log.level.error("Error al subir la imagen", ex);
            }
        }

    }
}

From source file:com.betel.flowers.web.bean.VariedadBean.java

public void updatefoto(ActionEvent evt) {
    if (this.getFile() != null) {
        if ((getFile().getFileName().endsWith(".png") || getFile().getFileName().endsWith(".PNG")
                || getFile().getFileName().endsWith(".jpg") || getFile().getFileName().endsWith(".JPG")
                || getFile().getFileName().endsWith(".jpg") || getFile().getFileName().endsWith(".JPEG")
                || getFile().getFileName().endsWith(".jpeg")) && this.getFile().getSize() < sizeImage) {
            try {
                HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance()
                        .getExternalContext().getRequest();
                String ipAdress = request.getLocalAddr();
                String filepath = "http://" + ipAdress + "/variedades/";
                String url = "/var/www/html/variedades/";
                String ext = getFile().getContentType();
                UploadFileRun upload = new UploadFileRun(url, this.selected.getCodigoFoto(),
                        ext.replace("image/", ""), getFile().getInputstream());
                upload.run();//  w w  w  .  j  a v a2 s .co m
                if (upload.getExito()) {
                    this.selected.setUrlFoto(
                            filepath + this.selected.getCodigoFoto() + "." + ext.replace("image/", ""));
                    LOG.log(Level.INFO, "dir IP save: " + filepath);
                }
            } catch (IOException ex) {
                //log.level.error("Error al subir la imagen", ex);
            }
        }

    }
}

From source file:com.betel.flowers.web.bean.VariedadBean.java

public void handleSaveFoto(FileUploadEvent event) {
    this.setFile(event.getFile());
    if (this.getFile() != null) {
        if ((getFile().getFileName().endsWith(".png") || getFile().getFileName().endsWith(".PNG")
                || getFile().getFileName().endsWith(".jpg") || getFile().getFileName().endsWith(".JPG")
                || getFile().getFileName().endsWith(".jpg") || getFile().getFileName().endsWith(".JPEG")
                || getFile().getFileName().endsWith(".jpeg")) && this.getFile().getSize() < sizeImage) {
            try {
                HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance()
                        .getExternalContext().getRequest();
                String ipAdress = request.getLocalAddr();
                String filepath = "http://" + ipAdress + "/variedades/";
                String url = "/var/www/html/variedades/";
                String ext = getFile().getContentType();
                UploadFileRun upload = new UploadFileRun(url, this.nuevo.getCodigoFoto(),
                        ext.replace("image/", ""), getFile().getInputstream());
                upload.run();//from   ww w  .  j  a v  a2  s.c  om
                if (upload.getExito()) {
                    this.nuevo.setUrlFoto(
                            filepath + this.nuevo.getCodigoFoto() + "." + ext.replace("image/", ""));
                    LOG.log(Level.INFO, "dir IP save: " + filepath);
                    FacesUtil.addMessageInfo("Se ha agregado la imagen.");
                }
            } catch (IOException ex) {
                //log.level.error("Error al subir la imagen", ex);
            }
        } else {
            FacesUtil.addMessageInfo("Tamao maximo de imagen 5.5 MB.");
        }

    }
}

From source file:com.betel.flowers.web.bean.VariedadBean.java

public void handleUpdateFoto(FileUploadEvent event) {
    this.setFile(event.getFile());
    if (this.getFile() != null) {
        if ((getFile().getFileName().endsWith(".png") || getFile().getFileName().endsWith(".PNG")
                || getFile().getFileName().endsWith(".jpg") || getFile().getFileName().endsWith(".JPG")
                || getFile().getFileName().endsWith(".jpg") || getFile().getFileName().endsWith(".JPEG")
                || getFile().getFileName().endsWith(".jpeg")) && this.getFile().getSize() < sizeImage) {
            try {
                HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance()
                        .getExternalContext().getRequest();
                String ipAdress = request.getLocalAddr();
                String filepath = "http://" + ipAdress + "/variedades/";
                String url = "/var/www/html/variedades/";
                String ext = getFile().getContentType();
                DeleteFileRun deleteFile = new DeleteFileRun(url, this.selected.getCodigoFoto(),
                        ext.replace("image/", ""));
                deleteFile.run();/*from ww w  .  j ava2s.  co  m*/
                this.selected.setCodigoFoto(this.codigoFoto());
                UploadFileRun upload = new UploadFileRun(url, this.selected.getCodigoFoto(),
                        ext.replace("image/", ""), getFile().getInputstream());
                upload.run();
                if (upload.getExito()) {
                    this.selected.setUrlFoto(
                            filepath + this.selected.getCodigoFoto() + "." + ext.replace("image/", ""));
                    LOG.log(Level.INFO, "dir IP save: " + filepath);
                    FacesUtil.addMessageInfo("Se ha actualizado la imagen.");
                }
            } catch (IOException ex) {
                //log.level.error("Error al subir la imagen", ex);
            }
        } else {
            FacesUtil.addMessageInfo("Tamao maximo de imagen 5.5 MB.");
        }

    }
}

From source file:org.openmrs.module.formaccesscontrol.web.taglib.RequireTag.java

/**
 * This is where all the magic happens. The privileges are checked and the user is redirected if
 * need be. <br/>/*from w  ww .j a  va 2s.  com*/
 * <br/>
 * Returns SKIP_PAGE if the user doesn't have the privilege and SKIP_BODY if it does.
 * 
 * @see javax.servlet.jsp.tagext.TagSupport#doStartTag()
 * @should allow user with the privilege
 * @should allow user to have any privilege
 * @should allow user with all privileges
 * @should reject user without the privilege
 * @should reject user without any of the privileges
 * @should reject user without all of the privileges
 */
@Override
public int doStartTag() {
    if (form == null) {
        return SKIP_BODY;
    }

    errorOccurred = false;
    HttpServletResponse httpResponse = (HttpServletResponse) pageContext.getResponse();
    HttpSession httpSession = pageContext.getSession();
    HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
    String request_ip_addr = request.getLocalAddr();
    String session_ip_addr = (String) httpSession.getAttribute(WebConstants.OPENMRS_CLIENT_IP_HTTPSESSION_ATTR);

    UserContext userContext = Context.getUserContext();

    if (userContext == null && privilege != null) {
        log.error("userContext is null. Did this pass through a filter?");
        //httpSession.removeAttribute(WebConstants.OPENMRS_CONTEXT_HTTPSESSION_ATTR);
        //TODO find correct error to throw 
        throw new APIException("The context is currently null.  Please try reloading the site.");
    }

    // Parse comma-separated list of privileges in allPrivileges and anyPrivileges attributes
    String[] allPrivilegesArray = StringUtils.commaDelimitedListToStringArray(allPrivileges);
    String[] anyPrivilegeArray = StringUtils.commaDelimitedListToStringArray(anyPrivilege);

    FormAccessControlService svc = Context.getService(FormAccessControlService.class);
    boolean hasPrivilege = hasPrivileges(svc, form, privilege, allPrivilegesArray, anyPrivilegeArray);
    if (!hasPrivilege) {
        errorOccurred = true;
        if (userContext.isAuthenticated()) {
            String referer = request.getHeader("Referer");
            // If the user has just authenticated, but is still not authorized to see the page.
            if (referer != null && referer.contains("login.")) {
                try {
                    httpResponse.sendRedirect(request.getContextPath()); // Redirect to the home page.
                    return SKIP_PAGE;
                } catch (IOException e) {
                    // oops, cannot redirect
                    log.error("Unable to redirect to the home page", e);
                    throw new APIException(e);
                }
            }

            httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "require.unauthorized");
            log.warn("The user: '" + Context.getAuthenticatedUser() + "' has attempted to access: " + redirect
                    + " which requires privilege: " + privilege + " or one of: " + allPrivileges + " or any of "
                    + anyPrivilege);
        } else {
            httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "require.login");
        }
    } else if (hasPrivilege && userContext.isAuthenticated()) {
        // redirect users to password change form
        User user = userContext.getAuthenticatedUser();
        log.debug("Login redirect: " + redirect);
        if (new UserProperties(user.getUserProperties()).isSupposedToChangePassword()
                && !redirect.contains("options.form")) {
            httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "User.password.change");
            errorOccurred = true;
            redirect = request.getContextPath() + "/options.form#Change Login Info";
            otherwise = redirect;
            try {
                httpResponse.sendRedirect(redirect);
                return SKIP_PAGE;
            } catch (IOException e) {
                // oops, cannot redirect
                log.error("Unable to redirect for password change: " + redirect, e);
                throw new APIException(e);
            }
        }
    }

    if (differentIpAddresses(session_ip_addr, request_ip_addr)) {
        errorOccurred = true;
        // stops warning message in IE when refreshing repeatedly
        if ("0.0.0.0".equals(request_ip_addr) == false) {
            log.warn("Invalid ip addr: expected " + session_ip_addr + ", but found: " + request_ip_addr);
            httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "require.ip_addr");
        }
    }

    log.debug("session ip addr: " + session_ip_addr);

    if (errorOccurred) {

        String url = "";
        if (redirect != null && !redirect.equals("")) {
            url = request.getContextPath() + redirect;
        } else {
            url = request.getRequestURI();
        }

        if (request.getQueryString() != null) {
            url = url + "?" + request.getQueryString();
        }
        httpSession.setAttribute(WebConstants.OPENMRS_LOGIN_REDIRECT_HTTPSESSION_ATTR, url);
        try {
            httpResponse.sendRedirect(request.getContextPath() + otherwise);
            return SKIP_PAGE;
        } catch (IOException e) {
            // oops, cannot redirect
            throw new APIException(e);
        }
    }

    return SKIP_BODY;
}