从JAX-RPC Web服务获取客户端IP地址的问题

时间:2015-08-02 05:34:07

标签: java web-services

我使用J2EE 1.4 创建了 JAX-RPC Web服务。当我尝试从Web服务获取客户端IP地址时,它始终将 IP设为127.0.0.1 < / strong>即可。 请解决我的问题。

服务实施类:

public class LeaveDetailsServiceImpl implements ServiceLifecycle {

private javax.xml.rpc.server.ServletEndpointContext ctx;

public void init(Object arg0) throws ServiceException {
    ctx=(javax.xml.rpc.server.ServletEndpointContext)arg0;
    }
public String getLeaveDetails(String empCode,String leaveYear,String uname,String pwd) throws RemoteException {
    StringBuffer resultMessage=new StringBuffer();
    String finalResult="";
    EmployeeWorker empWorker=new EmployeeWorker();
    HttpServletRequest request = (HttpServletRequest) ctx.getMessageContext().getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST);

    String clientIpAddress = request.getHeader("X-FORWARDED-FOR");
    if (clientIpAddress == null) {
        clientIpAddress = request.getRemoteAddr();
    }

    //LeaveDetailsForm leaveDetails=new LeaveDetailsForm();
    try
    {
     if(ServiceUtil.authenticate(uname,pwd,clientIpAddress))
     {
         if(empCode.trim()==null || empCode.trim().length()==0 || empCode.trim().equals(""))
            {
                resultMessage.append("Emp Code is required<br/>");
            }
         if(leaveYear.trim()==null || leaveYear.trim().length()==0 || leaveYear.trim().equals(""))
            {
                resultMessage.append("Leave Year is required<br/>");
            }
         if(!empCode.trim().matches("^\\d+$"))
         {
             resultMessage.append("Emp Code should have digits only<br/>");
         }
         else
         {

             if(!empWorker.checkEmpCode(empCode))
                     {
                 resultMessage.append("Employee Code does not exist<br/>");
             }
         }

         if(!leaveYear.trim().matches("^\\d{4}$"))
         {
             resultMessage.append("Leave Year should have exactly 4 digits<br/>");
         }
         else if(Integer.parseInt(leaveYear.trim())<2006)
         {
             resultMessage.append("Leave Year can not be before 2006<br/>");
         }
         else if(Integer.parseInt(leaveYear.trim())>=Calendar.getInstance().get(Calendar.YEAR))
         {
             resultMessage.append("Leave Year can not be a future year<br/>");
         }

         finalResult=resultMessage.toString();
         if(finalResult.equals(""))
         {

         ServiceUtil sUtil=new ServiceUtil();
         finalResult=sUtil.getServiceWaitTime();
           if(finalResult.equals("ok"))
           {

    //aLeaveDetails=

            finalResult=empWorker.getLeaveDetails(empCode,leaveYear);
            if(finalResult.indexOf("<Leave>") ==-1)
            {
                finalResult="Employee has no leave record for the year "+leaveYear;
            }
        }

           }

     }
    else
     {
        finalResult="You are not a authorized user";        

     }   

    }
    catch(Exception ex){

        System.out.println( ex.getMessage());

    }
     return finalResult;
}

public void destroy() {
    // TODO Auto-generated method stub

}

}

过滤类:

public class ClientIPAddressFilter implements Filter {

public ClientIPAddressFilter() {
super();
}

public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}

public void doFilter(ServletRequest request, ServletResponse response,   FilterChain chain) throws IOException, ServletException {
System.out.println("Remote Address: "+request.getRemoteAddr());
chain.doFilter(request, response);

}

public void destroy() {
// TODO Auto-generated method stub
}

}

的web.xml

<display-name>leavewebservice</display-name>
<filter>
    <description>
    </description>
    <display-name>
    ClientIPAddressFilter</display-name>
    <filter-name>ClientIPAddressFilter</filter-name>
    <filter-class>in.nic.util.ClientIPAddressFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>ClientIPAddressFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<servlet>
    <display-name>Apache-Axis Servlet</display-name>
    <servlet-name>AxisServlet</servlet-name>
    <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
</servlet>
<servlet>
    <display-name>Axis Admin Servlet</display-name>
    <servlet-name>AdminServlet</servlet-name>
    <servlet-class>org.apache.axis.transport.http.AdminServlet</servlet-class>
    <load-on-startup>100</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>AxisServlet</servlet-name>
    <url-pattern>/servlet/AxisServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>AxisServlet</servlet-name>
    <url-pattern>*.jws</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>AxisServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>AdminServlet</servlet-name>
    <url-pattern>/servlet/AdminServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
</welcome-file-list>

客户端调用Web服务类

public class LeaveDetailsServiceClientAction extends Action{
public ActionForward execute(ActionMapping mapping,
        ActionForm form,
        HttpServletRequest request,
        HttpServletResponse response)
throws IOException, ServletException {       

    //String target="success";
    ServiceClientForm scForm=(ServiceClientForm)form;
    ArrayList alCmtAlert=null;
    //CommitteeDetailsForm cmtOldDetails=new  CommitteeDetailsForm();

    String target="";
    long waitTime=0;
    //String minutesID="";

    ActionErrors errors=new ActionErrors();
    //ArrayList alVerifyMinutes=null;
    //String [] empcode=cmtDetails.getEmpcode();
    try
    {       
         String endPoint= "http://localhost:8080/leavewebservice/services/LeaveDetailsServiceImpl";
         String targetNamespace = "http://services.nic.in";
         String serviceName = "LeaveDetailsServiceImplService";
         String portName = "LeaveDetailsServiceImpl";
        QName qname = new QName("http://forms.leave.nic.in","LeaveDetailsForm");
        //Service service=new Service();
                // Service lookup 
                ServiceFactory serviceFactory = ServiceFactory.newInstance();
                javax.xml.rpc.Service service = serviceFactory.createService(
                    new QName(targetNamespace, serviceName));

                 //Service access 
                Call call = (Call) service.createCall();
               call.setProperty(Call.ENCODINGSTYLE_URI_PROPERTY, "");
            call.setProperty(Call.OPERATION_STYLE_PROPERTY, "wrapped");

            call.setTargetEndpointAddress(endPoint);
             call.removeAllParameters();
            call.setPortTypeName(new QName(targetNamespace, portName));
            call.setOperationName(new QName(targetNamespace, "getLeaveDetails"));

                if (call.isParameterAndReturnSpecRequired(call.getOperationName())){
                    call.addParameter(
                        "in0",
                        new QName("http://www.w3.org/2001/XMLSchema", "string"),
                String.class, javax.xml.rpc.ParameterMode.IN);
                    call.addParameter(
                            "in1",
                            new QName("http://www.w3.org/2001/XMLSchema", "string"),
                    String.class, javax.xml.rpc.ParameterMode.IN);
                    call.addParameter(
                            "in2",
                            new QName("http://www.w3.org/2001/XMLSchema", "string"),
                    String.class, javax.xml.rpc.ParameterMode.IN);
                    call.addParameter(
                            "in3",
                            new QName("http://www.w3.org/2001/XMLSchema", "string"),
                    String.class, javax.xml.rpc.ParameterMode.IN);
                    //call.setReturnClass(LeaveDetailsForm[].class);
                    //call.setre
                    call.setReturnType(new QName("http://www.w3.org/2001/XMLSchema", "string"));
                    //call.setReturnQName(new javax.xml.namespace.QName("http://services.nic.in", "getLeaveDetailsReturn"));
               }
                           String[] params = {scForm.getEmpCode(),scForm.getLeaveYear(),"ashish","6295"};
        //       LeaveDetailsForm[] aLeaveDetails=null;
               /* Service invocation */

                String result= (String)(call.invoke(params));
                    request.setAttribute("RESULT", result);

                target="success";   
    }
catch(Exception ex){
    target = "failure";
    System.out.println( ex.getMessage());
    errors.add("GLOBAL_ERROR",
            new ActionError("errors.database.error",
                    ex.getMessage())); 
}
if ( !errors.isEmpty() ) {
    target="failure";
    saveErrors(request, errors);
}

//System.out.println("hi------------"+target);
return mapping.findForward(target);
}

}

0 个答案:

没有答案