1 13 14 package org.ejbca.ui.web.protocol; 15 16 import java.io.ByteArrayOutputStream ; 17 import java.io.IOException ; 18 import java.util.Properties ; 19 20 import javax.servlet.ServletConfig ; 21 import javax.servlet.ServletException ; 22 import javax.servlet.ServletInputStream ; 23 import javax.servlet.http.HttpServlet ; 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletResponse ; 26 27 import org.apache.commons.lang.StringUtils; 28 import org.apache.log4j.Logger; 29 import org.ejbca.core.ejb.ServiceLocator; 30 import org.ejbca.core.model.InternalResources; 31 import org.ejbca.core.model.log.Admin; 32 import org.ejbca.core.protocol.IResponseMessage; 33 import org.ejbca.core.protocol.cmp.CmpMessageDispatcher; 34 import org.ejbca.ui.web.RequestHelper; 35 import org.ejbca.ui.web.pub.ServletUtils; 36 import org.ejbca.util.Base64; 37 38 39 146 public class CmpServlet extends HttpServlet { 147 private static final Logger log = Logger.getLogger(CmpServlet.class); 148 149 private static final InternalResources intres = InternalResources.getInstance(); 150 151 private Properties properties; 152 159 public void init(ServletConfig config) throws ServletException { 160 super.init(config); 161 properties = new Properties (); 162 String str = ServiceLocator.getInstance().getString("java:comp/env/allowRaVerifyPopo"); 163 if (StringUtils.equals("true", str)) { 164 log.debug("allowRAVerifyPopo=true"); 165 properties.setProperty("allowRaVerifyPopo", "true"); 166 } 167 str = ServiceLocator.getInstance().getString("java:comp/env/defaultCA"); 168 log.debug("defaultCA="+str); 169 if (StringUtils.isNotEmpty(str)) { 170 properties.setProperty("defaultCA", str); 171 } 172 str = ServiceLocator.getInstance().getString("java:comp/env/extractUsernameComponent"); 173 log.debug("extractUsernameComponent="+str); 174 if (StringUtils.isNotEmpty(str)) { 175 properties.setProperty("extractUsernameComponent", str); 176 } 177 str = ServiceLocator.getInstance().getString("java:comp/env/operationMode"); 178 log.debug("operationMode="+str); 179 if (StringUtils.isNotEmpty(str)) { 180 properties.setProperty("operationMode", str); 181 } 182 str = ServiceLocator.getInstance().getString("java:comp/env/raModeNameGenerationScheme"); 183 log.debug("raModeNameGenerationScheme="+str); 184 if (StringUtils.isNotEmpty(str)) { 185 properties.setProperty("raModeNameGenerationScheme", str); 186 } 187 str = ServiceLocator.getInstance().getString("java:comp/env/raModeNameGenerationParameters"); 188 log.debug("raModeNameGenerationParameters="+str); 189 if (StringUtils.isNotEmpty(str)) { 190 properties.setProperty("raModeNameGenerationParameters", str); 191 } 192 str = ServiceLocator.getInstance().getString("java:comp/env/raModeNameGenerationPrefix"); 193 log.debug("raModeNameGenerationPrefix="+str); 194 if (StringUtils.isNotEmpty(str)) { 195 properties.setProperty("raModeNameGenerationPrefix", str); 196 } 197 str = ServiceLocator.getInstance().getString("java:comp/env/raModeNameGenerationPostfix"); 198 log.debug("raModeNameGenerationPostfix="+str); 199 if (StringUtils.isNotEmpty(str)) { 200 properties.setProperty("raModeNameGenerationPostfix", str); 201 } 202 str = ServiceLocator.getInstance().getString("java:comp/env/responseProtection"); 203 if (StringUtils.isNotEmpty(str)) { 204 log.debug("responseProtection="+str); 205 properties.setProperty("responseProtection", str); 206 } 207 str = ServiceLocator.getInstance().getString("java:comp/env/raAuthenticationSecret"); 208 if (StringUtils.isNotEmpty(str)) { 209 log.debug("raAuthenticationSecret is not null"); 210 properties.setProperty("raAuthenticationSecret", str); 211 } 212 str = ServiceLocator.getInstance().getString("java:comp/env/endEntityProfile"); 213 if (StringUtils.isNotEmpty(str)) { 214 log.debug("endEntityProfile="+str); 215 properties.setProperty("endEntityProfile", str); 216 } 217 str = ServiceLocator.getInstance().getString("java:comp/env/certificateProfile"); 218 if (StringUtils.isNotEmpty(str)) { 219 log.debug("certificateProfile="+str); 220 properties.setProperty("certificateProfile", str); 221 } 222 str = ServiceLocator.getInstance().getString("java:comp/env/caName"); 223 if (StringUtils.isNotEmpty(str)) { 224 log.debug("caName="+str); 225 properties.setProperty("caName", str); 226 } 227 } 228 229 238 public void doPost(HttpServletRequest request, HttpServletResponse response) 239 throws IOException , ServletException { 240 log.debug(">doPost()"); 241 245 ServletInputStream sin = request.getInputStream(); 246 ByteArrayOutputStream output = new ByteArrayOutputStream (); 248 byte[] buf = new byte[1024]; 249 int n = 0; 250 while (-1 != (n = sin.read(buf))) { 251 output.write(buf, 0, n); 252 } 253 service(output.toByteArray(), request.getRemoteAddr(), response); 254 log.debug("<doPost()"); 255 } 257 266 public void doGet(HttpServletRequest request, HttpServletResponse response) 267 throws java.io.IOException , ServletException { 268 log.debug(">doGet()"); 269 270 log.info("Received un-allowed method GET in CMP servlet: query string=" + request.getQueryString()); 271 response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "You can only use POST!"); 272 273 log.debug("<doGet()"); 274 } 276 private void service(byte[] message, String remoteAddr, HttpServletResponse response) throws IOException { 277 try { 278 if ((message == null)) { 279 log.error("Got request missing message."); 280 response.sendError(HttpServletResponse.SC_BAD_REQUEST, "A message must be supplied!"); 281 return; 282 } 283 284 Admin administrator = new Admin(Admin.TYPE_RA_USER, remoteAddr); 286 if (log.isDebugEnabled()) { 287 log.debug("Received a CMP message by HTTP: " + new String (Base64.encode(message))); 288 } 289 String iMsg = intres.getLocalizedMessage("cmp.receivedmsg", remoteAddr); 290 log.info(iMsg); 291 CmpMessageDispatcher dispatcher = new CmpMessageDispatcher(administrator, properties); 292 IResponseMessage resp = dispatcher.dispatch(message); 293 if (resp != null) { 295 ServletUtils.addCacheHeaders(response); 297 RequestHelper.sendBinaryBytes(resp.getResponseMessage(), response, "application/pkixcmp", null); 299 iMsg = intres.getLocalizedMessage("cmp.sentresponsemsg", remoteAddr); 300 log.info(iMsg); 301 } 302 } catch (Exception e) { 303 log.error("Error in CmpServlet:", e); 304 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage()); 305 } 306 } 307 308 } | Popular Tags |