1 17 package org.alfresco.web.app.servlet; 18 19 import java.io.IOException ; 20 import java.net.SocketException ; 21 import java.text.MessageFormat ; 22 import java.util.Map ; 23 import java.util.StringTokenizer ; 24 25 import javax.servlet.ServletException ; 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 28 import javax.servlet.http.HttpSession ; 29 import javax.transaction.UserTransaction ; 30 31 import org.alfresco.error.AlfrescoRuntimeException; 32 import org.alfresco.model.ContentModel; 33 import org.alfresco.service.ServiceRegistry; 34 import org.alfresco.service.cmr.repository.NodeRef; 35 import org.alfresco.service.cmr.repository.NodeService; 36 import org.alfresco.service.cmr.repository.StoreRef; 37 import org.alfresco.service.cmr.repository.TemplateException; 38 import org.alfresco.service.cmr.repository.TemplateNode; 39 import org.alfresco.service.cmr.repository.TemplateService; 40 import org.alfresco.service.cmr.security.AccessStatus; 41 import org.alfresco.service.cmr.security.PermissionService; 42 import org.alfresco.web.app.Application; 43 import org.alfresco.web.ui.repo.component.template.DefaultModelHelper; 44 import org.apache.commons.logging.Log; 45 import org.apache.commons.logging.LogFactory; 46 47 70 public class TemplateContentServlet extends BaseServlet 71 { 72 private static final String MIMETYPE_HTML = "text/html"; 73 74 private static final long serialVersionUID = -4123407921997235977L; 75 76 private static Log logger = LogFactory.getLog(TemplateContentServlet.class); 77 78 private static final String DEFAULT_URL = "/template/{0}/{1}/{2}"; 79 private static final String TEMPLATE_URL = "/template/{0}/{1}/{2}/{3}/{4}/{5}"; 80 81 private static final String MSG_ERROR_CONTENT_MISSING = "error_content_missing"; 82 83 private static final String ARG_MIMETYPE = "mimetype"; 84 85 88 protected void doGet(HttpServletRequest req, HttpServletResponse res) 89 throws ServletException , IOException 90 { 91 String uri = req.getRequestURI(); 92 93 if (logger.isDebugEnabled()) 94 logger.debug("Processing URL: " + uri + (req.getQueryString() != null ? ("?" + req.getQueryString()) : "")); 95 96 AuthenticationStatus status = servletAuthenticate(req, res); 97 if (status == AuthenticationStatus.Failure) 98 { 99 return; 100 } 101 102 StringTokenizer t = new StringTokenizer (uri, "/"); 103 int tokenCount = t.countTokens(); 104 if (tokenCount < 5) 105 { 106 throw new IllegalArgumentException ("Download URL did not contain all required args: " + uri); 107 } 108 109 t.nextToken(); t.nextToken(); 112 StoreRef storeRef = new StoreRef(t.nextToken(), t.nextToken()); 114 NodeRef nodeRef = new NodeRef(storeRef, t.nextToken()); 115 116 NodeRef templateRef = null; 118 if (tokenCount >= 8) 119 { 120 storeRef = new StoreRef(t.nextToken(), t.nextToken()); 121 templateRef = new NodeRef(storeRef, t.nextToken()); 122 } 123 124 ServiceRegistry serviceRegistry = getServiceRegistry(getServletContext()); 126 NodeService nodeService = serviceRegistry.getNodeService(); 127 TemplateService templateService = serviceRegistry.getTemplateService(); 128 PermissionService permissionService = serviceRegistry.getPermissionService(); 129 130 if (permissionService.hasPermission(nodeRef, PermissionService.READ) == AccessStatus.DENIED || 132 (templateRef != null && permissionService.hasPermission(templateRef, PermissionService.READ) == AccessStatus.DENIED)) 133 { 134 redirectToLoginPage(req, res, getServletContext()); 135 return; 136 } 137 138 String mimetype = MIMETYPE_HTML; 139 if (req.getParameter(ARG_MIMETYPE) != null) 140 { 141 mimetype = req.getParameter(ARG_MIMETYPE); 142 } 143 res.setContentType(mimetype); 144 145 try 146 { 147 UserTransaction txn = null; 148 try 149 { 150 txn = serviceRegistry.getTransactionService().getUserTransaction(true); 151 txn.begin(); 152 153 if (templateRef == null) 155 { 156 if (nodeService.hasAspect(nodeRef, ContentModel.ASPECT_TEMPLATABLE)) 157 { 158 templateRef = (NodeRef)nodeService.getProperty(nodeRef, ContentModel.PROP_TEMPLATE); 159 } 160 if (templateRef == null) 161 { 162 throw new TemplateException("Template reference not set against node or not supplied in URL."); 163 } 164 } 165 166 Object model = getModel(serviceRegistry, req.getSession(), nodeRef); 168 169 try 173 { 174 templateService.processTemplate( 175 null, 176 templateRef.toString(), 177 model, 178 res.getWriter()); 179 180 txn.commit(); 182 } 183 catch (SocketException e) 184 { 185 if (e.getMessage().contains("ClientAbortException")) 186 { 187 logger.error("Client aborted stream read:\n node: " + nodeRef + "\n template: " + templateRef); 189 try { if (txn != null) {txn.rollback();} } catch (Exception tex) {} 190 } 191 else 192 { 193 throw e; 194 } 195 } 196 } 197 catch (Throwable txnErr) 198 { 199 try { if (txn != null) {txn.rollback();} } catch (Exception tex) {} 200 throw txnErr; 201 } 202 } 203 catch (Throwable err) 204 { 205 throw new AlfrescoRuntimeException("Error during template servlet processing: " + err.getMessage(), err); 206 } 207 } 208 209 221 private Object getModel(ServiceRegistry services, HttpSession session, NodeRef nodeRef) 222 { 223 Map root = DefaultModelHelper.buildDefaultModel(services, Application.getCurrentUser(session)); 225 226 TemplateNode node = new TemplateNode(nodeRef, services, DefaultModelHelper.imageResolver); 228 root.put("space", node); 229 root.put("document", node); 230 231 return root; 232 } 233 234 244 public final static String generateURL(NodeRef nodeRef, NodeRef templateRef) 245 { 246 if (templateRef == null) 247 { 248 return MessageFormat.format(DEFAULT_URL, new Object [] { 249 nodeRef.getStoreRef().getProtocol(), 250 nodeRef.getStoreRef().getIdentifier(), 251 nodeRef.getId() } ); 252 } 253 else 254 { 255 return MessageFormat.format(TEMPLATE_URL, new Object [] { 256 nodeRef.getStoreRef().getProtocol(), 257 nodeRef.getStoreRef().getIdentifier(), 258 nodeRef.getId(), 259 templateRef.getStoreRef().getProtocol(), 260 templateRef.getStoreRef().getIdentifier(), 261 templateRef.getId()} ); 262 } 263 } 264 } 265 | Popular Tags |