KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > extensions > ExtensionParser


1 package com.sslexplorer.extensions;
2
3 import java.io.ByteArrayOutputStream JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Map JavaDoc;
9 import java.util.Properties JavaDoc;
10
11 import javax.servlet.http.HttpServletRequest JavaDoc;
12
13 import org.jdom.Element;
14 import org.jdom.JDOMException;
15 import org.jdom.output.XMLOutputter;
16
17 import com.sslexplorer.boot.PropertyDefinition;
18 import com.sslexplorer.core.CoreException;
19 import com.sslexplorer.core.stringreplacement.VariableReplacement;
20 import com.sslexplorer.extensions.actions.UndefinedParameterException;
21 import com.sslexplorer.extensions.store.ExtensionStore;
22 import com.sslexplorer.policyframework.LaunchSession;
23 import com.sslexplorer.security.SessionInfo;
24
25 /**
26  * Utilities for processing extension descriptor XML for return to the agent
27  * launcher or agent itself.
28  *
29  * @author Lee David Painter <a HREF="mailto: lee@3sp.com">&lt;lee@3sp.com&gt;</a>
30  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
31  */

32 public class ExtensionParser {
33
34     /**
35      * Process an agent descriptor, replacing all replaceable content. No
36      * <i>Launch Session</i> is required for launching the agent.
37      *
38      * @param des agent descriptor
39      * @param request request
40      * @param session session
41      * @param properties parameters
42      * @return processed descriptor XML
43      * @throws UndefinedParameterException
44      * @throws JDOMException
45      * @throws IOException
46      * @throws CoreException
47      */

48     public static String JavaDoc processAgentParameters(ExtensionDescriptor des, HttpServletRequest JavaDoc request, SessionInfo session,
49                                                 Properties JavaDoc properties) throws UndefinedParameterException, JDOMException,
50                     IOException JavaDoc, CoreException {
51         try {
52             Element element = des.createProcessedDescriptorElement(session);
53             VariableReplacement r = new VariableReplacement();
54             r.setApplicationShortcut(des, null);
55             r.setSession(session);
56             r.setServletRequest(request);
57             Map JavaDoc<String JavaDoc, String JavaDoc> parameterValues = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
58             for(Iterator JavaDoc i = request.getParameterMap().keySet().iterator(); i.hasNext(); ) {
59                 String JavaDoc key = (String JavaDoc)i.next();
60                 parameterValues.put(key, request.getParameter(key));
61             }
62             return getXML(element, parameterValues, r, des);
63         } catch (Exception JavaDoc e) {
64             e.printStackTrace();
65             throw new CoreException(0, "");
66         }
67
68     }
69
70     /**
71      * Process an <i>Application Extension</i>'s descriptor , replacing all
72      * replaceable content. A <i>Launch Session</i> is required for launching
73      * any applications.
74      *
75      * @param launchSession launch session
76      * @param properties parameters
77      * @param parameterValues parameter values
78      * @param applicationId
79      * @return processed descriptor XML
80      * @throws Exception
81      * @throws Exception on any error
82      */

83     public static String JavaDoc processApplicationParameters(LaunchSession launchSession, Properties JavaDoc properties, Map JavaDoc<String JavaDoc, String JavaDoc> parameterValues,
84                                                         String JavaDoc applicationId) throws Exception JavaDoc {
85         ExtensionDescriptor des = ExtensionStore.getInstance().getExtensionDescriptor(applicationId);
86         Element element = des.createProcessedDescriptorElement(launchSession.getSession());
87         VariableReplacement r = new VariableReplacement();
88         r.setApplicationShortcut(des, null);
89         r.setLaunchSession(launchSession);
90         // TODO different way of getting 'request' parameters when no servlet request is available
91
return getXML(element, parameterValues, r, des);
92     }
93
94     static String JavaDoc getXML(Element element, Map JavaDoc<String JavaDoc, String JavaDoc> parameterValues, VariableReplacement replacer,
95                             ExtensionDescriptor des) throws UndefinedParameterException, IOException JavaDoc {
96
97         List JavaDoc params = element.getChildren("parameter");
98
99         for (Iterator JavaDoc it = params.iterator(); it.hasNext();) {
100             Element p = (Element) it.next();
101
102             String JavaDoc name = p.getAttribute("name").getValue();
103             String JavaDoc value = (String JavaDoc) parameterValues.get(name);
104
105             if (value == null) {
106                 value = des.getParameterDefinition(name).getDefaultValue();
107                 if (value == null || value.equals(PropertyDefinition.UNDEFINED_PARAMETER)) {
108                     throw new UndefinedParameterException("Parameter " + name + " is undefined");
109                 }
110             }
111             p.setAttribute("value", replacer.replace(value));
112         }
113
114         XMLOutputter output = new XMLOutputter();
115         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
116         output.output(element, out);
117         String JavaDoc xml = new String JavaDoc(out.toByteArray());
118
119         params = element.getChildren("parameter");
120
121         Map JavaDoc<String JavaDoc, String JavaDoc> parameters = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
122         for (Iterator JavaDoc it = params.iterator(); it.hasNext();) {
123             Element p = (Element) it.next();
124             parameters.put(p.getAttributeValue("name"), p.getAttributeValue("value"));
125         }
126         replacer.setApplicationShortcut(des, parameters);
127         return replacer.replace(xml);
128     }
129 }
130
Popular Tags