All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jcp.xml.dsig.internal.dom.DOMRetrievalMethod Maven / Gradle / Ivy

The newest version!
/*     */ package org.jcp.xml.dsig.internal.dom;
/*     */ 
/*     */ import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput;
/*     */ import java.io.ByteArrayInputStream;
/*     */ import java.net.URI;
/*     */ import java.net.URISyntaxException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Collections;
/*     */ import java.util.List;
/*     */ import javax.xml.crypto.Data;
/*     */ import javax.xml.crypto.MarshalException;
/*     */ import javax.xml.crypto.URIDereferencer;
/*     */ import javax.xml.crypto.URIReferenceException;
/*     */ import javax.xml.crypto.XMLCryptoContext;
/*     */ import javax.xml.crypto.XMLStructure;
/*     */ import javax.xml.crypto.dom.DOMCryptoContext;
/*     */ import javax.xml.crypto.dom.DOMURIReference;
/*     */ import javax.xml.crypto.dsig.Transform;
/*     */ import javax.xml.crypto.dsig.keyinfo.RetrievalMethod;
/*     */ import javax.xml.parsers.DocumentBuilder;
/*     */ import javax.xml.parsers.DocumentBuilderFactory;
/*     */ import org.w3c.dom.Attr;
/*     */ import org.w3c.dom.Document;
/*     */ import org.w3c.dom.Element;
/*     */ import org.w3c.dom.Node;
/*     */ 
/*     */ public final class DOMRetrievalMethod extends DOMStructure
/*     */   implements RetrievalMethod, DOMURIReference
/*     */ {
/*     */   private final List transforms;
/*     */   private String uri;
/*     */   private String type;
/*     */   private Attr here;
/*     */ 
/*     */   public DOMRetrievalMethod(String uri, String type, List transforms)
/*     */   {
/*  65 */     if (uri == null) {
/*  66 */       throw new NullPointerException("uri cannot be null");
/*     */     }
/*  68 */     if ((transforms == null) || (transforms.isEmpty())) {
/*  69 */       this.transforms = Collections.EMPTY_LIST;
/*     */     } else {
/*  71 */       List transformsCopy = new ArrayList(transforms);
/*  72 */       int i = 0; for (int size = transformsCopy.size(); i < size; i++) {
/*  73 */         if (!(transformsCopy.get(i) instanceof Transform)) {
/*  74 */           throw new ClassCastException("transforms[" + i + "] is not a valid type");
/*     */         }
/*     */       }
/*     */ 
/*  78 */       this.transforms = Collections.unmodifiableList(transformsCopy);
/*     */     }
/*  80 */     this.uri = uri;
/*  81 */     if ((uri != null) && (!uri.equals(""))) {
/*     */       try {
/*  83 */         new URI(uri);
/*     */       } catch (URISyntaxException e) {
/*  85 */         throw new IllegalArgumentException(e.getMessage());
/*     */       }
/*     */     }
/*     */ 
/*  89 */     this.type = type;
/*     */   }
/*     */ 
/*     */   public DOMRetrievalMethod(Element rmElem, XMLCryptoContext context)
/*     */     throws MarshalException
/*     */   {
/* 100 */     this.uri = DOMUtils.getAttributeValue(rmElem, "URI");
/* 101 */     this.type = DOMUtils.getAttributeValue(rmElem, "Type");
/*     */ 
/* 104 */     this.here = rmElem.getAttributeNodeNS(null, "URI");
/*     */ 
/* 107 */     List transforms = new ArrayList();
/* 108 */     Element transformsElem = DOMUtils.getFirstChildElement(rmElem);
/* 109 */     if (transformsElem != null) {
/* 110 */       Element transformElem = DOMUtils.getFirstChildElement(transformsElem);
/*     */ 
/* 112 */       while (transformElem != null) {
/* 113 */         transforms.add(new DOMTransform(transformElem, context));
/* 114 */         transformElem = DOMUtils.getNextSiblingElement(transformElem);
/*     */       }
/*     */     }
/* 117 */     if (transforms.isEmpty())
/* 118 */       this.transforms = Collections.EMPTY_LIST;
/*     */     else
/* 120 */       this.transforms = Collections.unmodifiableList(transforms);
/*     */   }
/*     */ 
/*     */   public String getURI()
/*     */   {
/* 125 */     return this.uri;
/*     */   }
/*     */ 
/*     */   public String getType() {
/* 129 */     return this.type;
/*     */   }
/*     */ 
/*     */   public List getTransforms() {
/* 133 */     return this.transforms;
/*     */   }
/*     */ 
/*     */   public void marshal(Node parent, String dsPrefix, DOMCryptoContext context) throws MarshalException
/*     */   {
/* 138 */     Document ownerDoc = DOMUtils.getOwnerDocument(parent);
/*     */ 
/* 140 */     Element rmElem = DOMUtils.createElement(ownerDoc, "RetrievalMethod", "http://www.w3.org/2000/09/xmldsig#", dsPrefix);
/*     */ 
/* 144 */     DOMUtils.setAttribute(rmElem, "URI", this.uri);
/* 145 */     DOMUtils.setAttribute(rmElem, "Type", this.type);
/*     */ 
/* 148 */     if (!this.transforms.isEmpty()) {
/* 149 */       Element transformsElem = DOMUtils.createElement(ownerDoc, "Transforms", "http://www.w3.org/2000/09/xmldsig#", dsPrefix);
/*     */ 
/* 151 */       rmElem.appendChild(transformsElem);
/* 152 */       int i = 0; for (int size = this.transforms.size(); i < size; i++) {
/* 153 */         ((DOMTransform)this.transforms.get(i)).marshal(transformsElem, dsPrefix, context);
/*     */       }
/*     */ 
/*     */     }
/*     */ 
/* 158 */     parent.appendChild(rmElem);
/*     */ 
/* 161 */     this.here = rmElem.getAttributeNodeNS(null, "URI");
/*     */   }
/*     */ 
/*     */   public Node getHere() {
/* 165 */     return this.here;
/*     */   }
/*     */ 
/*     */   public Data dereference(XMLCryptoContext context)
/*     */     throws URIReferenceException
/*     */   {
/* 171 */     if (context == null) {
/* 172 */       throw new NullPointerException("context cannot be null");
/*     */     }
/*     */ 
/* 179 */     URIDereferencer deref = context.getURIDereferencer();
/* 180 */     if (deref == null) {
/* 181 */       deref = DOMURIDereferencer.INSTANCE;
/*     */     }
/*     */ 
/* 184 */     Data data = deref.dereference(this, context);
/*     */     try
/*     */     {
/* 188 */       int i = 0; for (int size = this.transforms.size(); i < size; i++) {
/* 189 */         Transform transform = (Transform)this.transforms.get(i);
/* 190 */         data = ((DOMTransform)transform).transform(data, context);
/*     */       }
/*     */     } catch (Exception e) {
/* 193 */       throw new URIReferenceException(e);
/*     */     }
/* 195 */     return data;
/*     */   }
/*     */ 
/*     */   public XMLStructure dereferenceAsXMLStructure(XMLCryptoContext context) throws URIReferenceException
/*     */   {
/*     */     try
/*     */     {
/* 202 */       ApacheData data = (ApacheData)dereference(context);
/* 203 */       DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
/* 204 */       dbf.setNamespaceAware(true);
/* 205 */       DocumentBuilder db = dbf.newDocumentBuilder();
/* 206 */       Document doc = db.parse(new ByteArrayInputStream(data.getXMLSignatureInput().getBytes()));
/*     */ 
/* 208 */       Element kiElem = doc.getDocumentElement();
/* 209 */       if (kiElem.getLocalName().equals("X509Data")) {
/* 210 */         return new DOMX509Data(kiElem);
/*     */       }
/* 212 */       return null;
/*     */     }
/*     */     catch (Exception e) {
/* 215 */       throw new URIReferenceException(e);
/*     */     }
/*     */   }
/*     */ 
/*     */   public boolean equals(Object obj) {
/* 220 */     if (this == obj) {
/* 221 */       return true;
/*     */     }
/* 223 */     if (!(obj instanceof DOMURIReference)) {
/* 224 */       return false;
/*     */     }
/* 226 */     RetrievalMethod orm = (DOMURIReference)obj;
/*     */ 
/* 228 */     boolean typesEqual = this.type == null ? false : orm.getType() == null ? true : this.type.equals(orm.getType());
/*     */ 
/* 231 */     return (this.uri.equals(orm.getURI())) && (this.transforms.equals(orm.getTransforms())) && (typesEqual);
/*     */   }
/*     */ }

/* Location:           E:\HYN\Java\trunk\ref\lib-dep\xmldsig\xmldsig.jar
 * Qualified Name:     org.jcp.xml.dsig.internal.dom.DOMRetrievalMethod
 * JD-Core Version:    0.6.2
 */




© 2015 - 2024 Weber Informatics LLC | Privacy Policy