KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > servlet > DefaultApplyXSLTProperties


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: DefaultApplyXSLTProperties.java,v 1.13 2004/02/17 19:13:22 minchau Exp $
18  */

19 package servlet;
20
21 import java.net.*;
22 import javax.servlet.*;
23 import javax.servlet.http.*;
24 import java.util.Enumeration JavaDoc;
25 import java.util.Properties JavaDoc;
26
27 /*****************************************************************************************************
28  *
29  * DefaultApplyXSLTProperties contains operational parameters for DefaultApplyXSLT based
30  * on program defaults and configuration.
31  * <p>This class is also used to return values for request-time parameters.</p>
32  *
33  * @author Spencer Shepard (sshepard@us.ibm.com)
34  * @author R. Adam King (rak@us.ibm.com)
35  * @author Tom Rowe (trowe@us.ibm.com)
36  *
37  *****************************************************************************************************/

38
39 public class DefaultApplyXSLTProperties extends ApplyXSLTProperties {
40     
41     /**
42       * Program default for parameter "catalog".
43       * @see #getCatalog
44       */

45     private final String JavaDoc DEFAULT_catalog;
46
47     /**
48       * Host used for local context comparisons.
49       * @see #getLocalHost
50       * @see #setLocalHost
51       */

52     protected transient String JavaDoc localHost = null;
53     
54     /**
55      * Server port. Used in toSafeURL() -- fix submitted by Ritesh Kumar.
56      */

57     protected static int port =0;
58
59     /**
60       * Constructor to use program defaults.
61       */

62     public DefaultApplyXSLTProperties()
63     {
64     super();
65     DEFAULT_catalog = null;
66     setLocalHost();
67     // setSystemProperties();
68
}
69
70     /**
71       * Constructor to use to override program defaults.
72       * @param config Servlet configuration
73       * @see #setLocalHost
74       */

75     public DefaultApplyXSLTProperties(ServletConfig config)
76     {
77         super(config);
78         String JavaDoc cat = config.getInitParameter("catalog");
79         if (cat != null) DEFAULT_catalog = cat;
80         else DEFAULT_catalog = null;
81         setLocalHost();
82         setSystemProperties();
83     }
84
85     /**
86       * Sets the name of the local IP host name; this value will be used to constrain untrusted
87       * XML document and XSL stylesheet URLs to this trusted host.
88       * @see #getLocalHost
89       */

90     protected void setLocalHost()
91     {
92         try
93         {
94             localHost = InetAddress.getLocalHost().getHostName();
95         }
96         catch (Exception JavaDoc uhe)
97         {
98           localHost = null;
99         }
100     }
101
102     /**
103       * Returns the name of trusted IP host.
104       * @return Name of trusted host
105       * @see #setLocalHost
106       */

107     public String JavaDoc getLocalHost()
108     {
109         return localHost;
110     }
111
112     /**
113       * Returns a URL which is constrained to a trusted IP host.
114       * @param xURL URL or file path to be made safe
115       * @return Safe URL
116       * @exception MalformedURLException Thrown when xURL is not a valid URL
117       * @see #setLocalHost
118       * @see #getLocalHost
119       */

120     public URL toSafeURL(String JavaDoc xURL, HttpServletRequest request)
121     throws MalformedURLException
122     {
123       // Fix submitted by Ritesh Kumar. Port is included in construction of URL that is returned.
124
if (port == 0)
125         port = request.getServerPort();
126       
127         if (xURL == null)
128           return null;
129
130         if (xURL.startsWith("/"))
131       {
132           try
133         {
134               return new URL("http", localHost, port, xURL);
135           }
136         catch (MalformedURLException mue)
137         {
138             throw new MalformedURLException("toSafeURL(): " + xURL +
139                                                     " did not map to local");
140           }
141         }
142         URL tempURL = null;
143         try
144       {
145           tempURL = new URL(xURL);
146         }
147       catch (MalformedURLException mue)
148       {
149           throw new MalformedURLException("toSafeURL(): " + xURL +
150                                                   " not a valid URL");
151         }
152         try
153       {
154           return new URL(tempURL.getProtocol(), localHost,
155                              port, tempURL.getFile());
156         }
157       catch (MalformedURLException mue)
158       {
159           throw new MalformedURLException("toSafeURL(): " + xURL +
160                                                 " could not be converted to local host");
161         }
162     }
163
164     /**
165       * Returns a string representing the constrained URL for the XML document.
166       * If there is no request parameter for the XML document, return the configured default.
167       * @param request May contain an XML document URL parameter
168       * @return String form of XML URL
169       * @exception MalformedURLException Thrown when request URL is not a valid URL or path
170       * @see #toSafeURL
171       */

172     public String JavaDoc getXMLurl(HttpServletRequest request)
173     throws MalformedURLException
174     {
175         URL url = toSafeURL(getRequestParmString(request, "URL"),request);
176         if (url == null)
177           return super.getXMLurl(null);
178         return url.toExternalForm();
179     }
180
181     /**
182       * Returns a string representing the constrained URL for the XSL stylesheet
183       * from the request.
184       * @param request May contain an XSL stylesheet URL parameter
185       * @return String form of request XSL URL, or null if request contains no xslURL parameter
186       * @exception MalformedURLException Thrown when request URL is not a valid URL or path
187       * @see #toSafeURL
188       */

189     public String JavaDoc getXSLRequestURL(HttpServletRequest request)
190     throws MalformedURLException
191     {
192         URL url = toSafeURL(getRequestParmString(request, "xslURL"),request);
193         if (url == null)
194             return null;
195         return url.toExternalForm();
196     }
197
198     /**
199       * Returns a string representing the constrained request URL for the XSL stylesheet.
200       * If there is no request parameter for the XSL stylesheet, return the configured default.
201       * @param request May contain an XSL stylesheet URL parameter
202       * @return String form of XSL URL
203       * @exception MalformedURLException Thrown when request URL is not a valid URL or path
204       * @see #toSafeURL
205       */

206     public String JavaDoc getXSLurl(HttpServletRequest request)
207     throws MalformedURLException
208     {
209         String JavaDoc reqURL = getXSLRequestURL(request);
210         if (reqURL != null)
211             return reqURL;
212         URL url = toSafeURL(super.getXSLurl(null), request);
213         return url.toExternalForm();
214     }
215
216     /**
217       * Returns URLs for all <a HREF="http://www.ccil.org/~cowan/XML/XCatalog.html">XCatalogs</a>
218       * that are to be used to process the request. Catalogs are used to resolve XML public identifiers
219       * into system identifiers.
220       * <p>A single XCatalog can be configured as a default,
221       * but multiple XCatalogs can be specified at request time to augment the configured default.
222       * @param request May contain one or more XCatalog parameters
223       * @return Array of strings for all catalog URLs
224       */

225     public String JavaDoc[] getCatalog(HttpServletRequest request)
226     {
227         String JavaDoc temp[] = request.getParameterValues("catalog");
228         if (DEFAULT_catalog == null)
229             return temp;
230         if (temp == null)
231       {
232           String JavaDoc defaultArray[] = new String JavaDoc [1];
233           defaultArray[0] = DEFAULT_catalog;
234           return defaultArray;
235         }
236         int i, len = temp.length + 1;
237         String JavaDoc newCatalogs[] = new String JavaDoc[len];
238         newCatalogs[0] = DEFAULT_catalog;
239         for (i=1; i < len; i++)
240       {
241           newCatalogs[i] = temp[i-1];
242         }
243         return newCatalogs;
244     }
245     
246      /**
247    * I think we no longer need this. Sets the 3 jaxp core system properties.
248    */

249     protected void setSystemProperties()
250     {
251       Properties JavaDoc props = new Properties JavaDoc();
252     props.put("javax.xml.transform.TransformerFactory",
253               "org.apache.xalan.processor.TransformerFactoryImpl");
254     props.put("javax.xml.parsers.DocumentBuilderFactory",
255               "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
256     props.put("javax.xml.parsers.SAXParserFactory",
257               "org.apache.xerces.jaxp.SAXParserFactoryImpl");
258     
259       Properties JavaDoc systemProps = System.getProperties();
260       Enumeration JavaDoc propEnum = props.propertyNames();
261       while(propEnum.hasMoreElements())
262       {
263         String JavaDoc prop = (String JavaDoc)propEnum.nextElement();
264         if(!systemProps.containsKey(prop))
265           systemProps.put(prop, props.getProperty(prop));
266       }
267       System.setProperties(systemProps);
268     }
269
270 }
271
Popular Tags