一、编写服务端并发布
1、定义接口
2、接口实现
3、处理用户身份认证类
4、请求处理链
5、发布WS服务
二、编写调用WS的客户端
已有 0人发表留言,猛击->> 这里<<-参与讨论
ITeye推荐
1、定义接口
public interface EchoService { public String echo(String name); }
2、接口实现
@WebService(name="wsecho",portName="echoServicePort", serviceName="echoService",targetNamespace="http://ws.logcd.net/") @SOAPBinding(style = Style.DOCUMENT,use = Use.LITERAL)//Style.DOCUMENT为默认值 @HandlerChain(file="handle-chain.xml") public class EchoServiceImpl implements EchoService{ @WebMethod() public String echo(//指定参数名及命名空间 @WebParam(name="userName", targetNamespace="http://ws.logcd.net/") String name) { return "hello, " + name; } }
3、处理用户身份认证类
public class AuthValidatorHandler implements SOAPHandler<SOAPMessageContext> { public static final String TARGET_NAMESPACE = "http://ws.logcd.net/"; public static final String AUTH_HEADER_NAME = "AuthHeader"; public static final Map<String,String> cacheAcountMap = new HashMap<String,String>(); static{ cacheAcountMap.put("510028", "123456"); } @SuppressWarnings("unchecked") public boolean handleMessage(SOAPMessageContext ctx) { Boolean isResponsed=(Boolean)ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); if(!isResponsed){ SOAPMessage msg=ctx.getMessage(); try { String id = "", pwd = ""; SOAPHeader header = msg.getSOAPHeader(); Iterator<SOAPHeaderElement> it = header.getChildElements(new QName(TARGET_NAMESPACE, AUTH_HEADER_NAME)); if(it.hasNext()){ SOAPHeaderElement headElem = (SOAPHeaderElement)it.next(); Iterator<SOAPElement> eleIt = headElem.getChildElements(); while(eleIt.hasNext()){ SOAPElement ele = eleIt.next(); String key = ele.getLocalName(); if(key.toUpperCase().equals("ACCOUNT")){ String val = ele.getValue(); String[] arr = val.split("/"); if(arr.length == 2){ id = arr[0]; pwd = arr[1]; } break; } } } System.out.println("id="+id+",pwd="+pwd); if(StringUtils.isEmpty(id) || StringUtils.isEmpty(pwd)){ throw new SOAPFaultException(new QName(TARGET_NAMESPACE, "-1"),"User Acount Err!", "id or pwd is empty.", null); } return authValidate(id, pwd); } catch (SOAPException e) { throw new RuntimeException(e); } } return false; } /* * 验证用户名和密码 */ private boolean authValidate(String id, String pwd){ if(cacheAcountMap.containsKey(id) && ((String)cacheAcountMap.get(id)).equals(pwd)){ return true; }else{ throw new SOAPFaultException(new QName(TARGET_NAMESPACE, "-2"),"Unauthorized Visit!", "id or pwd incorrect.", null); } } @Override public void close(MessageContext arg0) { } @Override public boolean handleFault(SOAPMessageContext arg0) { return false; } @Override public Set<QName> getHeaders() { return null; } }
4、请求处理链
<?xml version="1.0" encoding="UTF-8"?><javaee:handler-chains xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><javaee:handler-chain><javaee:handler><javaee:handler-class>com.cdmcs.jws.auth.AuthValidatorHandler</javaee:handler-class></javaee:handler></javaee:handler-chain></javaee:handler-chains>
5、发布WS服务
public class WSPublisher { public static void main(String[] args){ Endpoint.publish("http://localhost:8099/echoService", new EchoServiceImpl()); System.out.println("publish all services..."); } }
二、编写调用WS的客户端
public class Jdk6WsAxisClient { public static void main(String[] args) { try { String endpoint = "http://localhost:8099/echoService"; String nameSpace = "http://ws.logcd.net/"; Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress(endpoint); call.setOperationName(new QName(nameSpace, "echo")); //参数名为WebParam中指定的参数名,未指定参数名时传arg0-argN //call.addParameter("userName", XMLType.XSD_STRING, ParameterMode.IN); //Style.DOCUMENT,且方法参数没有指定命名空间或Style.RPC call.addParameter(new QName(nameSpace, "userName"), XMLType.XSD_STRING, ParameterMode.IN);//Style.DOCUMENT指定了命名空间 call.setUseSOAPAction(true); call.setReturnType(org.apache.axis.Constants.XSD_STRING); call.setSOAPActionURI(nameSpace+"echoService"); SOAPHeaderElement soapHeaderElement = new SOAPHeaderElement(nameSpace, "AuthHeader"); soapHeaderElement.addChildElement("ACCOUNT").setValue("510028/123456"); call.addHeader(soapHeaderElement); String result = (String) call.invoke(new Object[] {"frank"}); System.out.println(result); } catch (Exception e) { e.printStackTrace(); } } }
- 本文附件下载:
- lib.rar (2.7 MB)
已有 0人发表留言,猛击->> 这里<<-参与讨论
ITeye推荐