1 17 package org.apache.geronimo.axis.client; 18 19 import java.lang.reflect.Method ; 20 import java.rmi.RemoteException ; 21 import java.util.Arrays ; 22 import java.util.Iterator ; 23 import java.util.Set ; 24 import java.util.List ; 25 import java.util.Map ; 26 import javax.security.auth.Subject ; 27 import javax.xml.rpc.holders.Holder ; 28 import javax.wsdl.OperationType; 29 30 import net.sf.cglib.proxy.MethodInterceptor; 31 import net.sf.cglib.proxy.MethodProxy; 32 import org.apache.axis.client.Call; 33 import org.apache.axis.description.ParameterDesc; 34 import org.apache.axis.utils.JavaUtils; 35 import org.apache.geronimo.security.ContextManager; 36 import org.apache.geronimo.security.jaas.NamedUsernamePasswordCredential; 37 38 41 public class ServiceEndpointMethodInterceptor implements MethodInterceptor { 42 43 private final GenericServiceEndpoint stub; 44 private final OperationInfo[] operations; 45 private final String credentialsName; 46 47 public ServiceEndpointMethodInterceptor(GenericServiceEndpoint stub, OperationInfo[] operations, String credentialsName) { 48 this.stub = stub; 49 this.operations = operations; 50 this.credentialsName = credentialsName; 51 } 52 53 public Object intercept(Object o, Method method, Object [] objects, MethodProxy methodProxy) throws Throwable { 54 int index = methodProxy.getSuperIndex(); 55 OperationInfo operationInfo = operations[index]; 56 if (operationInfo == null) { 57 throw new RuntimeException ("Operation not mapped: " + method.getName() + " index: " + index + "\n OperationInfos: " + Arrays.asList(operations)); 58 } 59 stub.checkCachedEndpoint(); 60 61 Call call = stub.createCall(); 62 63 operationInfo.prepareCall(call); 64 65 stub.setUpCall(call); 66 if (credentialsName != null) { 67 Subject subject = ContextManager.getNextCaller(); 68 if (subject == null) { 69 throw new IllegalStateException ("Subject missing but authentication turned on"); 70 } else { 71 Set creds = subject.getPrivateCredentials(NamedUsernamePasswordCredential.class); 72 boolean found = false; 73 for (Iterator iterator = creds.iterator(); iterator.hasNext();) { 74 NamedUsernamePasswordCredential namedUsernamePasswordCredential = (NamedUsernamePasswordCredential) iterator.next(); 75 if (credentialsName.equals(namedUsernamePasswordCredential.getName())) { 76 call.setUsername(namedUsernamePasswordCredential.getUsername()); 77 call.setPassword(new String (namedUsernamePasswordCredential.getPassword())); 78 found = true; 79 break; 80 } 81 } 82 if (!found) { 83 throw new IllegalStateException ("no NamedUsernamePasswordCredential found for name " + credentialsName); 84 } 85 } 86 } 87 Object response = null; 88 List parameterDescs = operationInfo.getOperationDesc().getParameters(); 89 Object [] unwrapped = extractFromHolders(objects, parameterDescs, operationInfo.getOperationDesc().getNumInParams()); 90 if (operationInfo.getOperationDesc().getMep() == OperationType.REQUEST_RESPONSE) { 91 try { 92 response = call.invoke(unwrapped); 93 } catch (RemoteException e) { 94 throw operationInfo.unwrapFault(e); 95 } 96 97 if (response instanceof RemoteException ) { 98 throw operationInfo.unwrapFault((RemoteException ) response); 99 } else { 100 stub.extractAttachments(call); 101 Map outputParameters = call.getOutputParams(); 102 putInHolders(outputParameters, objects, parameterDescs); 103 Class returnType = operationInfo.getOperationDesc().getReturnClass(); 104 if (response == null || returnType == null || returnType.isAssignableFrom(response.getClass())) { 106 return response; 107 } else { 108 return JavaUtils.convert(response, returnType); 109 } 110 } 111 } else if (operationInfo.getOperationDesc().getMep() == OperationType.ONE_WAY) { 112 call.invokeOneWay(unwrapped); 114 return null; 115 } else { 116 throw new RuntimeException ("Invalid messaging style: " + operationInfo.getOperationDesc().getMep()); 117 } 118 } 119 120 private Object [] extractFromHolders(Object [] objects, List parameterDescs, int inParameterCount) throws JavaUtils.HolderException { 121 if (objects.length != parameterDescs.size()) { 122 throw new IllegalArgumentException ("Mismatch parameter count: expected: " + parameterDescs.size() + ", actual: " + objects.length); 123 } 124 Object [] unwrapped = new Object [inParameterCount]; 125 int j = 0; 126 for (int i = 0; objects != null && i < objects.length; i++) { 127 Object parameter = objects[i]; 128 ParameterDesc parameterDesc = (ParameterDesc) parameterDescs.get(i); 129 130 if (parameterDesc.getMode() == ParameterDesc.INOUT) { 131 unwrapped[j++] = JavaUtils.getHolderValue((Holder ) parameter); 132 } else if (parameterDesc.getMode() == ParameterDesc.IN) { 133 unwrapped[j++] = parameter; 134 } 135 } 136 return unwrapped; 137 } 138 139 private void putInHolders(Map outputParameters, Object [] objects, List parameterDescs) throws JavaUtils.HolderException { 140 for (int i = 0; i < objects.length; i++) { 141 Object parameter = objects[i]; 142 ParameterDesc parameterDesc = (ParameterDesc) parameterDescs.get(i); 143 if ((parameterDesc.getMode() == ParameterDesc.INOUT) || 144 (parameterDesc.getMode() == ParameterDesc.OUT)) { 145 Object returned = outputParameters.get(parameterDesc.getQName()); 146 if (returned instanceof Holder ) { 147 returned = JavaUtils.getHolderValue(returned); 149 } 150 JavaUtils.setHolderValue((Holder ) parameter, returned); 151 } 152 } 153 } 154 155 } 156 | Popular Tags |