- Timestamp:
- 01/04/12 18:30:23 (5 months ago)
- Location:
- trunk/pustefix-webservices/pustefix-webservices-core/src/main/java/org/pustefixframework/webservices
- Files:
-
- 1 added
- 1 modified
-
ServiceRuntime.java (modified) (9 diffs)
-
ServiceStubProvider.java (added)
Legend:
- Unmodified
- Added
- Removed
-
trunk/pustefix-webservices/pustefix-webservices-core/src/main/java/org/pustefixframework/webservices/ServiceRuntime.java
r5854 r6100 21 21 import java.io.ByteArrayOutputStream; 22 22 import java.io.IOException; 23 import java.io.OutputStream; 24 import java.lang.reflect.Method; 23 25 import java.util.HashMap; 24 26 import java.util.Map; 25 27 28 import javax.servlet.ServletContext; 26 29 import javax.servlet.http.HttpServletRequest; 27 30 import javax.servlet.http.HttpServletResponse; … … 39 42 import org.pustefixframework.webservices.utils.RecordingResponseWrapper; 40 43 import org.springframework.aop.framework.Advised; 44 import org.springframework.beans.factory.DisposableBean; 45 import org.springframework.web.context.ServletContextAware; 41 46 42 47 import de.schlund.pfixcore.auth.AuthConstraint; … … 48 53 * @author mleidig@schlund.de 49 54 */ 50 public class ServiceRuntime {55 public class ServiceRuntime implements ServletContextAware, DisposableBean { 51 56 52 57 private final static Logger LOG=Logger.getLogger(ServiceRuntime.class); … … 69 74 private ServerContextImpl serverContext; 70 75 private ContextImpl context; 76 77 private ServiceStubProvider serviceStubProvider; 78 private ServletContext servletContext; 71 79 72 80 public ServiceRuntime() { … … 77 85 } 78 86 87 public void setServletContext(ServletContext servletContext) { 88 this.servletContext = servletContext; 89 //make ServiceStubProvider available to external consumers via ServletContext 90 this.serviceStubProvider = new ServiceStubProvider(this); 91 servletContext.setAttribute(ServiceStubProvider.class.getName(), serviceStubProvider); 92 } 93 94 public void destroy() throws Exception { 95 servletContext.removeAttribute(ServiceStubProvider.class.getName()); 96 } 97 79 98 public ServiceDescriptorCache getServiceDescriptorCache() { 80 99 return srvDescCache; … … 294 313 public void getStub(HttpServletRequest req,HttpServletResponse res) throws ServiceException, IOException { 295 314 296 long t1=System.currentTimeMillis();297 298 315 String nameParam=req.getParameter("name"); 299 316 if(nameParam==null) throw new ServiceException("Missing parameter: name"); … … 324 341 } 325 342 343 FileCacheData data = generateStub(services, serviceType, req.getContextPath()); 344 345 String etag=req.getHeader("If-None-Match"); 346 if(etag!=null && etag.equals(data.getMD5())) { 347 res.setStatus(HttpServletResponse.SC_NOT_MODIFIED); 348 } else { 349 res.setContentType("text/plain"); 350 res.setContentLength(data.getBytes().length); 351 res.setHeader("ETag",data.getMD5()); 352 res.getOutputStream().write(data.getBytes()); 353 res.getOutputStream().close(); 354 } 355 } 356 357 private FileCacheData generateStub(ServiceConfig[] services, String serviceType, String contextPath) throws ServiceException, IOException { 358 326 359 StringBuilder sb=new StringBuilder(); 327 for(S tring serviceName:serviceNames) {328 sb.append(service Name);360 for(ServiceConfig service:services) { 361 sb.append(service.getName()); 329 362 sb.append(" "); 330 363 } … … 340 373 ByteArrayOutputStream bout=new ByteArrayOutputStream(); 341 374 for(int i=0;i<services.length;i++) { 342 String requestPath = req.getContextPath()+ services[i].getGlobalServiceConfig().getRequestPath();375 String requestPath = contextPath + services[i].getGlobalServiceConfig().getRequestPath(); 343 376 stubGen.generateStub(services[i], requestPath, bout); 344 377 if(i<services.length-1) bout.write("\n\n".getBytes()); … … 353 386 } 354 387 355 long t2=System.currentTimeMillis(); 356 if(LOG.isDebugEnabled()) LOG.debug("Retrieved stub for '"+cacheKey+"' (Time: "+(t2-t1)+"ms)"); 357 358 String etag=req.getHeader("If-None-Match"); 359 if(etag!=null && etag.equals(data.getMD5())) { 360 res.setStatus(HttpServletResponse.SC_NOT_MODIFIED); 361 } else { 362 res.setContentType("text/plain"); 363 res.setContentLength(data.getBytes().length); 364 res.setHeader("ETag",data.getMD5()); 365 res.getOutputStream().write(data.getBytes()); 366 res.getOutputStream().close(); 367 } 388 return data; 389 } 390 391 public void generateStub(String[] serviceNames, String serviceType, OutputStream out) throws ServiceException, IOException { 392 393 serviceType = serviceType.toUpperCase(); 394 395 String contextPath; 396 //try to get context path from ServletContext, which is only directly supported since Servlet API 2.5 397 //this workaround uses reflection utilizing that Tomcat since 5.5.16 already implements this method internally 398 try { 399 Method meth = servletContext.getClass().getMethod("getContextPath"); 400 contextPath = (String)meth.invoke(servletContext); 401 } catch(Exception x) { 402 throw new RuntimeException("Your servlet container doesn't support getting the context path from the ServletContext. " + 403 "You should upgrade to a version supporting Servlet API 2.5 or newer - or at least a container internally " + 404 "implementing ServletContext.getContextPath()."); 405 } 406 407 ServiceConfig[] services=new ServiceConfig[serviceNames.length]; 408 for(int i=0;i<serviceNames.length;i++) { 409 ServiceConfig service=appServiceRegistry.getService(serviceNames[i]); 410 if(service==null) throw new ServiceException("Service not found: "+serviceNames[i]); 411 services[i]=service; 412 } 413 414 FileCacheData data = generateStub(services, serviceType, contextPath); 415 out.write(data.getBytes()); 368 416 } 369 417
