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.exceptions.CmisRuntimeException;
23  import org.apache.chemistry.opencmis.commons.impl.Constants;
24  import static org.apache.chemistry.opencmis.commons.impl.Converter.convert;
25  import org.apache.chemistry.opencmis.commons.impl.UrlBuilder;
26  import org.apache.chemistry.opencmis.commons.impl.jaxb.CmisObjectType;
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_POLICIES;
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.shared.HttpUtils.getStringParameter;
35  
36  import java.util.List;
37  
38  import javax.servlet.http.HttpServletRequest;
39  import javax.servlet.http.HttpServletResponse;
40  
41  
42  
43  
44  
45  public class PolicyService
46  {
47      private PolicyService(  )
48      {
49      }
50  
51      
52  
53  
54      public static void getAppliedPolicies( CallContext context, CmisService service, String repositoryId,
55          HttpServletRequest request, HttpServletResponse response )
56          throws Exception
57      {
58          
59          String objectId = getStringParameter( request, Constants.PARAM_ID );
60          String filter = getStringParameter( request, Constants.PARAM_FILTER );
61  
62          
63          List<ObjectData> policies = service.getAppliedPolicies( repositoryId, objectId, filter, null );
64  
65          if ( policies == null )
66          {
67              throw new CmisRuntimeException( "Policies are null!" );
68          }
69  
70          ObjectInfo objectInfo = service.getObjectInfo( repositoryId, objectId );
71  
72          if ( objectInfo == null )
73          {
74              throw new CmisRuntimeException( "Object Info is missing!" );
75          }
76  
77          
78          response.setStatus( HttpServletResponse.SC_OK );
79          response.setContentType( Constants.MEDIATYPE_FEED );
80  
81          
82          AtomFeed feed = new AtomFeed(  );
83          feed.startDocument( response.getOutputStream(  ) );
84          feed.startFeed( true );
85  
86          
87          feed.writeFeedElements( objectInfo.getId(  ), objectInfo.getCreatedBy(  ), objectInfo.getName(  ),
88              objectInfo.getLastModificationDate(  ), null, null );
89  
90          
91          UrlBuilder baseUrl = compileBaseUrl( request, repositoryId );
92  
93          feed.writeServiceLink( baseUrl.toString(  ), repositoryId );
94  
95          feed.writeSelfLink( compileUrl( baseUrl, RESOURCE_POLICIES, objectInfo.getId(  ) ), null );
96  
97          
98          AtomEntry entry = new AtomEntry( feed.getWriter(  ) );
99  
100         for ( ObjectData policy : policies )
101         {
102             if ( policy == null )
103             {
104                 continue;
105             }
106 
107             writePolicyEntry( service, entry, objectInfo.getId(  ), policy, repositoryId, baseUrl );
108         }
109 
110         
111         feed.endFeed(  );
112         feed.endDocument(  );
113     }
114 
115     
116 
117 
118     public static void applyPolicy( CallContext context, CmisService service, String repositoryId,
119         HttpServletRequest request, HttpServletResponse response )
120         throws Exception
121     {
122         
123         String objectId = getStringParameter( request, Constants.PARAM_ID );
124 
125         AtomEntryParser parser = new AtomEntryParser( request.getInputStream(  ), context.getTempDirectory(  ),
126                 context.getMemoryThreshold(  ) );
127 
128         
129         service.applyPolicy( repositoryId, parser.getId(  ), objectId, null );
130 
131         ObjectInfo objectInfo = service.getObjectInfo( repositoryId, parser.getId(  ) );
132 
133         if ( objectInfo == null )
134         {
135             throw new CmisRuntimeException( "Object Info is missing!" );
136         }
137 
138         ObjectData policy = objectInfo.getObject(  );
139 
140         if ( policy == null )
141         {
142             throw new CmisRuntimeException( "Policy is null!" );
143         }
144 
145         
146         UrlBuilder baseUrl = compileBaseUrl( request, repositoryId );
147         UrlBuilder location = compileUrlBuilder( baseUrl, RESOURCE_POLICIES, objectId );
148         location.addParameter( Constants.PARAM_POLICY_ID, policy.getId(  ) );
149 
150         response.setStatus( HttpServletResponse.SC_CREATED );
151         response.setContentType( Constants.MEDIATYPE_ENTRY );
152         response.setHeader( "Content-Location", location.toString(  ) );
153         response.setHeader( "Location", location.toString(  ) );
154 
155         
156         AtomEntry entry = new AtomEntry(  );
157         entry.startDocument( response.getOutputStream(  ) );
158         writePolicyEntry( service, entry, objectId, policy, repositoryId, baseUrl );
159         entry.endDocument(  );
160     }
161 
162     
163 
164 
165     public static void removePolicy( CallContext context, CmisService service, String repositoryId,
166         HttpServletRequest request, HttpServletResponse response )
167     {
168         
169         String objectId = getStringParameter( request, Constants.PARAM_ID );
170         String policyId = getStringParameter( request, Constants.PARAM_POLICY_ID );
171 
172         
173         service.removePolicy( repositoryId, policyId, objectId, null );
174 
175         
176         response.setStatus( HttpServletResponse.SC_NO_CONTENT );
177     }
178 
179     
180 
181 
182     private static void writePolicyEntry( CmisService service, AtomEntry entry, String objectId, ObjectData policy,
183         String repositoryId, UrlBuilder baseUrl ) throws Exception
184     {
185         CmisObjectType resultJaxb = convert( policy );
186 
187         if ( resultJaxb == null )
188         {
189             return;
190         }
191 
192         ObjectInfo info = service.getObjectInfo( repositoryId, policy.getId(  ) );
193 
194         if ( info == null )
195         {
196             throw new CmisRuntimeException( "Object Info not found!" );
197         }
198 
199         
200         entry.startEntry( false );
201 
202         
203         entry.writeObject( policy, info, null, null, null, null );
204 
205         
206         UrlBuilder selfLink = compileUrlBuilder( baseUrl, RESOURCE_POLICIES, objectId );
207         selfLink.addParameter( Constants.PARAM_POLICY_ID, info.getId(  ) );
208         entry.writeSelfLink( selfLink.toString(  ), null );
209 
210         
211         entry.endEntry(  );
212     }
213 }