This project has retired. For details please refer to its Attic page.
DocumentImpl xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.inmemory.storedobj.impl;
20  
21  import java.io.IOException;
22  import java.math.BigInteger;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.chemistry.opencmis.commons.PropertyIds;
27  import org.apache.chemistry.opencmis.commons.data.ContentStream;
28  import org.apache.chemistry.opencmis.commons.data.PropertyData;
29  import org.apache.chemistry.opencmis.commons.spi.BindingsObjectFactory;
30  import org.apache.chemistry.opencmis.inmemory.ConfigConstants;
31  import org.apache.chemistry.opencmis.inmemory.ConfigurationSettings;
32  import org.apache.chemistry.opencmis.inmemory.FilterParser;
33  import org.apache.chemistry.opencmis.inmemory.storedobj.api.Document;
34  import org.apache.chemistry.opencmis.inmemory.storedobj.api.Folder;
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  
38  /**
39   * InMemory Stored Document A document is a stored object that has a path and
40   * (optional) content
41   *
42   * @author Jens
43   *
44   */
45  
46  public class DocumentImpl extends AbstractMultiFilingImpl implements Document {
47      private ContentStreamDataImpl fContent;
48  
49      private static final Log LOG = LogFactory.getLog(AbstractSingleFilingImpl.class.getName());
50      private final Long MAX_CONTENT_SIZE_KB = ConfigurationSettings.getConfigurationValueAsLong(ConfigConstants.MAX_CONTENT_SIZE_KB);
51  
52      DocumentImpl(ObjectStoreImpl objStore) { // visibility should be package
53          super(objStore);
54      }
55  
56      public ContentStream getContent(long offset, long length) {
57          if (null == fContent) {
58              return null;
59          } else if (offset <= 0 && length < 0) {
60              return fContent;
61          } else {
62              return fContent.getCloneWithLimits(offset, length);
63          }
64      }
65  
66      public void setContent(ContentStream content, boolean mustPersist) {
67          if (null == content) {
68              fContent = null;
69          } else {
70              fContent = new ContentStreamDataImpl(MAX_CONTENT_SIZE_KB == null ? 0 : MAX_CONTENT_SIZE_KB);
71              String fileName = content.getFileName();
72              if (null == fileName || fileName.length() <= 0) {
73                  fileName = getName(); // use name of document as fallback
74              }
75              fContent.setFileName(fileName);
76              String mimeType = content.getMimeType();
77              if (null == mimeType || mimeType.length() <= 0) {
78                  mimeType = "application/octet-stream"; // use as fallback
79              }
80              fContent.setMimeType(mimeType);
81              try {
82                  fContent.setContent(content.getStream());
83              } catch (IOException e) {
84                  e.printStackTrace();
85                  throw new RuntimeException("Failed to get content from InputStream", e);
86              }
87          }
88      }
89  
90      @Override
91      public void fillProperties(Map<String, PropertyData<?>> properties, BindingsObjectFactory objFactory,
92              List<String> requestedIds) {
93  
94          super.fillProperties(properties, objFactory, requestedIds);
95  
96          // fill the version related properties (versions should override this
97          // but the spec requires some
98          // properties always to be set
99  
100         if (FilterParser.isContainedInFilter(PropertyIds.IS_IMMUTABLE, requestedIds)) {
101             properties.put(PropertyIds.IS_IMMUTABLE, objFactory.createPropertyBooleanData(PropertyIds.IS_IMMUTABLE,
102                     false));
103         }
104 
105         // Set the content related properties
106         if (FilterParser.isContainedInFilter(PropertyIds.CONTENT_STREAM_FILE_NAME, requestedIds)) {
107             properties.put(PropertyIds.CONTENT_STREAM_FILE_NAME, objFactory.createPropertyStringData(
108                     PropertyIds.CONTENT_STREAM_FILE_NAME, null != fContent ? fContent.getFileName() : (String)null) );
109         }
110         if (FilterParser.isContainedInFilter(PropertyIds.CONTENT_STREAM_ID, requestedIds)) {
111             properties.put(PropertyIds.CONTENT_STREAM_ID, objFactory.createPropertyStringData(
112                     PropertyIds.CONTENT_STREAM_ID, (String) null));
113         }
114         if (FilterParser.isContainedInFilter(PropertyIds.CONTENT_STREAM_LENGTH, requestedIds)) {
115             properties.put(PropertyIds.CONTENT_STREAM_LENGTH, objFactory.createPropertyIntegerData(
116                     PropertyIds.CONTENT_STREAM_LENGTH, null != fContent ? fContent.getBigLength() : BigInteger.ZERO));
117         }
118         if (FilterParser.isContainedInFilter(PropertyIds.CONTENT_STREAM_MIME_TYPE, requestedIds)) {
119             properties.put(PropertyIds.CONTENT_STREAM_MIME_TYPE, objFactory.createPropertyStringData(
120                     PropertyIds.CONTENT_STREAM_MIME_TYPE, null != fContent ? fContent.getMimeType() : (String)null) );
121         }
122         
123         // Spec requires versioning properties even for unversioned documents
124         // overwrite the version related properties
125         if (FilterParser.isContainedInFilter(PropertyIds.VERSION_SERIES_ID, requestedIds)) {
126             properties.put(PropertyIds.VERSION_SERIES_ID, objFactory.createPropertyIdData(
127                     PropertyIds.VERSION_SERIES_ID, (String)null));
128         }
129         if (FilterParser.isContainedInFilter(PropertyIds.IS_VERSION_SERIES_CHECKED_OUT, requestedIds)) {
130             properties.put(PropertyIds.IS_VERSION_SERIES_CHECKED_OUT, objFactory.createPropertyBooleanData(
131                     PropertyIds.IS_VERSION_SERIES_CHECKED_OUT, false));
132         }
133         if (FilterParser.isContainedInFilter(PropertyIds.VERSION_SERIES_CHECKED_OUT_BY, requestedIds)) {
134             properties.put(PropertyIds.VERSION_SERIES_CHECKED_OUT_BY, objFactory.createPropertyStringData(
135                     PropertyIds.VERSION_SERIES_CHECKED_OUT_BY, (String)null));
136         }
137         if (FilterParser.isContainedInFilter(PropertyIds.VERSION_SERIES_CHECKED_OUT_ID, requestedIds)) {
138             properties.put(PropertyIds.VERSION_SERIES_CHECKED_OUT_ID, objFactory.createPropertyIdData(
139                     PropertyIds.VERSION_SERIES_CHECKED_OUT_ID, (String)null));
140         }
141         if (FilterParser.isContainedInFilter(PropertyIds.IS_LATEST_VERSION, requestedIds)) {
142             properties.put(PropertyIds.IS_LATEST_VERSION, objFactory.createPropertyBooleanData(
143                     PropertyIds.IS_LATEST_VERSION, true));
144         }
145         if (FilterParser.isContainedInFilter(PropertyIds.IS_MAJOR_VERSION, requestedIds)) {
146             properties.put(PropertyIds.IS_MAJOR_VERSION, objFactory.createPropertyBooleanData(
147                     PropertyIds.IS_MAJOR_VERSION, true));
148         }
149         if (FilterParser.isContainedInFilter(PropertyIds.IS_LATEST_MAJOR_VERSION, requestedIds)) {
150             properties.put(PropertyIds.IS_LATEST_MAJOR_VERSION, objFactory.createPropertyBooleanData(
151                     PropertyIds.IS_LATEST_MAJOR_VERSION, true));
152         }
153         if (FilterParser.isContainedInFilter(PropertyIds.CHECKIN_COMMENT, requestedIds)) {
154             properties.put(PropertyIds.CHECKIN_COMMENT, objFactory.createPropertyStringData(
155                     PropertyIds.CHECKIN_COMMENT, (String )null));
156         }
157         if (FilterParser.isContainedInFilter(PropertyIds.VERSION_LABEL, requestedIds)) {
158             properties.put(PropertyIds.VERSION_LABEL, objFactory.createPropertyStringData(PropertyIds.VERSION_LABEL,
159                     (String) null));
160         }
161         
162     }
163 
164     public boolean hasContent() {
165         return null != fContent;
166     }
167 
168 }