1 13 package info.magnolia.cms.exchange.simple; 14 15 import info.magnolia.cms.beans.config.ConfigLoader; 16 import info.magnolia.cms.beans.config.ContentRepository; 17 import info.magnolia.cms.core.CacheHandler; 18 import info.magnolia.cms.core.HierarchyManager; 19 import info.magnolia.cms.security.Authenticator; 20 import info.magnolia.cms.security.Listener; 21 import info.magnolia.cms.security.Lock; 22 import info.magnolia.cms.security.SecureURI; 23 import info.magnolia.cms.security.SessionAccessControl; 24 import info.magnolia.exchange.ExchangeException; 25 import info.magnolia.exchange.Packet; 26 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.io.ObjectInputStream ; 30 import java.io.ObjectOutputStream ; 31 import java.net.URL ; 32 import java.net.URLConnection ; 33 34 import javax.jcr.PathNotFoundException; 35 import javax.servlet.ServletException ; 36 import javax.servlet.ServletOutputStream ; 37 import javax.servlet.SingleThreadModel ; 38 import javax.servlet.http.HttpServlet ; 39 import javax.servlet.http.HttpServletRequest ; 40 import javax.servlet.http.HttpServletResponse ; 41 42 import org.apache.commons.lang.BooleanUtils; 43 import org.apache.commons.lang.StringUtils; 44 import org.apache.log4j.Logger; 45 46 47 62 public class SimpleExchangeServlet extends HttpServlet implements SingleThreadModel { 63 64 67 private static final long serialVersionUID = 222L; 68 69 private static final String DEFAULT_ENCODING = "UTF-8"; 71 74 private static Logger log = Logger.getLogger(SimpleExchangeServlet.class); 75 76 private transient HierarchyManager hierarchyManager; 77 78 84 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 85 String context = request.getHeader(Syndicator.WORKING_CONTEXT); 86 87 log.debug("SimpleExchange.doGet()"); 89 try { 90 response.setContentType("text/plain"); response.setCharacterEncoding(DEFAULT_ENCODING); 92 94 String action = request.getHeader(Syndicator.ACTION); 95 String page = request.getHeader(Syndicator.PAGE); 96 String recursive = request.getHeader(Syndicator.RECURSIVE); 97 boolean recurse = BooleanUtils.toBoolean(recursive); 98 99 if (ConfigLoader.isConfigured() && (!Listener.isAllowed(request) || !Authenticator.authenticate(request))) { 100 return; 102 } 103 104 if (ConfigLoader.isConfigured()) { 105 this.hierarchyManager = SessionAccessControl.getHierarchyManager(request, context); 106 } 107 else { 108 this.hierarchyManager = ContentRepository.getHierarchyManager(context); 109 } 110 111 if (this.hierarchyManager == null) { 113 throw new ExchangeException("HierarchyManager is not configured for " + context); } 115 116 if (action.equals(Syndicator.ACTIVATE)) { 117 activate(request); 118 } 119 else if (action.equals(Syndicator.DE_ACTIVATE)) { 120 deactivate(request); 121 } 122 else if (action.equals(Syndicator.GET)) { 123 String type = request.getHeader(Syndicator.GET_TYPE); 124 get(page, type, recurse, response); 125 } 126 else { 127 throw new UnsupportedOperationException ("Method not supported by Exchange protocol - Simple (.01)"); } 129 } 130 catch (OutOfMemoryError e) { 131 Runtime rt = Runtime.getRuntime(); 132 log.error("---------\nOutOfMemoryError caught during activation. Total memory = " + rt.totalMemory() + ", free memory = " + rt.freeMemory() + "\n---------"); 136 } 137 catch (Throwable e) { 138 log.error(e.getMessage(), e); 139 } 140 } 141 142 148 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 149 doGet(request, response); 150 } 151 152 155 public void activate(HttpServletRequest request) throws Exception { 156 157 String page = request.getHeader(Syndicator.PAGE); 158 159 if (log.isDebugEnabled()) { 160 log.debug("Exchange : update request received for " + page); } 162 163 String parent = request.getHeader(Syndicator.PARENT); 164 String objectType = request.getHeader(Syndicator.OBJECT_TYPE); 165 String recursive = request.getHeader(Syndicator.RECURSIVE); 166 String senderContext = request.getHeader(Syndicator.SENDER_CONTEXT); 167 String context = request.getHeader(Syndicator.WORKING_CONTEXT); 168 169 String protocol = getProtocolName(request); 170 String host = request.getRemoteHost(); 171 String remotePort = request.getHeader(Syndicator.REMOTE_PORT); 172 String senderURL = request.getHeader(Syndicator.SENDER_URL); 173 174 if (StringUtils.isEmpty(senderURL)) { 175 senderURL = protocol + "://" + host + ":" + remotePort; } 177 178 String handle = StringUtils.defaultString(senderContext) + "/" + Syndicator.DEFAULT_HANDLER; 180 URL url = new URL (senderURL + handle); 181 String credentials = request.getHeader("Authorization"); URLConnection urlConnection = url.openConnection(); 183 urlConnection.setRequestProperty("Authorization", credentials); urlConnection.addRequestProperty(Syndicator.ACTION, Syndicator.GET); 185 urlConnection.addRequestProperty(Syndicator.WORKING_CONTEXT, context); 186 urlConnection.addRequestProperty(Syndicator.PAGE, page); 187 urlConnection.addRequestProperty(Syndicator.PARENT, parent); 188 urlConnection.addRequestProperty(Syndicator.GET_TYPE, Syndicator.GET_TYPE_SERIALIZED_OBJECT); 189 urlConnection.addRequestProperty(Syndicator.RECURSIVE, recursive); 190 urlConnection.addRequestProperty(Syndicator.OBJECT_TYPE, objectType); 191 InputStream in = urlConnection.getInputStream(); 193 try { 194 ObjectInputStream objectInputStream = new ObjectInputStream (in); 195 Object sc = objectInputStream.readObject(); 196 ContentWriter contentWriter = new ContentWriter(this.getHierarchyManager(), context, senderURL 198 + StringUtils.defaultString(senderContext) 199 + "/" + Syndicator.DEFAULT_HANDLER, request); 201 contentWriter.writeObject(parent, sc); 202 } 203 catch (Exception e) { 204 log.error("Failed to de-serialize - " + page); log.error(e.getMessage(), e); 206 } 207 Lock.setSystemLock(); 208 CacheHandler.flushCache(); 209 Lock.resetSystemLock(); 210 } 211 212 215 public void deactivate(HttpServletRequest request) throws Exception { 216 217 String page = request.getHeader(Syndicator.PAGE); 218 if (log.isDebugEnabled()) { 219 log.debug("Exchange : remove request received for " + page); } 221 HierarchyManager hm = this.getHierarchyManager(); 222 223 try { 224 hm.delete(page); 225 hm.save(); 226 CacheHandler.flushCache(); 227 SecureURI.delete(page); 228 SecureURI.delete(page + "/*"); } 230 catch (PathNotFoundException e) { 231 if (log.isDebugEnabled()) { 234 log.debug("Unable to deactivate node " + page + ": " + e.getMessage()); } 236 } 237 } 238 239 242 private void get(String page, String type, boolean recurse, HttpServletResponse response) throws Exception { 243 if (type.equalsIgnoreCase(Syndicator.GET_TYPE_BINARY)) { 244 if (log.isDebugEnabled()) { 246 log.debug("Binary request for " + page); } 248 HierarchyManager hm = this.getHierarchyManager(); 249 try { 250 InputStream is = hm.getNodeData(page).getValue().getStream(); 251 ServletOutputStream os = response.getOutputStream(); 252 byte[] buffer = new byte[8192]; 253 int read = 0; 254 while ((read = is.read(buffer)) > 0) { 255 os.write(buffer, 0, read); 256 } 257 os.flush(); 258 os.close(); 259 } 260 catch (PathNotFoundException e) { 261 log.error("Unable to spool " + page); throw new PathNotFoundException(e.getMessage()); 263 } 264 } 265 else { 266 if (log.isDebugEnabled()) { 268 log.debug("Serialized object request for " + page); } 270 271 Packet packet = PacketCollector.getPacket(this.getHierarchyManager(), page, recurse); 272 ObjectOutputStream os = new ObjectOutputStream (response.getOutputStream()); 273 os.writeObject(packet.getBody().getObject()); 274 os.flush(); 275 } 276 } 277 278 private HierarchyManager getHierarchyManager() throws Exception { 279 return this.hierarchyManager; 280 } 281 282 protected String getOperatedHandle(HttpServletRequest request) { 283 return request.getHeader(Syndicator.PAGE); 284 } 285 286 289 private String getProtocolName(HttpServletRequest request) { 290 String protocol = request.getProtocol(); 291 return StringUtils.substringBeforeLast(protocol, "/"); } 293 } 294
| Popular Tags
|