1 17 18 19 20 package org.apache.lenya.cms.cocoon.components.modules.input; 21 22 import java.text.SimpleDateFormat ; 23 import java.util.Arrays ; 24 import java.util.Date ; 25 import java.util.Iterator ; 26 import java.util.Map ; 27 28 import org.apache.avalon.framework.configuration.Configuration; 29 import org.apache.avalon.framework.configuration.ConfigurationException; 30 import org.apache.avalon.framework.parameters.Parameters; 31 import org.apache.avalon.framework.service.ServiceException; 32 import org.apache.avalon.framework.service.ServiceManager; 33 import org.apache.avalon.framework.service.Serviceable; 34 import org.apache.cocoon.environment.ObjectModelHelper; 35 import org.apache.cocoon.environment.Request; 36 import org.apache.commons.lang.StringUtils; 37 import org.apache.lenya.cms.cocoon.uriparameterizer.URIParameterizer; 38 import org.apache.lenya.cms.cocoon.uriparameterizer.URIParameterizerException; 39 import org.apache.lenya.cms.publication.DublinCore; 40 import org.apache.lenya.cms.publication.PageEnvelope; 41 import org.apache.lenya.cms.publication.PageEnvelopeFactory; 42 43 49 public class PageEnvelopeModule extends AbstractPageEnvelopeModule implements Serviceable { 50 51 private ServiceManager manager; 52 53 62 protected PageEnvelope getEnvelope(Map objectModel) throws ConfigurationException { 63 PageEnvelope envelope = null; 64 65 if (getLogger().isDebugEnabled()) { 66 Request request = ObjectModelHelper.getRequest(objectModel); 67 getLogger().debug("Resolving page envelope for URL [" + request.getRequestURI() + "]"); 68 } 69 70 try { 71 envelope = PageEnvelopeFactory.getInstance().getPageEnvelope(objectModel); 72 } catch (Exception e) { 73 throw new ConfigurationException("Resolving page envelope failed: ", e); 74 } 75 76 return envelope; 77 } 78 79 protected static final String URI_PARAMETER_DOCTYPE = "doctype"; 80 81 84 public Object getAttribute(String name, Configuration modeConf, Map objectModel) 85 throws ConfigurationException { 86 PageEnvelope envelope = getEnvelope(objectModel); 87 Object value = null; 88 89 try { 90 if (name.equals(PageEnvelope.AREA)) { 91 value = envelope.getDocument().getArea(); 92 } else if (name.equals(PageEnvelope.CONTEXT)) { 93 value = envelope.getContext(); 94 } else if (name.equals(PageEnvelope.PUBLICATION_ID)) { 95 value = envelope.getPublication().getId(); 96 } else if (name.equals(PageEnvelope.PUBLICATION)) { 97 value = envelope.getPublication(); 98 } else if (name.equals(PageEnvelope.PUBLICATION_LANGUAGES_CSV)) { 99 value = 100 StringUtils.join( 101 envelope.getPublication().getLanguages(), 102 ','); 103 } else if (name.equals(PageEnvelope.DOCUMENT)) { 104 value = envelope.getDocument(); 105 } else if (name.equals(PageEnvelope.DOCUMENT_ID)) { 106 value = envelope.getDocument().getId(); 107 } else if (name.equals(PageEnvelope.DOCUMENT_NAME)) { 108 value = envelope.getDocument().getName(); 109 } else if (name.equals(PageEnvelope.DOCUMENT_NODE_ID)) { value = envelope.getDocument().getNodeId(); 111 } else if (name.equals(PageEnvelope.DOCUMENT_LABEL)) { value = envelope.getDocument().getLabel(); 113 } else if (name.equals(PageEnvelope.DOCUMENT_URL)) { 114 value = envelope.getDocument().getDocumentURL(); 115 } else if (name.equals(PageEnvelope.DOCUMENT_URL_WITHOUT_LANGUAGE)) { 116 value = envelope.getDocument().getCompleteURLWithoutLanguage(); 117 } else if (name.equals(PageEnvelope.DOCUMENT_PATH)) { 118 value = envelope.getDocumentPath(); 119 } else if (name.equals(PageEnvelope.DOCUMENT_FILE)) { 120 value = envelope.getDocument().getFile(); 121 } else if (name.equals(PageEnvelope.DOCUMENT_EXTENSION)) { 122 value = envelope.getDocument().getExtension(); 123 } else if (name.equals(PageEnvelope.DEFAULT_LANGUAGE)) { 124 value = envelope.getPublication().getDefaultLanguage(); 125 } else if (name.equals(PageEnvelope.DOCUMENT_LANGUAGE)) { 126 value = envelope.getDocument().getLanguage(); 127 } else if (name.equals(PageEnvelope.DOCUMENT_LANGUAGES)) { 128 value = envelope.getDocument().getLanguages(); 129 } else if (name.equals(PageEnvelope.DOCUMENT_LANGUAGES_CSV)) { 130 value = 131 StringUtils.join( 132 envelope.getDocument().getLanguages(), 133 ','); 134 } else if (name.equals(PageEnvelope.DOCUMENT_DC_TITLE)) { 135 value = 136 envelope.getDocument().getDublinCore().getFirstValue(DublinCore.ELEMENT_TITLE); 137 } else if (name.equals(PageEnvelope.DOCUMENT_DC_CREATOR)) { 138 value = 139 envelope.getDocument().getDublinCore().getFirstValue( 140 DublinCore.ELEMENT_CREATOR); 141 } else if (name.equals(PageEnvelope.DOCUMENT_DC_PUBLISHER)) { 142 value = 143 envelope.getDocument().getDublinCore().getFirstValue( 144 DublinCore.ELEMENT_PUBLISHER); 145 } else if (name.equals(PageEnvelope.DOCUMENT_DC_SUBJECT)) { 146 value = 147 envelope.getDocument().getDublinCore().getFirstValue( 148 DublinCore.ELEMENT_SUBJECT); 149 } else if (name.equals(PageEnvelope.DOCUMENT_DC_DESCRIPTION)) { 150 value = 151 envelope.getDocument().getDublinCore().getFirstValue( 152 DublinCore.ELEMENT_DESCRIPTION); 153 } else if (name.equals(PageEnvelope.DOCUMENT_DC_RIGHTS)) { 154 value = 155 envelope.getDocument().getDublinCore().getFirstValue(DublinCore.ELEMENT_RIGHTS); 156 } else if (name.equals(PageEnvelope.DOCUMENT_DC_DATE_CREATED)) { 157 value = 158 envelope.getDocument().getDublinCore().getFirstValue(DublinCore.TERM_CREATED); 159 } else if (name.equals(PageEnvelope.DOCUMENT_LASTMODIFIED)) { 160 Date date = envelope.getDocument().getLastModified(); 161 value = new SimpleDateFormat (DATE_FORMAT).format(date); 162 } else if (name.equals(PageEnvelope.BREADCRUMB_PREFIX)) { 163 value = envelope.getPublication().getBreadcrumbPrefix(); 164 } else if (name.equals(PageEnvelope.SSL_PREFIX)) { 165 value = envelope.getPublication().getSSLPrefix(); 166 } else if (name.equals(PageEnvelope.DOCUMENT_TYPE)) { 167 value = getDocumentType(objectModel, envelope); 168 } else { 169 throw new ConfigurationException("The attribute [" + name + "] is not supported!"); 170 } 171 } catch (ConfigurationException e) { 172 throw e; 173 } catch (Exception e) { 174 throw new ConfigurationException( 175 "Getting attribute for name [" + name + "] failed: ", 176 e); 177 } 178 179 if (getLogger().isDebugEnabled()) { 180 getLogger().debug("Returning [" + name + "] = [" + value + "]"); 181 } 182 183 return value; 184 } 185 186 public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; 187 188 196 protected String getDocumentType(Map objectModel, PageEnvelope envelope) 197 throws ServiceException, URIParameterizerException { 198 String documentType; 199 URIParameterizer parameterizer = null; 200 Map map = null; 201 try { 202 parameterizer = (URIParameterizer) manager.lookup(URIParameterizer.ROLE); 203 204 Parameters parameters = new Parameters(); 205 parameters.setParameter( 206 URI_PARAMETER_DOCTYPE, 207 "cocoon://uri-parameter/" 208 + envelope.getPublication().getId() 209 + "/" 210 + URI_PARAMETER_DOCTYPE); 211 212 String source = 213 envelope.getDocument().getArea() + envelope.getDocument().getDocumentURL(); 214 215 Request request = ObjectModelHelper.getRequest(objectModel); 216 map = parameterizer.parameterize(filterURI(request.getRequestURI()), filterURI(source), parameters); 217 documentType = (String ) map.get(URI_PARAMETER_DOCTYPE); 218 219 } finally { 220 if (parameterizer != null) { 221 manager.release(parameterizer); 222 } 223 } 224 return documentType; 225 } 226 227 230 public Iterator getAttributeNames(Configuration modeConf, Map objectModel) 231 throws ConfigurationException { 232 return Arrays.asList(PageEnvelope.PARAMETER_NAMES).iterator(); 233 } 234 235 238 public Object [] getAttributeValues(String name, Configuration modeConf, Map objectModel) 239 throws ConfigurationException { 240 Object [] objects = { getAttribute(name, modeConf, objectModel)}; 241 242 return objects; 243 } 244 245 248 public void service(ServiceManager manager) throws ServiceException { 249 this.manager = manager; 250 } 251 252 258 private String filterURI(final String uri) 260 { 261 final int index = uri.indexOf(";jsessionid"); 262 263 if(index >= 0) 264 return uri.substring(0, index); 265 else 266 return uri; 267 } 268 } 269 | Popular Tags |