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.browser;
20
21 import org.apache.chemistry.opencmis.commons.data.ObjectData;
22 import org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException;
23 import static org.apache.chemistry.opencmis.commons.impl.Constants.PARAM_FILTER;
24 import static org.apache.chemistry.opencmis.commons.impl.Constants.PARAM_POLICY_ID;
25 import org.apache.chemistry.opencmis.commons.impl.JSONConverter;
26 import org.apache.chemistry.opencmis.commons.impl.TypeCache;
27 import org.apache.chemistry.opencmis.commons.impl.json.JSONArray;
28 import org.apache.chemistry.opencmis.commons.impl.json.JSONObject;
29 import org.apache.chemistry.opencmis.commons.impl.server.TypeCacheImpl;
30 import org.apache.chemistry.opencmis.commons.server.CallContext;
31 import org.apache.chemistry.opencmis.commons.server.CmisService;
32 import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.CONTEXT_OBJECT_ID;
33 import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.getSimpleObject;
34 import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.writeJSON;
35 import static org.apache.chemistry.opencmis.server.shared.HttpUtils.getStringParameter;
36
37 import java.util.List;
38
39 import javax.servlet.http.HttpServletRequest;
40 import javax.servlet.http.HttpServletResponse;
41
42
43
44
45
46 public class PolicyService
47 {
48
49
50
51 public static void getAppliedPolicies( CallContext context, CmisService service, String repositoryId,
52 HttpServletRequest request, HttpServletResponse response )
53 throws Exception
54 {
55
56 String objectId = (String) context.get( CONTEXT_OBJECT_ID );
57 String filter = getStringParameter( request, PARAM_FILTER );
58
59
60 List<ObjectData> policies = service.getAppliedPolicies( repositoryId, objectId, filter, null );
61
62 JSONArray jsonPolicies = new JSONArray( );
63
64 if ( policies != null )
65 {
66 TypeCache typeCache = new TypeCacheImpl( repositoryId, service );
67
68 for ( ObjectData policy : policies )
69 {
70 jsonPolicies.add( JSONConverter.convert( policy, typeCache, false ) );
71 }
72 }
73
74 response.setStatus( HttpServletResponse.SC_OK );
75 writeJSON( jsonPolicies, request, response );
76 }
77
78
79
80
81 public static void applyPolicy( CallContext context, CmisService service, String repositoryId,
82 HttpServletRequest request, HttpServletResponse response )
83 throws Exception
84 {
85
86 String objectId = (String) context.get( CONTEXT_OBJECT_ID );
87 String policyId = getStringParameter( request, PARAM_POLICY_ID );
88
89
90 service.applyPolicy( repositoryId, policyId, objectId, null );
91
92 ObjectData object = getSimpleObject( service, repositoryId, objectId );
93
94 if ( object == null )
95 {
96 throw new CmisRuntimeException( "Object is null!" );
97 }
98
99
100 response.setStatus( HttpServletResponse.SC_OK );
101
102 TypeCache typeCache = new TypeCacheImpl( repositoryId, service );
103 JSONObject jsonObject = JSONConverter.convert( object, typeCache, false );
104
105 writeJSON( jsonObject, request, response );
106 }
107
108
109
110
111 public static void removePolicy( CallContext context, CmisService service, String repositoryId,
112 HttpServletRequest request, HttpServletResponse response )
113 throws Exception
114 {
115
116 String objectId = (String) context.get( CONTEXT_OBJECT_ID );
117 String policyId = getStringParameter( request, PARAM_POLICY_ID );
118
119
120 service.removePolicy( repositoryId, policyId, objectId, null );
121
122 ObjectData object = getSimpleObject( service, repositoryId, objectId );
123
124 if ( object == null )
125 {
126 throw new CmisRuntimeException( "Object is null!" );
127 }
128
129
130 response.setStatus( HttpServletResponse.SC_OK );
131
132 TypeCache typeCache = new TypeCacheImpl( repositoryId, service );
133 JSONObject jsonObject = JSONConverter.convert( object, typeCache, false );
134
135 writeJSON( jsonObject, request, response );
136 }
137 }