1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* $Id: DefaultApplyXSLTProperties.java 470245 2006-11-02 06:34:33Z minchau $
*/
package servlet;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Enumeration;
import java.util.Properties;
/*****************************************************************************************************
*
* DefaultApplyXSLTProperties contains operational parameters for DefaultApplyXSLT based
* on program defaults and configuration.
* <p>This class is also used to return values for request-time parameters.</p>
*
* @author Spencer Shepard (sshepard@us.ibm.com)
* @author R. Adam King (rak@us.ibm.com)
* @author Tom Rowe (trowe@us.ibm.com)
*
*****************************************************************************************************/
public class DefaultApplyXSLTProperties extends ApplyXSLTProperties {
/**
* Program default for parameter "catalog".
* @see #getCatalog
*/
private final String DEFAULT_catalog;
/**
* Host used for local context comparisons.
* @see #getLocalHost
* @see #setLocalHost
*/
protected transient String localHost = null;
/**
* Server port. Used in toSafeURL() -- fix submitted by Ritesh Kumar.
*/
protected static int port =0;
/**
* Constructor to use program defaults.
*/
public DefaultApplyXSLTProperties()
{
super();
DEFAULT_catalog = null;
setLocalHost();
// setSystemProperties();
}
/**
* Constructor to use to override program defaults.
* @param config Servlet configuration
* @see #setLocalHost
*/
public DefaultApplyXSLTProperties(ServletConfig config)
{
super(config);
String cat = config.getInitParameter("catalog");
if (cat != null) DEFAULT_catalog = cat;
else DEFAULT_catalog = null;
setLocalHost();
setSystemProperties();
}
/**
* Sets the name of the local IP host name; this value will be used to constrain untrusted
* XML document and XSL stylesheet URLs to this trusted host.
* @see #getLocalHost
*/
protected void setLocalHost()
{
try
{
localHost = InetAddress.getLocalHost().getHostName();
}
catch (Exception uhe)
{
localHost = null;
}
}
/**
* Returns the name of trusted IP host.
* @return Name of trusted host
* @see #setLocalHost
*/
public String getLocalHost()
{
return localHost;
}
/**
* Returns a URL which is constrained to a trusted IP host.
* @param xURL URL or file path to be made safe
* @return Safe URL
* @exception MalformedURLException Thrown when xURL is not a valid URL
* @see #setLocalHost
* @see #getLocalHost
*/
public URL toSafeURL(String xURL, HttpServletRequest request)
throws MalformedURLException
{
// Fix submitted by Ritesh Kumar. Port is included in construction of URL that is returned.
if (port == 0)
port = request.getServerPort();
if (xURL == null)
return null;
if (xURL.startsWith("/"))
{
try
{
return new URL("http", localHost, port, xURL);
}
catch (MalformedURLException mue)
{
throw new MalformedURLException("toSafeURL(): " + xURL +
" did not map to local");
}
}
URL tempURL = null;
try
{
tempURL = new URL(xURL);
}
catch (MalformedURLException mue)
{
throw new MalformedURLException("toSafeURL(): " + xURL +
" not a valid URL");
}
try
{
return new URL(tempURL.getProtocol(), localHost,
port, tempURL.getFile());
}
catch (MalformedURLException mue)
{
throw new MalformedURLException("toSafeURL(): " + xURL +
" could not be converted to local host");
}
}
/**
* Returns a string representing the constrained URL for the XML document.
* If there is no request parameter for the XML document, return the configured default.
* @param request May contain an XML document URL parameter
* @return String form of XML URL
* @exception MalformedURLException Thrown when request URL is not a valid URL or path
* @see #toSafeURL
*/
public String getXMLurl(HttpServletRequest request)
throws MalformedURLException
{
URL url = toSafeURL(getRequestParmString(request, "URL"),request);
if (url == null)
return super.getXMLurl(null);
return url.toExternalForm();
}
/**
* Returns a string representing the constrained URL for the XSL stylesheet
* from the request.
* @param request May contain an XSL stylesheet URL parameter
* @return String form of request XSL URL, or null if request contains no xslURL parameter
* @exception MalformedURLException Thrown when request URL is not a valid URL or path
* @see #toSafeURL
*/
public String getXSLRequestURL(HttpServletRequest request)
throws MalformedURLException
{
URL url = toSafeURL(getRequestParmString(request, "xslURL"),request);
if (url == null)
return null;
return url.toExternalForm();
}
/**
* Returns a string representing the constrained request URL for the XSL stylesheet.
* If there is no request parameter for the XSL stylesheet, return the configured default.
* @param request May contain an XSL stylesheet URL parameter
* @return String form of XSL URL
* @exception MalformedURLException Thrown when request URL is not a valid URL or path
* @see #toSafeURL
*/
public String getXSLurl(HttpServletRequest request)
throws MalformedURLException
{
String reqURL = getXSLRequestURL(request);
if (reqURL != null)
return reqURL;
URL url = toSafeURL(super.getXSLurl(null), request);
return url.toExternalForm();
}
/**
* Returns URLs for all <a href="http://www.ccil.org/~cowan/XML/XCatalog.html">XCatalogs</a>
* that are to be used to process the request. Catalogs are used to resolve XML public identifiers
* into system identifiers.
* <p>A single XCatalog can be configured as a default,
* but multiple XCatalogs can be specified at request time to augment the configured default.
* @param request May contain one or more XCatalog parameters
* @return Array of strings for all catalog URLs
*/
public String[] getCatalog(HttpServletRequest request)
{
String temp[] = request.getParameterValues("catalog");
if (DEFAULT_catalog == null)
return temp;
if (temp == null)
{
String defaultArray[] = new String [1];
defaultArray[0] = DEFAULT_catalog;
return defaultArray;
}
int i, len = temp.length + 1;
String newCatalogs[] = new String[len];
newCatalogs[0] = DEFAULT_catalog;
for (i=1; i < len; i++)
{
newCatalogs[i] = temp[i-1];
}
return newCatalogs;
}
/**
* I think we no longer need this. Sets the 3 jaxp core system properties.
*/
protected void setSystemProperties()
{
Properties props = new Properties();
props.put("javax.xml.transform.TransformerFactory",
"org.apache.xalan.processor.TransformerFactoryImpl");
props.put("javax.xml.parsers.DocumentBuilderFactory",
"org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
props.put("javax.xml.parsers.SAXParserFactory",
"org.apache.xerces.jaxp.SAXParserFactoryImpl");
Properties systemProps = System.getProperties();
Enumeration propEnum = props.propertyNames();
while(propEnum.hasMoreElements())
{
String prop = (String)propEnum.nextElement();
if(!systemProps.containsKey(prop))
systemProps.put(prop, props.getProperty(prop));
}
System.setProperties(systemProps);
}
}
|