Save This Page
Home » neethi-2.0.4-src » org.apache » neethi » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one
    3    * or more contributor license agreements. See the NOTICE file
    4    * distributed with this work for additional information
    5    * regarding copyright ownership. The ASF licenses this file
    6    * to you under the Apache License, Version 2.0 (the
    7    * "License"); you may not use this file except in compliance
    8    * with the License. You may obtain a copy of the License at
    9    *
   10    * http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    * Unless required by applicable law or agreed to in writing,
   13    * software distributed under the License is distributed on an
   14    * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15    * KIND, either express or implied. See the License for the
   16    * specific language governing permissions and limitations
   17    * under the License.
   18    */
   19   
   20   package org.apache.neethi;
   21   
   22   import java.io.File;
   23   import java.io.FileOutputStream;
   24   import java.io.IOException;
   25   import java.io.PrintStream;
   26   import java.net.MalformedURLException;
   27   import java.net.URL;
   28   import java.net.URLConnection;
   29   
   30   import javax.xml.stream.XMLInputFactory;
   31   import javax.xml.stream.XMLStreamException;
   32   import javax.xml.stream.XMLStreamReader;
   33   import javax.xml.stream.XMLStreamWriter;
   34   
   35   import org.apache.axiom.om.OMElement;
   36   import org.apache.axiom.om.impl.builder.StAXOMBuilder;
   37   
   38   /**
   39    * PolicyReference is a wrapper that holds explict PolicyReferences.
   40    */
   41   public class PolicyReference implements PolicyComponent {
   42   
   43       private String uri;
   44   
   45       /**
   46        * Sets the Policy URI
   47        * @param uri the Policy URI
   48        */
   49       public void setURI(String uri) {
   50           this.uri = uri;
   51       }
   52   
   53       /**
   54        * Gets the Policy URI that is refered by self.
   55        * @return a String that is the Policy URI refered by self
   56        */
   57       public String getURI() {
   58           return uri;
   59       }
   60   
   61       public boolean equal(PolicyComponent policyComponent) {
   62           if (Constants.TYPE_POLICY_REF != policyComponent.getType()) {
   63               return false;
   64           }
   65           
   66           String URI = ((PolicyReference) policyComponent).getURI();
   67           if (URI != null && URI.length() != 0) {
   68               return URI.equals(this.uri);
   69           } 
   70           
   71           return false;
   72       }
   73   
   74   
   75       /**
   76        * Returns short value of Constants.TYPE_POLICY_REF 
   77        */
   78       public short getType() {
   79           return Constants.TYPE_POLICY_REF;
   80       }
   81   
   82       /**
   83        * Throws an UnsupportedOperationException since PolicyReference.normalize()
   84        * can't resolve the Policy that it refers to unless a PolicyRegistry is
   85        * provided.
   86        */
   87       public PolicyComponent normalize() {
   88           throw new UnsupportedOperationException("PolicyReference.normalize() is meaningless");
   89       }
   90       
   91       /**
   92        * Returns normalized version of the Policy that is refered by self. The specified 
   93        * PolicyRegistry is used to lookup for the Policy that is refered and <tt>dee</tt> 
   94        * indicates the level of normalization fo the returning Policy.
   95        * 
   96        * @param reg the PolicyRegistry that is used to resolved the Policy refered by self
   97        * @param deep the falg to indicate whether returning Policy should be fully normailized
   98        * @return the normalized version fo the Policy refered by self
   99        */
  100       public PolicyComponent normalize(PolicyRegistry reg, boolean deep) {
  101           String key = getURI();
  102           int pos = key.indexOf("#");
  103           if (pos == 0) {
  104               key = key.substring(1);
  105           }else if(pos > 0){
  106           	key = key.substring(0, pos);
  107           }
  108           
  109           Policy policy = reg.lookup(key);        
  110           
  111           if (policy == null) {
  112           	policy = getRemoteReferencedPolicy(key);
  113           	
  114           	if(policy == null){
  115           		throw new RuntimeException(key + " can't be resolved" );
  116           	}
  117           	
  118           	reg.register(key, policy);
  119               
  120           }
  121           
  122           return policy.normalize(reg, deep);
  123       }
  124   
  125       public void serialize(XMLStreamWriter writer) throws XMLStreamException {
  126   
  127           String wspPrefix = writer.getPrefix(Constants.URI_POLICY_NS);
  128           
  129           if (wspPrefix == null) {
  130               wspPrefix = Constants.ATTR_WSP;
  131               writer.setPrefix(wspPrefix, Constants.URI_POLICY_NS);
  132           }
  133           
  134           writer.writeStartElement(wspPrefix, Constants.ELEM_POLICY_REF, Constants.URI_POLICY_NS);
  135           writer.writeNamespace(Constants.ATTR_WSP, Constants.URI_POLICY_NS);
  136           writer.writeAttribute(Constants.ATTR_URI, getURI());
  137           
  138           writer.writeEndElement();
  139       }
  140       
  141       public OMElement getRemoteReferedElement(String uri){
  142       	OMElement documentElement = null;
  143       	
  144       	try{    		    		
  145       		
  146   	    	//create java.net URL pointing to remote resource			
  147   			URL url = new URL(uri);
  148   			URLConnection connection = url.openConnection();
  149   			connection.setDoInput(true);
  150   			
  151   			//create stax parser with the url content
  152   			XMLStreamReader parser = XMLInputFactory.newInstance().
  153   			                                         createXMLStreamReader(connection.getInputStream()); 
  154   			
  155   	        //get the root element (in this case the envelope)
  156   			StAXOMBuilder builder = new StAXOMBuilder(parser); 
  157   			documentElement = builder.getDocumentElement();	
  158   			
  159           }catch(XMLStreamException se){        	        	
  160           	throw new RuntimeException("Bad policy content: " + uri);
  161           }catch(MalformedURLException mue){        	
  162           	throw new RuntimeException("Malformed uri: " + uri);
  163           }catch(IOException ioe){        
  164           	throw new RuntimeException("Cannot reach remote resource: " + uri);
  165           }       
  166   		
  167   		return documentElement;
  168       }
  169       
  170       
  171       
  172       public Policy getRemoteReferencedPolicy(String uri) {
  173       	Policy policy = null;
  174       	
  175       	//extract the remote resource contents
  176       	OMElement policyElement = getRemoteReferedElement(uri);
  177       	
  178       	//call the policy engine with already extracted content
  179       	policy = PolicyEngine.getPolicy(policyElement);
  180       	
  181   		return policy;
  182       }
  183   }

Save This Page
Home » neethi-2.0.4-src » org.apache » neethi » [javadoc | source]