This project has retired. For details please refer to its Attic page.
RenditionImpl 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.client.runtime;
20  
21  import java.math.BigInteger;
22  
23  import org.apache.chemistry.opencmis.client.api.CmisObject;
24  import org.apache.chemistry.opencmis.client.api.Document;
25  import org.apache.chemistry.opencmis.client.api.OperationContext;
26  import org.apache.chemistry.opencmis.client.api.Rendition;
27  import org.apache.chemistry.opencmis.client.api.Session;
28  import org.apache.chemistry.opencmis.commons.data.ContentStream;
29  import org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException;
30  import org.apache.chemistry.opencmis.commons.impl.dataobjects.RenditionDataImpl;
31  
32  /**
33   * Implementation of <code>Rendition</code>.
34   */
35  public class RenditionImpl extends RenditionDataImpl implements Rendition {
36  
37      private static final long serialVersionUID = 1L;
38  
39      private final Session session;
40      private final String objectId;
41  
42      /**
43       * Constructor.
44       */
45      public RenditionImpl(Session session, String objectId, String streamId, String renditionDocumentId, String kind,
46              long length, String mimeType, String title, int height, int width) {
47          super(streamId, mimeType, BigInteger.valueOf(length), kind, title, BigInteger.valueOf(width), BigInteger
48                  .valueOf(height), renditionDocumentId);
49          this.session = session;
50          this.objectId = objectId;
51      }
52  
53      public long getLength() {
54          return length == null ? -1 : length.longValue();
55      }
56  
57      public long getHeight() {
58          return height == null ? -1 : height.longValue();
59      }
60  
61      public long getWidth() {
62          return width == null ? -1 : width.longValue();
63      }
64  
65      public Document getRenditionDocument() {
66          return getRenditionDocument(session.getDefaultContext());
67      }
68  
69      public Document getRenditionDocument(OperationContext context) {
70          if (renditionDocumentId == null) {
71              return null;
72          }
73          CmisObject rendDoc = session.getObject(session.createObjectId(renditionDocumentId), context);
74          if (!(rendDoc instanceof Document)) {
75              return null;
76          }
77  
78          return (Document) rendDoc;
79      }
80  
81      public ContentStream getContentStream() {
82          if ((objectId == null) || (streamId == null)) {
83              return null;
84          }
85  
86          ContentStream contentStream;
87          try {
88              contentStream = session.getBinding().getObjectService()
89                      .getContentStream(session.getRepositoryInfo().getId(), objectId, streamId, null, null, null);
90          } catch (CmisConstraintException e) {
91              // no content stream
92              return null;
93          }
94  
95          if (contentStream == null) {
96              return null;
97          }
98  
99          String filename = contentStream.getFileName();
100         if (filename == null) {
101             filename = getTitle();
102         }
103         BigInteger bigLength = contentStream.getBigLength();
104         if (bigLength == null) {
105             bigLength = getBigLength();
106         }
107         long length = bigLength == null ? -1 : bigLength.longValue();
108 
109         return session.getObjectFactory().createContentStream(filename, length,
110                 contentStream.getMimeType(), contentStream.getStream());
111     }
112 
113 }