View Javadoc

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.client.bindings.spi.LinkAccess;
29  import org.apache.chemistry.opencmis.commons.data.ContentStream;
30  import org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException;
31  import org.apache.chemistry.opencmis.commons.impl.dataobjects.RenditionDataImpl;
32  
33  /**
34   * Implementation of <code>Rendition</code>.
35   */
36  public class RenditionImpl extends RenditionDataImpl implements Rendition {
37  
38      private static final long serialVersionUID = 1L;
39  
40      private final Session session;
41      private final String objectId;
42  
43      /**
44       * Constructor.
45       */
46      public RenditionImpl(Session session, String objectId, String streamId, String renditionDocumentId, String kind,
47              long length, String mimeType, String title, int height, int width) {
48          super(streamId, mimeType, BigInteger.valueOf(length), kind, title, BigInteger.valueOf(width), BigInteger
49                  .valueOf(height), renditionDocumentId);
50          this.session = session;
51          this.objectId = objectId;
52      }
53  
54      @Override
55      public long getLength() {
56          return getBigLength() == null ? -1 : getBigLength().longValue();
57      }
58  
59      @Override
60      public long getHeight() {
61          return getBigHeight() == null ? -1 : getBigHeight().longValue();
62      }
63  
64      @Override
65      public long getWidth() {
66          return getBigWidth() == null ? -1 : getBigWidth().longValue();
67      }
68  
69      @Override
70      public Document getRenditionDocument() {
71          return getRenditionDocument(session.getDefaultContext());
72      }
73  
74      @Override
75      public Document getRenditionDocument(OperationContext context) {
76          if (getRenditionDocumentId() == null) {
77              return null;
78          }
79          CmisObject rendDoc = session.getObject(getRenditionDocumentId(), context);
80          if (!(rendDoc instanceof Document)) {
81              return null;
82          }
83  
84          return (Document) rendDoc;
85      }
86  
87      @Override
88      public ContentStream getContentStream() {
89          if (objectId == null || getStreamId() == null) {
90              return null;
91          }
92  
93          ContentStream contentStream;
94          try {
95              contentStream = session.getBinding().getObjectService()
96                      .getContentStream(session.getRepositoryInfo().getId(), objectId, getStreamId(), null, null, null);
97          } catch (CmisConstraintException e) {
98              // no content stream
99              return null;
100         }
101 
102         if (contentStream == null) {
103             return null;
104         }
105 
106         String filename = contentStream.getFileName();
107         if (filename == null) {
108             filename = getTitle();
109         }
110         BigInteger bigLength = contentStream.getBigLength();
111         if (bigLength == null) {
112             bigLength = getBigLength();
113         }
114         long length = bigLength == null ? -1 : bigLength.longValue();
115 
116         return session.getObjectFactory().createContentStream(filename, length, contentStream.getMimeType(),
117                 contentStream.getStream());
118     }
119 
120     @Override
121     public String getContentUrl() {
122         if (session.getBinding().getObjectService() instanceof LinkAccess) {
123             LinkAccess linkAccess = (LinkAccess) session.getBinding().getObjectService();
124             return linkAccess.loadRenditionContentLink(session.getRepositoryInfo().getId(), objectId, getStreamId());
125         }
126 
127         return null;
128     }
129 }