1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
48
49 public class RelationshipService
50 {
51 private RelationshipService( )
52 {
53 }
54
55
56
57
58 public static void getObjectRelationships( CallContext context, CmisService service, String repositoryId,
59 HttpServletRequest request, HttpServletResponse response )
60 throws Exception
61 {
62
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
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
90 response.setStatus( HttpServletResponse.SC_OK );
91 response.setContentType( Constants.MEDIATYPE_FEED );
92
93
94 AtomFeed feed = new AtomFeed( );
95 feed.startDocument( response.getOutputStream( ) );
96 feed.startFeed( true );
97
98
99 feed.writeFeedElements( objectInfo.getId( ), objectInfo.getCreatedBy( ), objectInfo.getName( ),
100 objectInfo.getLastModificationDate( ), null, relationships.getNumItems( ) );
101
102
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
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
132 feed.endFeed( );
133 feed.endDocument( );
134 }
135 }