1 57 58 package org.apache.soap.server.http; 59 60 import java.io.*; 61 import java.util.*; 62 import java.lang.reflect.*; 63 import javax.servlet.* ; 64 import javax.servlet.http.*; 65 import javax.xml.parsers.*; 66 import org.w3c.dom.*; 67 import org.apache.soap.*; 68 import org.apache.soap.server.*; 69 import org.apache.soap.util.*; 70 import org.apache.soap.util.xml.*; 71 import org.apache.soap.rpc.SOAPContext; 72 import org.apache.soap.encoding.SOAPMappingRegistry; 73 import javax.activation.*; 74 import javax.mail.*; 75 import javax.mail.internet.*; 76 import org.apache.soap.transport.EnvelopeEditor; 77 78 83 public class ServerHTTPUtils { 84 private static final String SERVICE_MANAGER_ID = "serviceManager"; 85 private static final String SCRIPT_CLASS = "com.ibm.bsf.BSFManager"; 86 private static final String SCRIPT_INVOKER = 87 "org.apache.soap.server.InvokeBSF"; 88 private static final String SERVLET_CLASSLOADER = "servletClassLoader"; 89 90 97 public static ServiceManager getServiceManagerFromContext(ServletContext context, 98 String configFilename) { 99 Object o; 100 if ( context != null ) { 101 synchronized (context) { 102 o = context.getAttribute(SERVICE_MANAGER_ID); 103 if (o == null) { 104 o = new ServiceManager(context, configFilename); 105 context.setAttribute (SERVICE_MANAGER_ID, o); 106 } 107 } 108 } 109 else { 110 o = new ServiceManager( null, configFilename ); 111 } 112 return (ServiceManager) o; 113 } 114 115 118 public static ServiceManager getServiceManagerFromContext(ServletContext context) { 119 return getServiceManagerFromContext(context, null); 120 } 121 122 125 public static ClassLoader getServletClassLoaderFromContext(ServletContext context) { 126 if (context != null) { 127 Object o; 128 129 synchronized (context) { 130 o = context.getAttribute(SERVLET_CLASSLOADER); 131 } 132 133 return (ClassLoader ) o; 134 } else { 135 return null; 136 } 137 } 138 139 142 public static void setServletClassLoaderIntoContext(ServletContext context, 143 ClassLoader cl) { 144 synchronized (context) { 145 context.setAttribute(SERVLET_CLASSLOADER, cl); 146 } 147 } 148 149 157 public static File getFileFromNameAndContext(String fileName, 158 ServletContext context) { 159 File file = new File(fileName); 160 161 if (!file.isAbsolute()) 162 { 163 if (context != null) 164 { 165 String realFileName = context.getRealPath(fileName); 166 167 if (realFileName != null) 168 { 169 file = new File(realFileName); 170 } 171 } 172 } 173 174 return file; 175 } 176 177 189 public static Envelope readEnvelopeFromRequest (DocumentBuilder xdb, 190 String contentType, 191 int contentLength, 192 InputStream requestStream, 193 EnvelopeEditor editor, 194 HttpServletResponse res, 195 SOAPContext ctx) 196 throws SOAPException, IOException { 197 try { 198 return ServerUtils.readEnvelopeFromInputStream (xdb, requestStream, 199 contentLength, 200 contentType, editor, 201 ctx); 202 } catch (IllegalArgumentException e) { 203 String msg = e.getMessage (); 204 res.sendError (res.SC_BAD_REQUEST, "Error unmarshalling envelope: " + 205 msg); 206 return null; 207 } catch (MessagingException me) { 208 res.sendError (res.SC_BAD_REQUEST, "Error unmarshalling envelope: " + 209 me); 210 return null; 211 } 212 } 213 214 219 public static Object getTargetObject (ServiceManager serviceManager, 220 DeploymentDescriptor dd, 221 String targetID, 222 HttpServlet thisServlet, 223 HttpSession session, 224 SOAPContext ctxt, 225 ServletContext context) 226 throws SOAPException { 227 int scope = dd.getScope (); 228 byte providerType = dd.getProviderType (); 229 String className; 230 Object targetObject = null; 231 if (providerType == DeploymentDescriptor.PROVIDER_JAVA || 232 providerType == DeploymentDescriptor.PROVIDER_USER_DEFINED) { 233 className = dd.getProviderClass (); 234 } else { 235 className = SCRIPT_CLASS; 237 } 238 239 Object scopeLock = null; 242 if (scope == DeploymentDescriptor.SCOPE_REQUEST) { 243 scopeLock = thisServlet; } else if (scope == DeploymentDescriptor.SCOPE_SESSION) { 245 scopeLock = session; 246 } else if (scope == DeploymentDescriptor.SCOPE_APPLICATION) { 247 scopeLock = context; 248 } else { 249 throw new SOAPException (Constants.FAULT_CODE_SERVER, 250 "Service uses deprecated object scope " + 251 "'page': inform provider of error"); 252 } 253 254 boolean freshObject = false; 256 257 if (targetID.equals (ServerConstants.SERVICE_MANAGER_SERVICE_NAME)) { 260 targetObject = serviceManager; 261 } else { 262 if ( scopeLock == null ) scopeLock = className ; synchronized (scopeLock) { 265 if (scopeLock == session) { 266 targetObject = session.getValue (targetID); 268 } else if (scopeLock == context) { 269 targetObject = context.getAttribute (targetID); 270 } else { 271 targetObject = null; 272 } 273 if (targetObject == null) { 274 try { 275 Class c = ctxt.loadClass(className); 276 277 if (dd.getIsStatic ()) { 278 targetObject = c; 279 } else { 280 targetObject = c.newInstance (); 281 } 282 freshObject = true; 283 284 if (scopeLock == session) { 288 session.putValue (targetID, targetObject); 289 } else if (scopeLock == context) { 291 context.setAttribute (targetID, targetObject); 292 } 293 } catch (Exception e) { 294 String msg; 295 if (providerType == DeploymentDescriptor.PROVIDER_JAVA || 296 providerType == DeploymentDescriptor.PROVIDER_USER_DEFINED) { 297 msg = "Unable to resolve target object: " + e.getMessage (); 298 } else { 299 msg = "Unable to load BSF: script services not available " + 300 "without BSF: " + e.getMessage (); 301 } 302 throw new SOAPException ( 303 Constants.FAULT_CODE_SERVER_BAD_TARGET_OBJECT_URI, msg, e); 304 } 305 } 306 } 307 } 308 309 if ((providerType != DeploymentDescriptor.PROVIDER_JAVA && 312 providerType != DeploymentDescriptor.PROVIDER_USER_DEFINED) 313 && freshObject) { 314 Class bc = null; 317 try { 318 bc = ctxt.loadClass(SCRIPT_INVOKER); 319 } catch (Exception e) { 320 String msg = "Unable to load BSF invoker (" + SCRIPT_INVOKER + ")" + 321 ": script services not available without BSF: " + e.getMessage (); 322 throw new SOAPException (Constants.FAULT_CODE_SERVER, msg, e); 323 } 324 325 String script = dd.getScriptFilenameOrString (); 327 if (providerType == DeploymentDescriptor.PROVIDER_SCRIPT_FILE) { 328 String fileName = context.getRealPath (script); 329 try { 330 script = IOUtils.getStringFromReader (new FileReader (fileName)); 331 } catch (Exception e) { 332 String msg = "Unable to load script file (" + fileName + ")" + 333 ": " + e.getMessage (); 334 throw new SOAPException (Constants.FAULT_CODE_SERVER, msg, e); 335 } 336 } 337 338 Class [] sig = {DeploymentDescriptor.class, 340 Object .class, 341 String .class}; 342 try { 343 Method m = MethodUtils.getMethod (bc, "init", sig, true); 344 m.invoke (null, new Object [] {dd, targetObject, script}); 345 } catch (InvocationTargetException ite) { 346 Throwable te = ite.getTargetException(); 347 if (te instanceof SOAPException) 348 throw (SOAPException)te; 349 String msg = "Unable to invoke init method of script invoker: " + te; 350 throw new SOAPException (Constants.FAULT_CODE_SERVER, msg, te); 351 } catch (Exception e) { 352 String msg = "Unable to invoke init method of script invoker: " + e; 353 throw new SOAPException (Constants.FAULT_CODE_SERVER, msg, e); 354 } 355 } 356 357 return targetObject; 358 } 359 360 365 public static SOAPMappingRegistry 366 getSMRFromContext (ServletContext context) { 367 SOAPMappingRegistry smr = null; 368 synchronized (context) { 369 smr = 370 (SOAPMappingRegistry) context.getAttribute ("__cached_servlet_SMR__"); 371 if (smr == null) { 372 smr = new SOAPMappingRegistry (); 373 context.setAttribute ("__cached_servlet_SMR__", smr); 374 } 375 } 376 return smr; 377 } 378 } 379 | Popular Tags |