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.client;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertFalse;
23  import static org.junit.Assert.assertNotNull;
24  import static org.junit.Assert.assertNull;
25  
26  import java.io.File;
27  import java.io.IOException;
28  import java.util.List;
29  import java.util.Locale;
30  import java.util.Map;
31  
32  import javax.net.ssl.HostnameVerifier;
33  import javax.net.ssl.SSLSocketFactory;
34  import javax.xml.ws.handler.HandlerResolver;
35  
36  import org.apache.chemistry.opencmis.commons.SessionParameter;
37  import org.apache.chemistry.opencmis.commons.enums.BindingType;
38  import org.apache.chemistry.opencmis.commons.server.CallContext;
39  import org.apache.chemistry.opencmis.commons.server.CmisService;
40  import org.apache.chemistry.opencmis.commons.server.CmisServiceFactory;
41  import org.apache.chemistry.opencmis.commons.server.TempStoreOutputStream;
42  import org.apache.chemistry.opencmis.commons.spi.AuthenticationProvider;
43  import org.junit.Test;
44  import org.w3c.dom.Element;
45  
46  public class SessionParameterMapTest {
47  
48      @Test
49      public void testMap() throws IOException {
50          SessionParameterMap map = new SessionParameterMap();
51  
52          // bindings
53          map.setAtomPubBindingUrl("http://atomoub/url");
54          assertEquals(BindingType.ATOMPUB.value(), map.get(SessionParameter.BINDING_TYPE));
55          assertEquals("http://atomoub/url", map.get(SessionParameter.ATOMPUB_URL));
56  
57          map.setWebServicesBindingUrl("http://webservices/url");
58          assertEquals(BindingType.WEBSERVICES.value(), map.get(SessionParameter.BINDING_TYPE));
59          assertEquals("http://webservices/url", map.get(SessionParameter.WEBSERVICES_REPOSITORY_SERVICE));
60          assertEquals("http://webservices/url", map.get(SessionParameter.WEBSERVICES_NAVIGATION_SERVICE));
61          assertEquals("http://webservices/url", map.get(SessionParameter.WEBSERVICES_OBJECT_SERVICE));
62          assertEquals("http://webservices/url", map.get(SessionParameter.WEBSERVICES_VERSIONING_SERVICE));
63          assertEquals("http://webservices/url", map.get(SessionParameter.WEBSERVICES_DISCOVERY_SERVICE));
64          assertEquals("http://webservices/url", map.get(SessionParameter.WEBSERVICES_MULTIFILING_SERVICE));
65          assertEquals("http://webservices/url", map.get(SessionParameter.WEBSERVICES_RELATIONSHIP_SERVICE));
66          assertEquals("http://webservices/url", map.get(SessionParameter.WEBSERVICES_ACL_SERVICE));
67          assertEquals("http://webservices/url", map.get(SessionParameter.WEBSERVICES_POLICY_SERVICE));
68  
69          map.setBrowserBindingUrl("http://browser/url");
70          assertEquals(BindingType.BROWSER.value(), map.get(SessionParameter.BINDING_TYPE));
71          assertEquals("http://browser/url", map.get(SessionParameter.BROWSER_URL));
72  
73          map.setLocalBindingClass(TestLocalSessionFactory.class);
74          assertEquals(BindingType.LOCAL.value(), map.get(SessionParameter.BINDING_TYPE));
75          assertEquals(TestLocalSessionFactory.class.getName(), map.get(SessionParameter.LOCAL_FACTORY));
76  
77          map.setAtomPubBindingUrl(null);
78          assertNull(map.get(SessionParameter.BINDING_TYPE));
79          assertNull(map.get(SessionParameter.ATOMPUB_URL));
80  
81          // user and password
82          map.setUserAndPassword("user", "password");
83          assertEquals("user", map.get(SessionParameter.USER));
84          assertEquals("password", map.get(SessionParameter.PASSWORD));
85  
86          map.setUserAndPassword(null, "password");
87          assertFalse(map.containsKey(SessionParameter.USER));
88          assertFalse(map.containsKey(SessionParameter.PASSWORD));
89  
90          map.setProxyUserAndPassword("user", "password");
91          assertEquals("user", map.get(SessionParameter.PROXY_USER));
92          assertEquals("password", map.get(SessionParameter.PROXY_PASSWORD));
93  
94          // repository id
95          map.setRepositoryId("repid");
96          assertEquals("repid", map.get(SessionParameter.REPOSITORY_ID));
97          map.setRepositoryId(null);
98          assertFalse(map.containsKey(SessionParameter.REPOSITORY_ID));
99  
100         // authentication
101         map.setAuthenticationProvider(TestAuthenticationProvider.class);
102         assertEquals(TestAuthenticationProvider.class.getName(),
103                 map.get(SessionParameter.AUTHENTICATION_PROVIDER_CLASS));
104 
105         map.setAuthenticationProvider(null);
106         assertNull(map.get(SessionParameter.AUTHENTICATION_PROVIDER_CLASS));
107 
108         map.setNtlmAuthentication("user", "password");
109         assertEquals("user", map.get(SessionParameter.USER));
110         assertEquals("password", map.get(SessionParameter.PASSWORD));
111         assertEquals("false", map.get(SessionParameter.AUTH_HTTP_BASIC));
112         assertEquals("false", map.get(SessionParameter.AUTH_SOAP_USERNAMETOKEN));
113         assertNotNull(map.get(SessionParameter.AUTHENTICATION_PROVIDER_CLASS));
114 
115         map.setBasicAuthentication("user1", "password1");
116         assertEquals("user1", map.get(SessionParameter.USER));
117         assertEquals("password1", map.get(SessionParameter.PASSWORD));
118         assertEquals("true", map.get(SessionParameter.AUTH_HTTP_BASIC));
119         assertEquals("false", map.get(SessionParameter.AUTH_SOAP_USERNAMETOKEN));
120 
121         map.setUsernameTokenAuthentication("user2", "password2", true);
122         assertEquals("user2", map.get(SessionParameter.USER));
123         assertEquals("password2", map.get(SessionParameter.PASSWORD));
124         assertEquals("true", map.get(SessionParameter.AUTH_HTTP_BASIC));
125         assertEquals("true", map.get(SessionParameter.AUTH_SOAP_USERNAMETOKEN));
126 
127         map.setNoAuthentication();
128         assertEquals("false", map.get(SessionParameter.AUTH_HTTP_BASIC));
129         assertEquals("false", map.get(SessionParameter.AUTH_SOAP_USERNAMETOKEN));
130 
131         // locale
132         map.setLocale(new Locale("de", "DE"));
133         assertEquals("de", map.get(SessionParameter.LOCALE_ISO639_LANGUAGE));
134         assertEquals("DE", map.get(SessionParameter.LOCALE_ISO3166_COUNTRY));
135 
136         // HTTP related
137         map.setCookies(true);
138         assertEquals("true", map.get(SessionParameter.COOKIES));
139         map.setCookies(false);
140         assertEquals("false", map.get(SessionParameter.COOKIES));
141 
142         map.setCompression(true);
143         assertEquals("true", map.get(SessionParameter.COMPRESSION));
144 
145         map.setClientCompression(false);
146         assertEquals("false", map.get(SessionParameter.CLIENT_COMPRESSION));
147 
148         map.setConnectionTimeout(12345);
149         assertEquals("12345", map.get(SessionParameter.CONNECT_TIMEOUT));
150 
151         map.setReadTimeout(98765);
152         assertEquals("98765", map.get(SessionParameter.READ_TIMEOUT));
153 
154         // header
155         map.addHeader(null, "value");
156         map.addHeader("header0", "value0");
157         map.addHeader("header1", "value1");
158         map.addHeader("header2", "value2");
159 
160         assertEquals("header0:value0", map.get(SessionParameter.HEADER + ".0"));
161         assertEquals("header1:value1", map.get(SessionParameter.HEADER + ".1"));
162         assertEquals("header2:value2", map.get(SessionParameter.HEADER + ".2"));
163 
164         // store and load
165         File tmp = File.createTempFile("session", "parameters");
166         try {
167             map.store(tmp);
168 
169             SessionParameterMap map2 = new SessionParameterMap();
170             map2.load(tmp);
171 
172             assertEquals(map.size(), map2.size());
173             for (String key : map.keySet()) {
174                 assertEquals(map.get(key), map2.get(key));
175             }
176         } finally {
177             tmp.delete();
178         }
179 
180         // parse
181         String parameters = map.toString();
182         SessionParameterMap map2 = new SessionParameterMap();
183         map2.parse(parameters);
184 
185         assertEquals(map.size(), map2.size());
186         for (String key : map.keySet()) {
187             assertEquals(map.get(key), map2.get(key));
188         }
189     }
190 
191     private class TestAuthenticationProvider implements AuthenticationProvider {
192 
193         private static final long serialVersionUID = 1L;
194 
195         @Override
196         public Map<String, List<String>> getHTTPHeaders(String url) {
197             return null;
198         }
199 
200         @Override
201         public Element getSOAPHeaders(Object portObject) {
202             return null;
203         }
204 
205         @Override
206         public HandlerResolver getHandlerResolver() {
207             return null;
208         }
209 
210         @Override
211         public SSLSocketFactory getSSLSocketFactory() {
212             return null;
213         }
214 
215         @Override
216         public HostnameVerifier getHostnameVerifier() {
217             return null;
218         }
219 
220         @Override
221         public void putResponseHeaders(String url, int statusCode, Map<String, List<String>> headers) {
222         }
223     }
224 
225     private class TestLocalSessionFactory implements CmisServiceFactory {
226 
227         @Override
228         public void init(Map<String, String> parameters) {
229         }
230 
231         @Override
232         public void destroy() {
233         }
234 
235         @Override
236         public CmisService getService(CallContext context) {
237             return null;
238         }
239 
240         @Override
241         public File getTempDirectory() {
242             return null;
243         }
244 
245         @Override
246         public boolean encryptTempFiles() {
247             return false;
248         }
249 
250         @Override
251         public int getMemoryThreshold() {
252             return 0;
253         }
254 
255         @Override
256         public long getMaxContentSize() {
257             return 0;
258         }
259 
260         @Override
261         public TempStoreOutputStream getTempFileOutputStream(String repositoryId) {
262             return null;
263         }
264     }
265 }