001 package com.croftsoft.apps.wyrm.servlet; 002 003 // J2SE 004 import java.io.*; 005 import java.net.*; 006 import java.util.*; 007 import javax.naming.*; 008 import javax.rmi.*; 009 import javax.xml.transform.*; 010 import javax.xml.transform.dom.*; 011 import javax.xml.transform.stream.*; 012 013 // J2EE 014 import javax.servlet.*; 015 import javax.servlet.http.*; 016 017 // Standard Extensions 018 import javax.xml.bind.JAXBContext; 019 import javax.xml.bind.JAXBException; 020 import javax.xml.bind.Marshaller; 021 import javax.xml.bind.Validator; 022 023 // CroftSoft Core 024 import com.croftsoft.core.lang.ClassLib; 025 026 // Application 027 import com.croftsoft.apps.wyrm.WyrmConstants; 028 import com.croftsoft.apps.wyrm.server.WyrmServerLocal; 029 import com.croftsoft.apps.wyrm.server.WyrmServerLocalHome; 030 import com.croftsoft.apps.wyrm.xjc.*; 031 032 /********************************************************************* 033 * Wyrm Servlet class. 034 * 035 * @version 036 * 2002-11-03 037 * @since 038 * 2002-10-16 039 * @author 040 * <a href="http://alumni.caltech.edu/~croft">David Wallace Croft</a> 041 *********************************************************************/ 042 043 public final class WyrmServlet 044 extends HttpServlet 045 implements WyrmConstants 046 ////////////////////////////////////////////////////////////////////// 047 ////////////////////////////////////////////////////////////////////// 048 { 049 050 private static final String WYRM_SERVER_LOCAL_HOME 051 = "WyrmServerLocalHome"; 052 053 // 054 055 private static final String PARAM_PASSWORD = "password"; 056 057 private static final String PARAM_PASSWORD_2 = "password2"; 058 059 private static final String PARAM_REQUEST = "request"; 060 061 private static final String PARAM_USERNAME = "username"; 062 063 // 064 065 private static final String SESSION_USERNAME = "username"; 066 067 private static final String SESSION_PASSWORD = "password"; 068 069 // 070 071 private static final String ENCODING_CHARSET = "utf-8"; 072 073 private static final String XSLT_FILE = "/xsl/browser.xsl"; 074 075 private static final String JAXB_CONTEXT_PATH 076 = "com.croftsoft.apps.wyrm.xjc"; 077 078 private static final String SCHEMA_LOCATION_NAMESPACE 079 = "https://www.croftsoft.com/apps/wyrm/xjc "; 080 081 private static final String XSD_DIR = "/xsd/"; 082 083 private static final String XSD_EXT = ".xsd"; 084 085 // 086 087 private WyrmServerLocal wyrmServerLocal; 088 089 private JAXBContext jaxbContext; 090 091 private Templates templates; 092 093 ////////////////////////////////////////////////////////////////////// 094 ////////////////////////////////////////////////////////////////////// 095 096 public void init ( ) 097 throws ServletException 098 ////////////////////////////////////////////////////////////////////// 099 { 100 try 101 { 102 103 // Do I need to use narrow with a local home interface? 104 105 WyrmServerLocalHome wyrmServerLocalHome 106 = ( WyrmServerLocalHome ) PortableRemoteObject.narrow ( 107 new InitialContext ( ).lookup ( WYRM_SERVER_LOCAL_HOME ), 108 WyrmServerLocalHome.class ); 109 110 wyrmServerLocal = wyrmServerLocalHome.create ( ); 111 112 jaxbContext = JAXBContext.newInstance ( JAXB_CONTEXT_PATH ); 113 114 TransformerFactory transformerFactory 115 = TransformerFactory.newInstance ( ); 116 117 URL xsltURL = getServletContext ( ).getResource ( XSLT_FILE ); 118 119 templates = transformerFactory.newTemplates ( 120 new StreamSource ( xsltURL.toExternalForm ( ) ) ); 121 } 122 catch ( Exception ex ) 123 { 124 throw new ServletException ( ex ); 125 } 126 } 127 128 public void service ( 129 HttpServletRequest httpServletRequest, 130 HttpServletResponse httpServletResponse ) 131 throws IOException, ServletException 132 ////////////////////////////////////////////////////////////////////// 133 { 134 try 135 { 136 Object request = toRequest ( httpServletRequest ); 137 138 // System.out.println ( "Request: " + request ); 139 140 Object response = wyrmServerLocal.serve ( request ); 141 142 // System.out.println ( "Response: " + response ); 143 144 if ( response == null ) 145 { 146 httpServletResponse.setStatus ( 147 HttpServletResponse.SC_NO_CONTENT ); 148 } 149 else 150 { 151 httpServletResponse.setStatus ( HttpServletResponse.SC_OK ); 152 153 // Is a Validator thread safe? If so, move creation to init(). 154 155 Validator validator = jaxbContext.createValidator ( ); 156 157 if ( !validator.validateRoot ( response ) ) 158 { 159 throw new Exception ( "validateRoot returned false" ); 160 } 161 162 Marshaller marshaller = jaxbContext.createMarshaller ( ); 163 164 // Since the JAXB_ENCODING property is UTF-8 by default, 165 // setting this is unnecessary when the ENCODING_CHARSET 166 // is UTF-8. 167 // 168 // marshaller.setProperty ( 169 // Marshaller.JAXB_ENCODING, ENCODING_CHARSET ); 170 171 // JAXB_FORMATTED_OUTPUT is false by default. 172 // 173 // marshaller.setProperty ( 174 // Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE ); 175 176 /* 177 This is broken since the JAXB_SCHEMA_LOCATION uses the "xsi" namespace 178 but does not define it. 179 180 String shortName 181 = ClassLib.getShortName ( response.getClass ( ) ); 182 183 // Trim off "Impl" at the end of the class name. 184 185 String schemaLocationHint 186 = httpServletRequest.getScheme ( ) 187 + "://" 188 + httpServletRequest.getServerName ( ) 189 + ":" 190 + httpServletRequest.getServerPort ( ) 191 + httpServletRequest.getContextPath ( ) 192 + XSD_DIR 193 + shortName.substring ( 0, shortName.length ( ) - 4 ) 194 + XSD_EXT; 195 196 String jaxbSchemaLocation 197 = SCHEMA_LOCATION_NAMESPACE 198 + schemaLocationHint; 199 200 marshaller.setProperty ( 201 Marshaller.JAXB_SCHEMA_LOCATION, jaxbSchemaLocation ); 202 */ 203 204 DOMResult domResult = new DOMResult ( ); 205 206 marshaller.marshal ( response, domResult ); 207 208 DOMSource domSource = new DOMSource ( domResult.getNode ( ) ); 209 210 // Templates is thread safe; Transformer is not. 211 212 Transformer transformer = templates.newTransformer ( ); 213 214 httpServletResponse.setContentType ( 215 "text/xml; charset=" + ENCODING_CHARSET ); 216 217 transformer.transform ( 218 domSource, 219 new StreamResult ( httpServletResponse.getWriter ( ) ) ); 220 } 221 } 222 catch ( Exception ex ) 223 { 224 ex.printStackTrace ( httpServletResponse.getWriter ( ) ); 225 } 226 } 227 228 ////////////////////////////////////////////////////////////////////// 229 ////////////////////////////////////////////////////////////////////// 230 231 private Object toRequest ( HttpServletRequest httpServletRequest ) 232 throws JAXBException 233 ////////////////////////////////////////////////////////////////////// 234 { 235 String requestType 236 = httpServletRequest.getParameter ( PARAM_REQUEST ); 237 238 String password = null; 239 240 String username = null; 241 242 HttpSession httpSession = httpServletRequest.getSession ( ); 243 244 if ( requestType == null ) 245 { 246 requestType = TYPE_STATE; 247 } 248 249 if ( requestType.equals ( TYPE_CREATE_USER ) 250 || requestType.equals ( TYPE_LOGIN ) ) 251 { 252 username = httpServletRequest.getParameter ( PARAM_USERNAME ); 253 254 password = httpServletRequest.getParameter ( PARAM_PASSWORD ); 255 256 if ( requestType.equals ( TYPE_CREATE_USER ) ) 257 { 258 String password2 259 = httpServletRequest.getParameter ( PARAM_PASSWORD_2 ); 260 261 if ( ( password != null ) 262 && !password.equals ( password2 ) ) 263 { 264 password = null; 265 } 266 } 267 268 httpSession.setAttribute ( SESSION_USERNAME, username ); 269 270 httpSession.setAttribute ( SESSION_PASSWORD, password ); 271 } 272 else 273 { 274 password = ( String ) httpSession.getAttribute ( SESSION_PASSWORD ); 275 276 username = ( String ) httpSession.getAttribute ( SESSION_USERNAME ); 277 278 if ( requestType.equals ( TYPE_LOGOUT ) ) 279 { 280 httpSession.invalidate ( ); 281 } 282 } 283 284 Request request = ObjectFactory.createRequest ( ); 285 286 request.setType ( requestType ); 287 288 request.setUsername ( username ); 289 290 request.setPassword ( password ); 291 292 return request; 293 } 294 295 ////////////////////////////////////////////////////////////////////// 296 ////////////////////////////////////////////////////////////////////// 297 }