1 package com.sslexplorer.extensions; 2 3 import java.io.ByteArrayOutputStream ; 4 import java.io.IOException ; 5 import java.util.HashMap ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 import java.util.Map ; 9 import java.util.Properties ; 10 11 import javax.servlet.http.HttpServletRequest ; 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 32 public class ExtensionParser { 33 34 48 public static String processAgentParameters(ExtensionDescriptor des, HttpServletRequest request, SessionInfo session, 49 Properties properties) throws UndefinedParameterException, JDOMException, 50 IOException , 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 <String , String > parameterValues = new HashMap <String , String >(); 58 for(Iterator i = request.getParameterMap().keySet().iterator(); i.hasNext(); ) { 59 String key = (String )i.next(); 60 parameterValues.put(key, request.getParameter(key)); 61 } 62 return getXML(element, parameterValues, r, des); 63 } catch (Exception e) { 64 e.printStackTrace(); 65 throw new CoreException(0, ""); 66 } 67 68 } 69 70 83 public static String processApplicationParameters(LaunchSession launchSession, Properties properties, Map <String , String > parameterValues, 84 String applicationId) throws Exception { 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 return getXML(element, parameterValues, r, des); 92 } 93 94 static String getXML(Element element, Map <String , String > parameterValues, VariableReplacement replacer, 95 ExtensionDescriptor des) throws UndefinedParameterException, IOException { 96 97 List params = element.getChildren("parameter"); 98 99 for (Iterator it = params.iterator(); it.hasNext();) { 100 Element p = (Element) it.next(); 101 102 String name = p.getAttribute("name").getValue(); 103 String value = (String ) 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 out = new ByteArrayOutputStream (); 116 output.output(element, out); 117 String xml = new String (out.toByteArray()); 118 119 params = element.getChildren("parameter"); 120 121 Map <String , String > parameters = new HashMap <String , String >(); 122 for (Iterator 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 |