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.server.impl.atompub;
20  
21  import org.apache.chemistry.opencmis.commons.data.ObjectData;
22  import org.apache.chemistry.opencmis.commons.data.ObjectList;
23  import org.apache.chemistry.opencmis.commons.enums.RelationshipDirection;
24  import org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException;
25  import org.apache.chemistry.opencmis.commons.impl.Constants;
26  import org.apache.chemistry.opencmis.commons.impl.UrlBuilder;
27  import org.apache.chemistry.opencmis.commons.server.CallContext;
28  import org.apache.chemistry.opencmis.commons.server.CmisService;
29  import org.apache.chemistry.opencmis.commons.server.ObjectInfo;
30  import static org.apache.chemistry.opencmis.server.impl.atompub.AtomPubUtils.RESOURCE_RELATIONSHIPS;
31  import static org.apache.chemistry.opencmis.server.impl.atompub.AtomPubUtils.compileBaseUrl;
32  import static org.apache.chemistry.opencmis.server.impl.atompub.AtomPubUtils.compileUrl;
33  import static org.apache.chemistry.opencmis.server.impl.atompub.AtomPubUtils.compileUrlBuilder;
34  import static org.apache.chemistry.opencmis.server.impl.atompub.AtomPubUtils.writeObjectEntry;
35  import static org.apache.chemistry.opencmis.server.shared.HttpUtils.getBigIntegerParameter;
36  import static org.apache.chemistry.opencmis.server.shared.HttpUtils.getBooleanParameter;
37  import static org.apache.chemistry.opencmis.server.shared.HttpUtils.getEnumParameter;
38  import static org.apache.chemistry.opencmis.server.shared.HttpUtils.getStringParameter;
39  
40  import java.math.BigInteger;
41  
42  import javax.servlet.http.HttpServletRequest;
43  import javax.servlet.http.HttpServletResponse;
44  
45  
46  /**
47   * Relationship Service operations.
48   */
49  public class RelationshipService
50  {
51      private RelationshipService(  )
52      {
53      }
54  
55      /**
56       * Get object relationships.
57       */
58      public static void getObjectRelationships( CallContext context, CmisService service, String repositoryId,
59          HttpServletRequest request, HttpServletResponse response )
60          throws Exception
61      {
62          // get parameters
63          String objectId = getStringParameter( request, Constants.PARAM_ID );
64          Boolean includeSubRelationshipTypes = getBooleanParameter( request, Constants.PARAM_SUB_RELATIONSHIP_TYPES );
65          RelationshipDirection relationshipDirection = getEnumParameter( request,
66                  Constants.PARAM_RELATIONSHIP_DIRECTION, RelationshipDirection.class );
67          String typeId = getStringParameter( request, Constants.PARAM_TYPE_ID );
68          String filter = getStringParameter( request, Constants.PARAM_FILTER );
69          Boolean includeAllowableActions = getBooleanParameter( request, Constants.PARAM_ALLOWABLE_ACTIONS );
70          BigInteger maxItems = getBigIntegerParameter( request, Constants.PARAM_MAX_ITEMS );
71          BigInteger skipCount = getBigIntegerParameter( request, Constants.PARAM_SKIP_COUNT );
72  
73          // execute
74          ObjectList relationships = service.getObjectRelationships( repositoryId, objectId, includeSubRelationshipTypes,
75                  relationshipDirection, typeId, filter, includeAllowableActions, maxItems, skipCount, null );
76  
77          if ( relationships == null )
78          {
79              throw new CmisRuntimeException( "Relationships are null!" );
80          }
81  
82          ObjectInfo objectInfo = service.getObjectInfo( repositoryId, objectId );
83  
84          if ( objectInfo == null )
85          {
86              throw new CmisRuntimeException( "Object Info is missing!" );
87          }
88  
89          // set headers
90          response.setStatus( HttpServletResponse.SC_OK );
91          response.setContentType( Constants.MEDIATYPE_FEED );
92  
93          // write XML
94          AtomFeed feed = new AtomFeed(  );
95          feed.startDocument( response.getOutputStream(  ) );
96          feed.startFeed( true );
97  
98          // write basic Atom feed elements
99          feed.writeFeedElements( objectInfo.getId(  ), objectInfo.getCreatedBy(  ), objectInfo.getName(  ),
100             objectInfo.getLastModificationDate(  ), null, relationships.getNumItems(  ) );
101 
102         // write links
103         UrlBuilder baseUrl = compileBaseUrl( request, repositoryId );
104 
105         feed.writeServiceLink( baseUrl.toString(  ), repositoryId );
106 
107         feed.writeSelfLink( compileUrl( baseUrl, RESOURCE_RELATIONSHIPS, objectInfo.getId(  ) ), null );
108 
109         UrlBuilder pagingUrl = new UrlBuilder( compileUrlBuilder( baseUrl, RESOURCE_RELATIONSHIPS, objectInfo.getId(  ) ) );
110         pagingUrl.addParameter( Constants.PARAM_SUB_RELATIONSHIP_TYPES, includeSubRelationshipTypes );
111         pagingUrl.addParameter( Constants.PARAM_RELATIONSHIP_DIRECTION, relationshipDirection );
112         pagingUrl.addParameter( Constants.PARAM_TYPE_ID, typeId );
113         pagingUrl.addParameter( Constants.PARAM_FILTER, filter );
114         pagingUrl.addParameter( Constants.PARAM_ALLOWABLE_ACTIONS, includeAllowableActions );
115         feed.writePagingLinks( pagingUrl, maxItems, skipCount, relationships.getNumItems(  ),
116             relationships.hasMoreItems(  ), AtomPubUtils.PAGE_SIZE );
117 
118         // write entries
119         AtomEntry entry = new AtomEntry( feed.getWriter(  ) );
120 
121         for ( ObjectData object : relationships.getObjects(  ) )
122         {
123             if ( object == null )
124             {
125                 continue;
126             }
127 
128             writeObjectEntry( service, entry, object, null, repositoryId, null, null, baseUrl, false );
129         }
130 
131         // we are done
132         feed.endFeed(  );
133         feed.endDocument(  );
134     }
135 }