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.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   * Policy Service operations.
44   */
45  public class PolicyService
46  {
47      private PolicyService(  )
48      {
49      }
50  
51      /**
52       * Get applied policies.
53       */
54      public static void getAppliedPolicies( CallContext context, CmisService service, String repositoryId,
55          HttpServletRequest request, HttpServletResponse response )
56          throws Exception
57      {
58          // get parameters
59          String objectId = getStringParameter( request, Constants.PARAM_ID );
60          String filter = getStringParameter( request, Constants.PARAM_FILTER );
61  
62          // execute
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          // set headers
78          response.setStatus( HttpServletResponse.SC_OK );
79          response.setContentType( Constants.MEDIATYPE_FEED );
80  
81          // write XML
82          AtomFeed feed = new AtomFeed(  );
83          feed.startDocument( response.getOutputStream(  ) );
84          feed.startFeed( true );
85  
86          // write basic Atom feed elements
87          feed.writeFeedElements( objectInfo.getId(  ), objectInfo.getCreatedBy(  ), objectInfo.getName(  ),
88              objectInfo.getLastModificationDate(  ), null, null );
89  
90          // write links
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          // write entries
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         // we are done
111         feed.endFeed(  );
112         feed.endDocument(  );
113     }
114 
115     /**
116      * Apply policy.
117      */
118     public static void applyPolicy( CallContext context, CmisService service, String repositoryId,
119         HttpServletRequest request, HttpServletResponse response )
120         throws Exception
121     {
122         // get parameters
123         String objectId = getStringParameter( request, Constants.PARAM_ID );
124 
125         AtomEntryParser parser = new AtomEntryParser( request.getInputStream(  ), context.getTempDirectory(  ),
126                 context.getMemoryThreshold(  ) );
127 
128         // execute
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         // set headers
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         // write XML
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      * Remove policy.
164      */
165     public static void removePolicy( CallContext context, CmisService service, String repositoryId,
166         HttpServletRequest request, HttpServletResponse response )
167     {
168         // get parameters
169         String objectId = getStringParameter( request, Constants.PARAM_ID );
170         String policyId = getStringParameter( request, Constants.PARAM_POLICY_ID );
171 
172         // execute
173         service.removePolicy( repositoryId, policyId, objectId, null );
174 
175         // set headers
176         response.setStatus( HttpServletResponse.SC_NO_CONTENT );
177     }
178 
179     /**
180      * Writes an entry that is attached to an object.
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         // start
200         entry.startEntry( false );
201 
202         // write the object
203         entry.writeObject( policy, info, null, null, null, null );
204 
205         // write links
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         // we are done
211         entry.endEntry(  );
212     }
213 }