View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors. 
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   *
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  
25  package org.modeshape.sequencer.zip;
26  
27  import java.io.ByteArrayOutputStream;
28  import java.io.File;
29  import java.io.InputStream;
30  import java.util.zip.ZipEntry;
31  import java.util.zip.ZipInputStream;
32  import org.modeshape.graph.JcrLexicon;
33  import org.modeshape.graph.JcrNtLexicon;
34  import org.modeshape.graph.ModeShapeLexicon;
35  import org.modeshape.graph.property.BinaryFactory;
36  import org.modeshape.graph.property.DateTimeFactory;
37  import org.modeshape.graph.property.NameFactory;
38  import org.modeshape.graph.property.Path;
39  import org.modeshape.graph.property.PathFactory;
40  import org.modeshape.graph.sequencer.SequencerOutput;
41  import org.modeshape.graph.sequencer.StreamSequencer;
42  import org.modeshape.graph.sequencer.StreamSequencerContext;
43  
44  /**
45   * A sequencer that processes and extract metadata from ZIP files.
46   */
47  public class ZipSequencer implements StreamSequencer {
48  
49      /**
50       * {@inheritDoc}
51       * 
52       * @see org.modeshape.graph.sequencer.StreamSequencer#sequence(java.io.InputStream,
53       *      org.modeshape.graph.sequencer.SequencerOutput, org.modeshape.graph.sequencer.StreamSequencerContext)
54       */
55      public void sequence( InputStream stream,
56                            SequencerOutput output,
57                            StreamSequencerContext context ) {
58          BinaryFactory binaryFactory = context.getValueFactories().getBinaryFactory();
59          DateTimeFactory dateFactory = context.getValueFactories().getDateFactory();
60          PathFactory pathFactory = context.getValueFactories().getPathFactory();
61          NameFactory nameFactory = context.getValueFactories().getNameFactory();
62  
63          try {
64              ZipInputStream in = new ZipInputStream(stream);
65              ZipEntry entry = in.getNextEntry();
66              byte[] buf = new byte[1024];
67  
68              // Create top-level node
69              Path zipPath = pathFactory.createRelativePath(ZipLexicon.CONTENT);
70              output.setProperty(zipPath, JcrLexicon.PRIMARY_TYPE, ZipLexicon.CONTENT);
71              while (entry != null) {
72                  Path entryPath = zipPath;
73                  String entryName = entry.getName();
74                  for (String segment : entryName.split(File.separator)) {
75                      entryPath = pathFactory.create(entryPath, nameFactory.create(segment));
76                  }
77  
78                  if (entry.isDirectory()) { // If entry is directory, create nt:folder node
79                      output.setProperty(entryPath, JcrLexicon.PRIMARY_TYPE, JcrNtLexicon.FOLDER);
80                  } else { // If entry is File, create nt:file
81                      output.setProperty(entryPath, JcrLexicon.PRIMARY_TYPE, JcrNtLexicon.FILE);
82  
83                      Path contentPath = pathFactory.create(entryPath, JcrLexicon.CONTENT);
84                      output.setProperty(contentPath, JcrLexicon.PRIMARY_TYPE, ModeShapeLexicon.RESOURCE);
85                      int n;
86                      ByteArrayOutputStream baout = new ByteArrayOutputStream();
87                      while ((n = in.read(buf, 0, 1024)) > -1) {
88                          baout.write(buf, 0, n);
89                      }
90                      byte[] bytes = baout.toByteArray();
91                      output.setProperty(contentPath, JcrLexicon.DATA, binaryFactory.create(bytes));
92                      // all other nt:file properties should be generated by other sequencers (mimetype, encoding,...) but we'll
93                      // default them here
94                      output.setProperty(contentPath, JcrLexicon.ENCODED, "binary");
95                      output.setProperty(contentPath, JcrLexicon.LAST_MODIFIED,
96                                         dateFactory.create(entry.getTime()).toString());
97                      output.setProperty(contentPath, JcrLexicon.MIMETYPE,
98                                         "application/octet-stream");
99  
100                 }
101                 in.closeEntry();
102                 entry = in.getNextEntry();
103 
104             }
105         } catch (Exception e) {
106             e.printStackTrace();
107         }
108 
109     }
110 }