View Javadoc

1   package org.astrogrid.portal.myspace.generation;
2   
3   import java.io.IOException;
4   import java.util.Map;
5   
6   import javax.xml.parsers.DocumentBuilder;
7   import javax.xml.parsers.DocumentBuilderFactory;
8   
9   import org.apache.avalon.framework.parameters.Parameters;
10  import org.apache.cocoon.ProcessingException;
11  import org.apache.cocoon.environment.ObjectModelHelper;
12  import org.apache.cocoon.environment.Request;
13  import org.apache.cocoon.environment.Session;
14  import org.apache.cocoon.environment.SourceResolver;
15  import org.apache.cocoon.generation.AbstractGenerator;
16  import org.apache.cocoon.xml.dom.DOMStreamer;
17  import org.astrogrid.portal.myspace.acting.framework.ContextWrapper;
18  import org.astrogrid.portal.myspace.acting.framework.ContextWrapperFactory;
19  import org.astrogrid.portal.myspace.acting.framework.MySpaceHandler;
20  import org.astrogrid.portal.utils.acting.ActionUtils;
21  import org.astrogrid.portal.utils.acting.ActionUtilsFactory;
22  import org.astrogrid.store.Agsl;
23  import org.astrogrid.store.delegate.StoreFile;
24  import org.w3c.dom.Document;
25  import org.w3c.dom.Element;
26  import org.xml.sax.SAXException;
27  
28  /***
29   * @author peter.shillan
30   */
31  public class StoreFilePropertiesGenerator extends AbstractGenerator {
32    private static final String STORE_FILE_PROPS = "properties";
33    private static final String STORE_FILE_CREATED = "created";
34    private static final String STORE_FILE_MIME_TYPE = "mime-type";
35    private static final String STORE_FILE_MODIFIED = "modified";
36    private static final String STORE_FILE_NAME = "name";
37    private static final String STORE_FILE_OWNER = "owner";
38    private static final String STORE_FILE_PATH = "path";
39    private static final String STORE_FILE_SIZE = "size";
40    private static final String STORE_FILE_URL = "url" ;
41  
42    private ContextWrapper context;
43  
44    /* (non-Javadoc)
45     * @see org.apache.cocoon.sitemap.SitemapModelComponent#setup(org.apache.cocoon.environment.SourceResolver, java.util.Map, java.lang.String, org.apache.avalon.framework.parameters.Parameters)
46     */
47    public void setup(SourceResolver resolver, Map objectModel, String src, Parameters params)
48        throws ProcessingException, SAXException, IOException {
49      super.setup(resolver, objectModel, src, params);
50      
51      // Get environment.
52      ActionUtils utils = ActionUtilsFactory.getActionUtils();
53      Request request = ObjectModelHelper.getRequest(objectModel);
54      Session session = request.getSession(true);
55  
56      // Create environmental context.
57      context = ContextWrapperFactory.getContextWrapper(
58          ContextWrapper.PARAM_PROTOCOL, utils, params, request, session);
59    }
60   
61    /* (non-Javadoc)
62     * @see org.apache.cocoon.generation.Generator#generate()
63     */
64    public void generate() throws IOException, SAXException, ProcessingException {
65      // TODO Auto-generated method stub
66      String src = context.getParameter(MySpaceHandler.PARAM_SRC);
67      
68      // Validate the parameters.
69      if(src != null && src.length() > 0) {
70        StoreFile storeFile = context.getStoreClient().getFile(src);
71        
72        if(storeFile == null || !storeFile.isFile()) {
73          throw new ProcessingException(src + " is not a file");
74        }
75        
76        Document document = null;
77        
78        // Create document we are generating.
79        try {
80          DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
81          DocumentBuilder builder = builderFactory.newDocumentBuilder();
82          document = builder.newDocument();
83        }
84        catch(Exception e) {
85          new ProcessingException("failed to create new document", e);
86        }
87        
88        Element rootElement = document.createElement(StoreFilePropertiesGenerator.STORE_FILE_PROPS);
89        // TODO use java.text.DateFormat
90        rootElement.setAttribute(
91            StoreFilePropertiesGenerator.STORE_FILE_CREATED,
92            storeFile.getCreated().toString());
93  
94        rootElement.setAttribute(
95            StoreFilePropertiesGenerator.STORE_FILE_MIME_TYPE,
96            storeFile.getMimeType());
97        
98        // TODO use java.text.DateFormat
99  /*      rootElement.setAttribute(
100           StoreFilePropertiesGenerator.STORE_FILE_MODIFIED,
101           storeFile.getModified().toLocaleString());
102 */      
103       rootElement.setAttribute(
104           StoreFilePropertiesGenerator.STORE_FILE_NAME,
105           storeFile.getName());
106       
107 //      rootElement.setAttribute(
108 //          StoreFilePropertiesGenerator.STORE_FILE_OWNER,
109 //          storeFile.getOwner());
110       
111       //JL. This is a hack. The above is the kosher way.
112       rootElement.setAttribute(
113               StoreFilePropertiesGenerator.STORE_FILE_OWNER,
114               context.getUser().getUserId() );
115       
116       rootElement.setAttribute(
117           StoreFilePropertiesGenerator.STORE_FILE_PATH,
118           storeFile.getPath());
119       
120       rootElement.setAttribute(
121           StoreFilePropertiesGenerator.STORE_FILE_SIZE,
122           Long.toString(storeFile.getSize()));
123       
124       Agsl agsl = context.getStoreClient().getAgsl(storeFile.getPath());
125       
126       rootElement.setAttribute(
127           StoreFilePropertiesGenerator.STORE_FILE_URL,
128           agsl.resolveURL().toString() );
129 
130       document.appendChild(rootElement);
131 
132       // Stream the fully produced document.
133       DOMStreamer streamer = new DOMStreamer();
134       streamer.setNormalizeNamespaces(false);
135       streamer.setContentHandler(contentHandler);
136       streamer.stream(document);
137     }
138     else {
139       throw new ProcessingException("invalid store file");
140     }
141   }
142 }