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.RepositoryInfo;
22 import org.apache.chemistry.opencmis.commons.definitions.TypeDefinition;
23 import org.apache.chemistry.opencmis.commons.definitions.TypeDefinitionContainer;
24 import org.apache.chemistry.opencmis.commons.definitions.TypeDefinitionList;
25 import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
26 import org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException;
27 import static org.apache.chemistry.opencmis.commons.impl.Constants.PARAM_DEPTH;
28 import static org.apache.chemistry.opencmis.commons.impl.Constants.PARAM_MAX_ITEMS;
29 import static org.apache.chemistry.opencmis.commons.impl.Constants.PARAM_PROPERTY_DEFINITIONS;
30 import static org.apache.chemistry.opencmis.commons.impl.Constants.PARAM_SKIP_COUNT;
31 import static org.apache.chemistry.opencmis.commons.impl.Constants.PARAM_TRANSACTION;
32 import static org.apache.chemistry.opencmis.commons.impl.Constants.PARAM_TYPE_ID;
33 import org.apache.chemistry.opencmis.commons.impl.JSONConverter;
34 import org.apache.chemistry.opencmis.commons.impl.json.JSONArray;
35 import org.apache.chemistry.opencmis.commons.impl.json.JSONObject;
36 import org.apache.chemistry.opencmis.commons.impl.json.JSONValue;
37 import org.apache.chemistry.opencmis.commons.server.CallContext;
38 import org.apache.chemistry.opencmis.commons.server.CmisService;
39 import static org.apache.chemistry.opencmis.server.shared.HttpUtils.getBigIntegerParameter;
40 import static org.apache.chemistry.opencmis.server.shared.HttpUtils.getBooleanParameter;
41 import static org.apache.chemistry.opencmis.server.shared.HttpUtils.getStringParameter;
42
43 import java.math.BigInteger;
44
45 import java.util.List;
46
47 import javax.servlet.http.Cookie;
48 import javax.servlet.http.HttpServletRequest;
49 import javax.servlet.http.HttpServletResponse;
50
51
52
53
54
55 public final class RepositoryService
56 {
57 private RepositoryService( )
58 {
59 }
60
61
62
63
64 public static void getRepositories( CallContext context, CmisService service, HttpServletRequest request,
65 HttpServletResponse response ) throws Exception
66 {
67
68 List<RepositoryInfo> infoDataList = service.getRepositoryInfos( null );
69
70 JSONObject result = new JSONObject( );
71
72 for ( RepositoryInfo ri : infoDataList )
73 {
74 String repositoryUrl = BrowserBindingUtils.compileRepositoryUrl( request, ri.getId( ) ).toString( );
75 String rootUrl = BrowserBindingUtils.compileRootUrl( request, ri.getId( ) ).toString( );
76
77 result.put( ri.getId( ), JSONConverter.convert( ri, repositoryUrl, rootUrl ) );
78 }
79
80 response.setStatus( HttpServletResponse.SC_OK );
81 BrowserBindingUtils.writeJSON( result, request, response );
82 }
83
84
85
86
87 public static void getRepositoryInfo( CallContext context, CmisService service, String repositoryId,
88 HttpServletRequest request, HttpServletResponse response )
89 throws Exception
90 {
91
92 RepositoryInfo ri = service.getRepositoryInfo( repositoryId, null );
93
94 String repositoryUrl = BrowserBindingUtils.compileRepositoryUrl( request, ri.getId( ) ).toString( );
95 String rootUrl = BrowserBindingUtils.compileRootUrl( request, ri.getId( ) ).toString( );
96
97 JSONObject result = new JSONObject( );
98 result.put( ri.getId( ), JSONConverter.convert( ri, repositoryUrl, rootUrl ) );
99
100 response.setStatus( HttpServletResponse.SC_OK );
101 BrowserBindingUtils.writeJSON( result, request, response );
102 }
103
104
105
106
107 public static void getLastResult( CallContext context, CmisService service, String repositoryId,
108 HttpServletRequest request, HttpServletResponse response )
109 throws Exception
110 {
111 String transaction = getStringParameter( request, PARAM_TRANSACTION );
112 String cookieName = BrowserBindingUtils.getCookieName( transaction );
113 String cookieValue = null;
114
115 if ( request.getCookies( ) != null )
116 {
117 for ( Cookie cookie : request.getCookies( ) )
118 {
119 if ( cookieName.equals( cookie.getName( ) ) )
120 {
121 cookieValue = cookie.getValue( );
122
123 break;
124 }
125 }
126 }
127
128 try
129 {
130 if ( cookieValue == null )
131 {
132 cookieValue = BrowserBindingUtils.createCookieValue( 0, null,
133 CmisInvalidArgumentException.EXCEPTION_NAME, "Unknown transaction!" );
134 }
135 else
136 {
137 JSONValue.parse( cookieValue );
138 }
139 }
140 catch ( Exception pe )
141 {
142 cookieValue = BrowserBindingUtils.createCookieValue( 0, null, CmisRuntimeException.EXCEPTION_NAME,
143 "Cookie pasring error!" );
144 }
145
146 BrowserBindingUtils.deleteCookie( request, response, repositoryId, transaction );
147
148 response.setStatus( HttpServletResponse.SC_OK );
149 BrowserBindingUtils.writeJSON( (JSONObject) JSONValue.parse( cookieValue ), request, response );
150 }
151
152
153
154
155 public static void getTypeChildren( CallContext context, CmisService service, String repositoryId,
156 HttpServletRequest request, HttpServletResponse response )
157 throws Exception
158 {
159
160 String typeId = getStringParameter( request, PARAM_TYPE_ID );
161 boolean includePropertyDefinitions = getBooleanParameter( request, PARAM_PROPERTY_DEFINITIONS, false );
162 BigInteger maxItems = getBigIntegerParameter( request, PARAM_MAX_ITEMS );
163 BigInteger skipCount = getBigIntegerParameter( request, PARAM_SKIP_COUNT );
164
165
166 TypeDefinitionList typeList = service.getTypeChildren( repositoryId, typeId, includePropertyDefinitions,
167 maxItems, skipCount, null );
168 JSONObject jsonTypeList = JSONConverter.convert( typeList );
169
170 response.setStatus( HttpServletResponse.SC_OK );
171 BrowserBindingUtils.writeJSON( jsonTypeList, request, response );
172 }
173
174 public static void getTypeDescendants( CallContext context, CmisService service, String repositoryId,
175 HttpServletRequest request, HttpServletResponse response )
176 throws Exception
177 {
178
179 String typeId = getStringParameter( request, PARAM_TYPE_ID );
180 BigInteger depth = getBigIntegerParameter( request, PARAM_DEPTH );
181 boolean includePropertyDefinitions = getBooleanParameter( request, PARAM_PROPERTY_DEFINITIONS, false );
182
183
184 List<TypeDefinitionContainer> typeTree = service.getTypeDescendants( repositoryId, typeId, depth,
185 includePropertyDefinitions, null );
186
187 if ( typeTree == null )
188 {
189 throw new CmisRuntimeException( "Type tree is null!" );
190 }
191
192 JSONArray jsonTypeTree = new JSONArray( );
193
194 for ( TypeDefinitionContainer container : typeTree )
195 {
196 jsonTypeTree.add( JSONConverter.convert( container ) );
197 }
198
199 response.setStatus( HttpServletResponse.SC_OK );
200 BrowserBindingUtils.writeJSON( jsonTypeTree, request, response );
201 }
202
203 public static void getTypeDefinition( CallContext context, CmisService service, String repositoryId,
204 HttpServletRequest request, HttpServletResponse response )
205 throws Exception
206 {
207
208 String typeId = getStringParameter( request, PARAM_TYPE_ID );
209
210
211 TypeDefinition type = service.getTypeDefinition( repositoryId, typeId, null );
212 JSONObject jsonType = JSONConverter.convert( type );
213
214 response.setStatus( HttpServletResponse.SC_OK );
215 BrowserBindingUtils.writeJSON( jsonType, request, response );
216 }
217 }