View Javadoc
1 /* 2 * $Header: /cvsroot/ebxmlrr/ebxmlrr/src/share/com/sun/ebxml/registry/interfaces/rest/RestServlet.java,v 1.7 2003/06/27 15:31:33 farrukh_najmi Exp $ 3 * 4 * ==================================================================== 5 * 6 * This code is subject to the freebxml License, Version 1.1 7 * 8 * Copyright (c) 2003 freebxml.org. All rights reserved. 9 * 10 * ==================================================================== 11 */ 12 13 package com.sun.ebxml.registry.interfaces.rest; 14 15 import java.io.IOException; 16 import javax.servlet.ServletException; 17 import javax.servlet.http.HttpServletRequest; 18 import javax.servlet.http.HttpServletResponse; 19 20 /*** 21 * 22 * @author Uday Subbarayan(mailto:uday.s@sun.com) 23 * @version 24 */ 25 public class RestServlet extends javax.servlet.http.HttpServlet { 26 27 private org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(this.getClass()); 28 29 public static final String SLOT_REGISTRY_OBJECT_URI="urn:oasis:names:tc:ebxml-regrep:2.5:rim:RegistryObject:locator"; 30 public static final String SLOT_REPOSITORY_ITEM_URI="urn:oasis:names:tc:ebxml-regrep:2.5:rim:RegistryObject:RegistryEntry:ExtrinsicObject:contentLocator"; 31 32 /*** Initializes the servlet. 33 */ 34 public void init(javax.servlet.ServletConfig config) throws ServletException { 35 super.init(config); 36 37 } 38 39 /*** Destroys the servlet. 40 */ 41 public void destroy() { 42 43 } 44 45 /*** Processes requests for both HTTP <code>GET</code> methods. 46 * @param request servlet request 47 * @param response servlet response 48 */ 49 protected void processGetRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 50 String interfaces=request.getParameter("interface"); 51 52 if(interfaces == null || interfaces.equals("")){ 53 processURIGetRequest(request,response); 54 }else if(interfaces.equalsIgnoreCase("QueryManager")){ 55 processQueryManagerGetRequest(request,response); 56 }else if(interfaces.equalsIgnoreCase("LifecycleManager")){ 57 response.sendError(HttpServletResponse.SC_BAD_REQUEST,"LifeCycleManager is not supported via Get,It should be via Post only!"); 58 }else{ 59 response.sendError(HttpServletResponse.SC_BAD_REQUEST,"The value for interface must be QueryManager!"); 60 61 } 62 } 63 64 /*** Processes requests for both HTTP <code>POST</code> methods. 65 * @param request servlet request 66 * @param response servlet response 67 */ 68 protected void processPostRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 69 String interfaces=request.getParameter("interface"); 70 processQueryManagerPostRequest(request,response);//calling the method directly till RESTSender is finished.It will be removed 71 72 //comment out temporarily untill RESTSender is finished. 73 /* 74 if(interfaces == null || interfaces.equals("")){ 75 response.sendError(HttpServletResponse.SC_BAD_REQUEST,"interface parameter cannot be null or empty!"); 76 }else if(interfaces.equalsIgnoreCase("QueryManager")){ 77 processQueryManagerPostRequest(request,response); 78 }else if(interfaces.equalsIgnoreCase("LifecycleManager")){ 79 response.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED,"LifeCycleManager Methods Are Yet To be Implemented!"); 80 }else{ 81 response.sendError(HttpServletResponse.SC_BAD_REQUEST,"The value for interface must be QueryManager or LifeCycleManager!"); 82 83 } 84 */ 85 86 } 87 88 /*** Handles the HTTP <code>GET</code> method. 89 * @param request servlet request 90 * @param response servlet response 91 */ 92 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 93 processGetRequest(request, response); 94 } 95 96 /*** Handles the HTTP <code>POST</code> method. 97 * @param request servlet request 98 * @param response servlet response 99 */ 100 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 101 //System.out.println("Printig inside the doPost!"); 102 processPostRequest(request, response); 103 } 104 105 /*** Returns the description of the servlet. 106 * @param response servlet description 107 */ 108 public String getServletInfo() { 109 return "REST Implementation of OASIS Ebxml-RR"; 110 } 111 112 /*** Process the QueryManager request and sends the response back to the client 113 * @param 114 * 115 */ 116 private void processQueryManagerGetRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ 117 String method=request.getParameter("method"); 118 String id=request.getParameter("param-id"); 119 if(method==null||method.equals("")){ 120 //return the error code 121 response.sendError(HttpServletResponse.SC_BAD_REQUEST,"The correct value for the method param must be specified!"); 122 }else if(method.equalsIgnoreCase("getRegistryObject")){ 123 try{ 124 response.setContentType("text/xml"); 125 java.io.PrintWriter out = response.getWriter(); 126 //call the API in QM 127 org.oasis.ebxml.registry.bindings.rim.RegistryObjectType regobj=com.sun.ebxml.registry.query.QueryManagerImpl.getInstance().getRegistryObject(id); 128 regobj.marshal(out); 129 out.close(); 130 //set the Http status 131 response.setStatus(HttpServletResponse.SC_OK); 132 }catch(org.exolab.castor.xml.MarshalException e){ 133 //throw new RegistryException(e); 134 e.printStackTrace(); 135 }catch(org.exolab.castor.xml.ValidationException e){ 136 //throw new RegistryException(e); 137 e.printStackTrace(); 138 }catch(com.sun.ebxml.registry.RegistryException e){ 139 //throw new RegistryException(e); 140 e.printStackTrace(); 141 }catch(java.lang.NullPointerException e){ 142 response.sendError(HttpServletResponse.SC_NOT_FOUND,"The RegistryObject you requested is not found in the registry!"); 143 } 144 145 }else if(method.equalsIgnoreCase("getRepositoryItem")){ 146 try{ 147 com.sun.ebxml.registry.repository.RepositoryItem rt =com.sun.ebxml.registry.query.QueryManagerImpl.getInstance().getRepositoryItem(id); 148 if(rt==null){ 149 response.sendError(HttpServletResponse.SC_NOT_FOUND,"The RepositoryItem you requested is not found in the repository!"); 150 }else{ 151 152 org.oasis.ebxml.registry.bindings.rim.ExtrinsicObjectType eo = (org.oasis.ebxml.registry.bindings.rim.ExtrinsicObjectType) 153 com.sun.ebxml.registry.query.QueryManagerImpl.getInstance().getRegistryObject(id); 154 155 response.setContentType(eo.getMimeType()); 156 157 javax.activation.DataHandler dataHandler=rt.getDataHandler(); 158 javax.servlet.ServletOutputStream sout=response.getOutputStream(); 159 java.io.InputStream fStream=dataHandler.getInputStream(); 160 int bytesSize=fStream.available(); 161 byte[] b=new byte[bytesSize]; 162 fStream.read(b); 163 sout.write(b); 164 sout.close(); 165 //set the Http OK status 166 response.setStatus(HttpServletResponse.SC_OK); 167 } 168 169 }catch(com.sun.ebxml.registry.RegistryException e){ 170 e.printStackTrace(); 171 } 172 173 }else{ 174 //return the error code 175 response.sendError(HttpServletResponse.SC_BAD_REQUEST); 176 } 177 } 178 179 /*** Process the QueryManager POST request and sends the response back to the client 180 * @param 181 * 182 */ 183 private void processQueryManagerPostRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ 184 185 try{ 186 java.io.PrintWriter out = response.getWriter(); 187 String xmlAdhocQueryRequest = request.getParameter("xmldoc"); 188 189 /*String xmlAdhocQueryRequest=""; 190 String input; 191 java.io.BufferedReader bReader=request.getReader(); 192 while((input = bReader.readLine()) != null){ 193 xmlAdhocQueryRequest = xmlAdhocQueryRequest + input; 194 } 195 bReader.close();*/ 196 197 System.out.println(xmlAdhocQueryRequest); 198 String message=xmlAdhocQueryRequest; 199 org.oasis.ebxml.registry.bindings.query.AdhocQueryRequest req = org.oasis.ebxml.registry.bindings.query.AdhocQueryRequest.unmarshal(new java.io.StringReader(message)); 200 //System.out.println("req.isValid()= "+req.isValid()); 201 if(req.isValid()){ 202 org.oasis.ebxml.registry.bindings.rim.User user=new org.oasis.ebxml.registry.bindings.rim.User(); 203 org.oasis.ebxml.registry.bindings.rs.RegistryResponse regResponse=com.sun.ebxml.registry.query.QueryManagerImpl.getInstance().submitAdhocQuery(user,req); 204 regResponse.marshal(out); 205 out.close(); 206 //set the Http OK status 207 response.setStatus(HttpServletResponse.SC_OK); 208 }else{ 209 //return the error code 210 response.sendError(HttpServletResponse.SC_BAD_REQUEST,"The input XML is NOT a valid AdhocQueryRequest!"); 211 } 212 213 }catch(org.exolab.castor.xml.MarshalException ex){ 214 ex.printStackTrace(); 215 }catch(org.exolab.castor.xml.ValidationException ex){ 216 ex.printStackTrace(); 217 }catch(com.sun.ebxml.registry.RegistryException ex){ 218 ex.printStackTrace(); 219 } 220 } 221 222 /*** 223 * Process client request for a resource by its URI. 224 * The request could match the URI of a RegistryObject or 225 * a RepositoryItem. 226 */ 227 private void processURIGetRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ 228 String pathInfo = request.getPathInfo(); 229 log.info("requestURI=" + request.getRequestURI() + " contextPath="+request.getContextPath() + " servletPath="+request.getServletPath() + " pathInfo="+pathInfo); 230 231 java.util.ArrayList results = null; 232 results = getRepositoryItemByURI(pathInfo, response); 233 234 if ((results == null) || (results.size() == 0)) { 235 results = getRegistryObjectByURI(pathInfo, response); 236 } 237 } 238 239 /*** 240 * Attempt to get a RepositoryItem by its URI. 241 * 242 */ 243 private java.util.ArrayList getRepositoryItemByURI(String pathInfo, HttpServletResponse response) throws ServletException, IOException{ 244 java.util.ArrayList results = null; 245 try{ 246 org.oasis.ebxml.registry.bindings.rim.ExtrinsicObjectType ro = null; 247 String sqlQuery = "SELECT * FROM ExtrinsicObject ro, Slot s WHERE s.value='"+ pathInfo + "' AND s.name_='"+SLOT_REPOSITORY_ITEM_URI+"' AND s.parent=ro.id"; 248 org.oasis.ebxml.registry.bindings.query.ResponseOption responseOption = new org.oasis.ebxml.registry.bindings.query.ResponseOption(); 249 responseOption.setReturnComposedObjects(true); 250 responseOption.setReturnType(org.oasis.ebxml.registry.bindings.query.types.ReturnTypeType.LEAFCLASS); 251 String tableName = com.sun.ebxml.registry.persistence.rdb.ExtrinsicObjectDAO.getTableNameStatic(); 252 java.util.ArrayList objectRefs = new java.util.ArrayList(); 253 results= com.sun.ebxml.registry.persistence.rdb.SQLPersistenceManagerImpl.getInstance().executeSQLQuery(sqlQuery, responseOption, tableName, objectRefs); 254 255 if (results.size() == 0) { 256 } 257 else if (results.size() == 1) { 258 ro = (org.oasis.ebxml.registry.bindings.rim.ExtrinsicObjectType)results.get(0); 259 260 log.info("Found ExtrinsicObject for RepositoryItem with id = " + ro.getId()); 261 response.setContentType(ro.getMimeType()); 262 String id = ro.getId(); 263 com.sun.ebxml.registry.repository.RepositoryItem rt =com.sun.ebxml.registry.query.QueryManagerImpl.getInstance().getRepositoryItem(id); 264 if(rt==null){ 265 response.sendError(HttpServletResponse.SC_NOT_FOUND,"The RepositoryItem for id " + id + " not found in the repository!"); 266 } else{ 267 javax.activation.DataHandler dataHandler=rt.getDataHandler(); 268 javax.servlet.ServletOutputStream sout=response.getOutputStream(); 269 java.io.InputStream fStream=dataHandler.getInputStream(); 270 int bytesSize=fStream.available(); 271 byte[] b=new byte[bytesSize]; 272 fStream.read(b); 273 sout.write(b); 274 sout.close(); 275 //set the Http status 276 response.setStatus(HttpServletResponse.SC_OK); 277 } 278 } else { 279 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,"Duplicate RegistryObjects found with URI Slot with value '" + pathInfo + "'"); 280 } 281 }catch(com.sun.ebxml.registry.RegistryException e){ 282 //throw new RegistryException(e); 283 e.printStackTrace(); 284 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString()); 285 }catch(java.lang.NullPointerException e){ 286 e.printStackTrace(); 287 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString()); 288 } 289 290 return results; 291 } 292 293 /*** 294 * Attempt to get a RegistryObject by its URI. 295 * 296 */ 297 private java.util.ArrayList getRegistryObjectByURI(String pathInfo, HttpServletResponse response) throws ServletException, IOException{ 298 java.util.ArrayList results = null; 299 try{ 300 org.oasis.ebxml.registry.bindings.rim.RegistryObjectType ro = null; 301 String sqlQuery = "SELECT * FROM RegistryObject ro, Slot s WHERE s.value='"+ pathInfo + "' AND s.name_='"+SLOT_REGISTRY_OBJECT_URI+"' AND s.parent=ro.id"; 302 org.oasis.ebxml.registry.bindings.query.ResponseOption responseOption = new org.oasis.ebxml.registry.bindings.query.ResponseOption(); 303 responseOption.setReturnComposedObjects(true); 304 responseOption.setReturnType(org.oasis.ebxml.registry.bindings.query.types.ReturnTypeType.LEAFCLASS); 305 String tableName = com.sun.ebxml.registry.persistence.rdb.RegistryObjectDAO.getTableNameStatic(); 306 java.util.ArrayList objectRefs = new java.util.ArrayList(); 307 results= com.sun.ebxml.registry.persistence.rdb.SQLPersistenceManagerImpl.getInstance().executeSQLQuery(sqlQuery, responseOption, tableName, objectRefs); 308 309 if (results.size() == 0) { 310 response.sendError(HttpServletResponse.SC_NOT_FOUND,"No RegistryObject found with URI Slot with value '" + pathInfo + "'"); 311 } else if (results.size() == 1) { 312 ro = (org.oasis.ebxml.registry.bindings.rim.RegistryObjectType)results.get(0); 313 314 log.info("Found RegistryObject with id = " + ro.getId()); 315 response.setContentType("text/xml"); 316 java.io.PrintWriter out = response.getWriter(); 317 318 ro.marshal(out); 319 out.close(); 320 //set the Http status 321 response.setStatus(HttpServletResponse.SC_OK); 322 323 } else { 324 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,"Duplicate RegistryObjects found with URI Slot with value '" + pathInfo + "'"); 325 } 326 }catch(org.exolab.castor.xml.MarshalException e){ 327 //throw new RegistryException(e); 328 e.printStackTrace(); 329 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString()); 330 }catch(org.exolab.castor.xml.ValidationException e){ 331 //throw new RegistryException(e); 332 e.printStackTrace(); 333 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString()); 334 }catch(com.sun.ebxml.registry.RegistryException e){ 335 //throw new RegistryException(e); 336 e.printStackTrace(); 337 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString()); 338 }catch(java.lang.NullPointerException e){ 339 e.printStackTrace(); 340 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString()); 341 } 342 343 return results; 344 } 345 346 347 /*** Process the LifeCycleManager Request and sends the response back to the client 348 * @param 349 * 350 */ 351 private void ProcessLCMPostRequest(){ 352 //This is an optional support in the v2.2 spec and yet to be implemented 353 } 354 355 }

This page was automatically generated by Maven