View Javadoc

1   package org.astrogrid.portal.utils.acting;
2   
3   import java.io.IOException;
4   import java.io.StringReader;
5   import java.util.List;
6   
7   import javax.xml.parsers.DocumentBuilder;
8   import javax.xml.parsers.DocumentBuilderFactory;
9   import javax.xml.parsers.ParserConfigurationException;
10  import javax.xml.parsers.SAXParser;
11  import javax.xml.parsers.SAXParserFactory;
12  
13  import org.apache.avalon.framework.parameters.Parameters;
14  import org.apache.cocoon.environment.Request;
15  import org.apache.cocoon.environment.Session;
16  import org.apache.log4j.BasicConfigurator;
17  import org.apache.log4j.Category;
18  import org.astrogrid.common.creator.Creator;
19  import org.astrogrid.common.creator.CreatorException;
20  import org.w3c.dom.Document;
21  import org.w3c.dom.Element;
22  import org.xml.sax.InputSource;
23  import org.xml.sax.SAXException;
24  
25  
26  public class ActionUtilsDefault implements ActionUtils {
27    private Category logger = Category.getInstance(getClass());
28    
29    static {
30      BasicConfigurator.configure();
31    }
32    
33    /***
34     * Do not allow creation of <code>ActionUtilsDefault</code>.
35     * @see org.astrogrid.datacenter.cocoon.action.ActionUtilsFactory#getActionUtils()
36     */
37    ActionUtilsDefault() {
38    }
39  
40    /***
41     * Get a request parameter given a parameter passed into the action via the sitemap.
42     * 
43     * @param param request parameter name
44     * @param defaultValue default value of request parameter name
45     * @param params sitemap parameters
46     * @param request request context object
47     * 
48     * @return value of request parameter
49     */
50    public String getRequestParameter(String param, String defaultValue, Parameters params, Request request) {
51      // Make sure XML string exists for validation purposes.
52      String value = request.getParameter(param);
53      if(value == null) {
54        value = defaultValue;
55      }
56      logger.debug("[getRequestParameter] value: " + value);
57  
58      return value;
59    }
60  
61    /***
62     * @see ActionUtils#getRequestParameter(String, String, Parameters, Request)
63     */
64    public String getRequestParameter(String param, Parameters params, Request request) {
65      return getRequestParameter(param, "", params, request);
66    }
67    
68    /***
69     * Get a session attribute given a attribute passed into the action via the sitemap.
70     * 
71     * @param param session attribute name
72     * @param defaultValue default value of session attribute name
73     * @param params sitemap parameters
74     * @param session session context object
75     * 
76     * @return value of session attribute
77     */
78    public String getSessionAttribute(String param, String defaultValue, Parameters params, Session session) {
79      // Make sure XML string exists for validation purposes.
80      String value = (String) session.getAttribute(param);
81      if(value == null) {
82        value = defaultValue;
83      }
84      logger.debug("[getSessionAttribute] value: " + value);
85  
86      return value;
87    }
88  
89    /***
90     * @see ActionUtils#getSessionAttribute(String, String, Parameters, Session)
91     */
92    public String getSessionAttribute(String param, Parameters params, Session session) {
93      return getSessionAttribute(param, "", params, session);
94    }
95    
96    public Object getSessionObject(String sitemapParam, String defaultValue, Parameters params, Session session) {
97      Object result = defaultValue;
98      
99      if(session != null) {
100       Object attribute = session.getAttribute(sitemapParam);
101       if(attribute != null) {
102         result = attribute;
103       }
104     }
105     
106     return result;
107   }
108   
109   public Object getSessionObject(String sitemapParam, Parameters params, Session session) {
110     return getSessionObject(sitemapParam, null, params, session);
111   }
112 
113   public String getAnyParameter(String param, Parameters params, Request request, Session session) {
114     return getAnyParameter(param, "", params, request, session);
115   }
116   
117   public String getAnyParameter(String param, String defaultValue, Parameters params, Request request, Session session) {
118     String result = "";
119     
120     // Try a request parameter first (if valid request).
121     if(request != null) {
122       result = getRequestParameter(param, params, request);
123       if(result != null && result.length() > 0) {
124         logger.info("[getAnyParameter] REQUEST: [" + param + "," + result + "]");
125         return result;
126       }
127     }
128     
129     // Try a session attribute next (if valid session).
130     if(session != null) {
131       result = getSessionAttribute(param, params, session);
132       if(result != null && result.length() > 0) {
133         logger.info("[getAnyParameter] SESSION: [" + param + "," + result + "]");
134         return result;
135       }
136     }
137     
138     // Get either sitemap parameter or the default value.
139     result = params.getParameter(param, defaultValue);
140     logger.info("[getAnyParameter] SITEMAP: [" + param + "," + result + "]");
141     return result;
142   }
143 
144   public Object getAnyParameterObject(String sitemapParam, String defaultValue, Parameters params, Request request, Session session) {
145     Object result = defaultValue;
146     
147     Object sessionObject = getSessionObject(sitemapParam, params, session);
148     if(sessionObject == null) {
149       result = getAnyParameterObject(sitemapParam, params, request, session);
150     }
151     else {
152       result = sessionObject;
153     }
154     
155     return result;
156   }
157 
158   public Object getAnyParameterObject(String sitemapParam, Parameters params, Request request, Session session) {
159     return getAnyParameterObject(sitemapParam, null, params, request, session);
160   }
161   
162   /***  
163    * @see ActionUtils#getNewObject(String, String, Parameters, Object[])
164    */
165   public Object getNewObject(String sitemapParam, Parameters params, Request request, List args) throws CreatorException {
166     return getNewObject(sitemapParam, "", params, request, args);
167   }
168   
169   /***
170    * Get a new instance of a class name given in the sitemap parameters.
171    * 
172    * @see org.astrogrid.common.creator.Creator
173    * 
174    * @param sitemapParam sitemap parameter containing the class name
175    * @param defaultClassName default class name to use
176    * @param params sitemap parameters
177    * @param request request context object
178    * @param args constructor arguments
179    * 
180    * @return new instance
181    */
182   public Object getNewObject(String sitemapParam, String defaultClassName, Parameters params, Request request, List args) throws CreatorException {
183     Object result = null;
184     
185     Object[] realArgs = null;
186     if(args != null && args.size() > 0) {
187       realArgs = args.toArray();
188     }
189     
190     String className = params.getParameter(sitemapParam, defaultClassName);
191     
192     Creator creator = new Creator();
193     result = creator.newInstance(className, realArgs);
194     
195     return result;
196   }
197   
198   public Element getDomElement(String adql) throws ParserConfigurationException, SAXException, IOException {
199     Element result = null;
200     
201     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
202     factory.setNamespaceAware(shouldBeNSAware(adql));
203     factory.setValidating(shouldValidate(adql));
204     
205     StringReader reader = new StringReader(adql);
206     try {
207       InputSource is = new InputSource(reader);
208       DocumentBuilder builder = factory.newDocumentBuilder();
209       Document document = builder.parse(is);
210       
211       result = document.getDocumentElement();
212     }
213     catch(ParserConfigurationException e) {
214       logger.debug("[getDomElement] exception: " + e.getMessage());
215       throw e;
216     }
217     catch(SAXException e) {
218       logger.debug("[getDomElement] exception: " + e.getMessage());
219       throw e;
220     }
221     catch(IOException e) {
222       logger.debug("[getDomElement] exception: " + e.getMessage());
223       throw e;
224     }
225     finally {
226       reader.close();
227     }
228     
229     return result;
230   }
231 
232   public ValidationHandler validate(String xml) {
233     ValidationHandler handler = new ValidationHandler();
234 
235     logger.debug("[validate] xml:      " + xml);
236     logger.debug("[validate] validate: " + shouldValidate(xml));
237     logger.debug("[validate] ns aware: " + shouldBeNSAware(xml));
238 
239     SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
240     saxParserFactory.setValidating(shouldValidate(xml));
241     saxParserFactory.setNamespaceAware(shouldBeNSAware(xml));
242 
243     StringReader reader = new StringReader(xml);
244     try {
245       InputSource is = new InputSource(reader);
246       
247       SAXParser saxParser = saxParserFactory.newSAXParser();
248       saxParser.getXMLReader().setErrorHandler(handler);
249       saxParser.parse(is, handler);
250     }
251     catch(ParserConfigurationException e) {
252       logger.debug("[validate] exception: " + e.getMessage());
253       handler.exception(e);
254     }
255     catch(SAXException e) {
256       logger.debug("[validate] exception: " + e.getMessage());
257       handler.exception(e);
258     }
259     catch(IOException e) {
260       logger.debug("[validate] exception: " + e.getMessage());
261       handler.exception(e);
262     }
263     finally {
264       reader.close();
265     }
266     
267     return handler;
268   }
269   
270   public boolean shouldValidate(String xml) {
271     return ( xml.indexOf("<!DOCTYPE") > -1 ||
272              xml.indexOf(":schemaLocation") > -1 );
273   }
274   
275   public boolean shouldBeNSAware(String xml) {
276     return xml.indexOf("xmlns:") > -1;
277   }
278 }