Example usage for javax.xml.ws.handler MessageContext SERVLET_REQUEST

List of usage examples for javax.xml.ws.handler MessageContext SERVLET_REQUEST

Introduction

In this page you can find the example usage for javax.xml.ws.handler MessageContext SERVLET_REQUEST.

Prototype

String SERVLET_REQUEST

To view the source code for javax.xml.ws.handler MessageContext SERVLET_REQUEST.

Click Source Link

Document

Standard property: servlet request object.

Usage

From source file:org.soaplab.services.protocol.jaxws.AnalysisService.java

/**************************************************************************
 * Return a name under which this service was called. This
 * name is "hidden" in the URL invoking this web service. <p>
 *
 * @throw SoaplabException if the service name cannot be
 * obtained; or if the web service context is not known. The
 * latter may happen if this class is not used within a
 * servlet container (such as Tomcat) properly - because it is
 * the conainer who injects code into otherwise empty web
 * service context instance./*w w w .j av  a2 s .  c  o m*/
 *
 *************************************************************************/
@WebMethod(exclude = true)
public String getServiceName() throws SoaplabException {

    // do we have a Web Service Context?
    if (wsc == null)
        throw new SoaplabException("Internal error: Undefined Web Service Context.");

    MessageContext mcx = null;
    try {
        mcx = wsc.getMessageContext();
        if (mcx == null)
            throw new SoaplabException("Internal error: Undefined Message Context.");
    } catch (java.lang.IllegalStateException e) {
        throw new SoaplabException("Internal error: The method is called while no request is being serviced");
    }
    HttpServletRequest req = (HttpServletRequest) mcx.get(MessageContext.SERVLET_REQUEST);
    if (req == null)
        throw new SoaplabException("Internal error: Undefined HttpServletRequest.");
    String requestURI = req.getRequestURI();
    String serviceName = StringUtils.substringAfterLast(requestURI, "/");
    if (StringUtils.isBlank(serviceName)) {
        if (StringUtils.isNotBlank(requestURI)) {
            // request URI does not have any slash (is it possible?)
            return requestURI;
        } else {
            throw new SoaplabException("Internal error: Cannot recognize what service was called.");
        }
    }
    return serviceName;
}

From source file:org.soaplab.services.protocol.jaxws.AnalysisService.java

@WebMethod(exclude = true)
public void getClientInfo(SoaplabMap map) throws SoaplabException {

    // do we have a Web Service Context?
    if (wsc == null)
        throw new SoaplabException("Internal error: Undefined Web Service Context.");

    MessageContext mcx = null;/* ww  w .ja v  a2  s .c  o m*/

    try {
        mcx = wsc.getMessageContext();
        if (mcx == null)
            throw new SoaplabException("Internal error: Undefined Message Context.");
    } catch (java.lang.IllegalStateException e) {
        throw new SoaplabException("Internal error: The method is called while no request is being serviced");
    }
    HttpServletRequest httpReq = (HttpServletRequest) mcx.get(MessageContext.SERVLET_REQUEST);
    if (httpReq == null)
        throw new SoaplabException("Internal error: Undefined HttpServletRequest.");

    // Client IP address...
    String remoteIP = null;
    if (httpReq.getRemoteAddr() != null)
        remoteIP = httpReq.getRemoteAddr();
    // Handle proxy or load balancer client IP.
    if (httpReq.getHeader("X-Cluster-Client-Ip") != null) {
        remoteIP = httpReq.getHeader("X-Cluster-Client-Ip");
    }
    // Default value in case of problems
    if (remoteIP == null)
        remoteIP = "unknown";

    map.put(SoaplabConstants.REMOTE_IP, remoteIP);
    map.put(SoaplabConstants.INTERFACE, "jaxws");

    return;
}

From source file:org.viafirma.conector.security.SecurityServiceWebHandler.java

/**
 * Comprueba que las ips que acceden a la aplicacin son efectivamente ip
 * permitidas.//  w ww  . j  av  a  2 s.c  o  m
 * 
 * @see javax.xml.ws.handler.Handler#handleMessage(javax.xml.ws.handler.MessageContext)
 */
public boolean handleMessage(MessageHandlerContext context) {
    ServletRequest servletRequest = ((ServletRequest) context.get(MessageContext.SERVLET_REQUEST));
    String remoteAddres = servletRequest.getRemoteAddr();
    if (ipsAllowedList == null) {
        ipCacheMaker(context, servletRequest);
    }
    String auxRemoteAddres = "";
    String auxIpAllowed = "";
    boolean allow = false;
    for (String ipAllowed : ipsAllowedList) {
        if (ipAllowed.contains("*") && allow == false) {
            int astPosition = ipAllowed.indexOf("*");
            auxRemoteAddres = remoteAddres.substring(0, astPosition);
            auxIpAllowed = ipAllowed.substring(0, astPosition);
            if (auxIpAllowed.equals(auxRemoteAddres)) {
                if (log.isInfoEnabled())
                    log.info("Servicio Web solicitado desde ip: " + remoteAddres);
                allow = true;
            }
        } else {
            if (ipAllowed.equals(remoteAddres) && allow == false) {
                if (log.isInfoEnabled())
                    log.info("Servicio Web solicitado desde ip: " + remoteAddres);
                allow = true;
            }
        }
    }
    if (!allow) {
        log.error("Acceso denegado. La ip " + remoteAddres + " no tiene permiso para acceder a los WS.");
        throw new WebServiceException(
                "Acceso denegado. La ip " + remoteAddres + " no tiene permiso para acceder a los WS.");
    }

    return true;

}