View Javadoc

1   package org.astrogrid.security;
2   
3   import java.util.Map;
4   import javax.security.auth.Subject;
5   import javax.xml.namespace.QName;
6   import javax.xml.rpc.handler.GenericHandler;
7   import javax.xml.rpc.handler.HandlerInfo;
8   import javax.xml.rpc.handler.MessageContext;
9   import javax.xml.rpc.handler.soap.SOAPMessageContext;
10  import javax.xml.rpc.JAXRPCException;
11  import javax.xml.soap.SOAPMessage;
12  
13  
14  /***
15   * A JAX-RPC handler for security credentials.
16   *
17   * This is a generic class that defines things common
18   * to client-side and server-side handers.
19   *
20   * @author Guy Rixon
21   */
22  public abstract class CredentialHandler extends GenericHandler {
23  
24    /***
25     * The object through which the security parameters can be got.
26     */
27    protected Subject subject;
28  
29  
30    /***
31     * Recovers the JAAS Subject resource from the configuration.
32     *
33     * @param i configuration data for the handler, including the Subject.
34     * @throws JAXRPCException if the configuration is invalid
35     */
36    public final void init (final HandlerInfo i) {
37      Map m = i.getHandlerConfig();
38      Object s = m.get("Subject");
39      assert(s != null);
40      assert(Subject.class.isInstance(s));
41      this.subject = (Subject)s;
42    }
43  
44    /***
45     * Lists the header blocks processed by this handler.
46     * Only one type is handled: the identity header.
47     *
48     * @return the list of names of header blocks.
49     */
50    public QName[] getHeaders () {
51      return new QName[] {WsseHeaderElement.getName()};
52    }
53  
54  
55    /***
56     * Extracts the SOAP message from the JAX-RPC context.
57     * This might fail if this class is used for a service
58     * other than a SOAP service.
59     *
60     * @param mc the message context holding the message
61     * @return the message
62     * @throws JAXRPC Exception if no SOAP message is available
63     */
64    protected final SOAPMessage getMessage (final MessageContext mc) {
65      SOAPMessage sm = null;
66      try {
67        SOAPMessageContext smc = (SOAPMessageContext) mc;
68        sm = smc.getMessage();
69      }
70      catch (Exception e) {
71        // Downcast failed; i.e. not a SOAP message.
72        throw new JAXRPCException("Invalid use of JAX-RPC: " +
73                                  "the handler was invoked for a MessageContext " +
74                                  "that is not a SOAPMessage context.");
75      }
76      if (sm == null) {
77        // SOAP context but no message present.
78        throw new JAXRPCException("Invalid use of JAX-RPC: " +
79                                  "the handler was invoked when there was " +
80                                  "no message to handle.");
81      }
82      return sm;
83    }
84  
85  }