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.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   * Repository Service operations.
54   */
55  public final class RepositoryService
56  {
57      private RepositoryService(  )
58      {
59      }
60  
61      /**
62       * getRepositories.
63       */
64      public static void getRepositories( CallContext context, CmisService service, HttpServletRequest request,
65          HttpServletResponse response ) throws Exception
66      {
67          // execute
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       * getRepositoryInfo.
86       */
87      public static void getRepositoryInfo( CallContext context, CmisService service, String repositoryId,
88          HttpServletRequest request, HttpServletResponse response )
89          throws Exception
90      {
91          // execute
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      * getLastResult.
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      * getTypeChildren.
154      */
155     public static void getTypeChildren( CallContext context, CmisService service, String repositoryId,
156         HttpServletRequest request, HttpServletResponse response )
157         throws Exception
158     {
159         // get parameters
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         // execute
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         // get parameters
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         // execute
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         // get parameters
208         String typeId = getStringParameter( request, PARAM_TYPE_ID );
209 
210         // execute
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 }