View Javadoc

1   /*
2    * Copyright 2007 Hippo
3    *
4    * Licensed under the Apache License, Version 2.0 (the  "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" 
12   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
15   */
16  package nl.hippo.client.webdav.binding.facets;
17  
18  import java.io.IOException;
19  import java.io.InputStream;
20  
21  import nl.hippo.client.api.ClientException;
22  import nl.hippo.client.api.content.DocumentPath;
23  import nl.hippo.client.api.content.FacetCollection;
24  import nl.hippo.client.webdav.WebdavConfig;
25  import nl.hippo.client.webdav.WebdavResponse;
26  import nl.hippo.client.webdav.caching.WebdavCacheHandler;
27  import nl.hippo.client.webdav.method.Facets;
28  import nl.hippo.client.webdav.utils.Converters;
29  
30  import org.jibx.runtime.BindingDirectory;
31  import org.jibx.runtime.IBindingFactory;
32  import org.jibx.runtime.IUnmarshallingContext;
33  import org.jibx.runtime.JiBXException;
34  
35  public class FacetFactory {
36  
37      private static final String FACETS_XSLT = "resources/facets.xslt";
38      private static final WebdavResponse xslt;
39  
40      private static IBindingFactory facetsXMLBindingFactory;
41      static {
42          try {
43              facetsXMLBindingFactory = BindingDirectory.getFactory(FacetCollectionImpl.class);
44          } catch (JiBXException e) {
45              throw new ExceptionInInitializerError("Exception while initializing FacetBindingFactory: " + e.getMessage());
46          }
47          InputStream in = FacetFactory.class.getResourceAsStream(FACETS_XSLT);
48          try {
49              xslt = new WebdavResponse(Converters.stream2Bytes(in));
50          } catch (IOException e) {
51              throw new ExceptionInInitializerError("Exception while initializing facet searchresult transformer: "
52                      + e.getMessage());
53          }
54      }
55  
56      private WebdavConfig config;
57  
58      public FacetFactory(WebdavConfig config) {
59          if (config == null) {
60              throw new IllegalArgumentException("FacetFactory constructor parameter config may not be null.");
61          }
62          this.config = config;
63      }
64  
65      public FacetCollection fetchFacets(WebdavCacheHandler cache, DocumentPath targetPath, InputStream dasl)
66              throws ClientException {
67          if (dasl == null) {
68              throw new IllegalArgumentException("Parameter dasl may not be null");
69          }
70          try {
71              String daslAsString = new String(Converters.stream2Bytes(dasl));
72              return fetchFacets(cache, targetPath, daslAsString);
73  
74          } catch (IOException e) {
75              throw new ClientException("Exception while reading dasl from InputStream" + e.getMessage());
76          }
77      }
78  
79      public FacetCollection fetchFacets(WebdavCacheHandler cache, DocumentPath targetPath, String dasl)
80              throws ClientException {
81          Facets facets = new Facets(config);
82          WebdavResponse response = facets.execute(cache, targetPath, dasl);
83          FacetCollection result = bindFacets(cache, response);
84  
85          return result;
86      }
87  
88      private FacetCollection bindFacets(WebdavCacheHandler cache, WebdavResponse response) throws ClientException {
89          if (!response.isValid()) {
90              return null;
91          }
92  
93          FacetCollection result;
94          WebdavResponse transformedResponse = response.transform(xslt, cache);
95          try {
96              // Unmarshal the DocumentCollection xml to an Document array
97              IUnmarshallingContext context = facetsXMLBindingFactory.createUnmarshallingContext();
98              result = (FacetCollection) context.unmarshalDocument(transformedResponse.getResponseAsStream(), null);
99          } catch (JiBXException e) {
100             throw new ClientException("Unmarshalling XML to FacetCollection failed.", e);
101         }
102         return result;
103     }
104 
105 }