This project has retired. For details please refer to its Attic page.
RepositoryService xref

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