View Javadoc

1   /**
2    * Copyright 2007 Björn Voß
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * 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" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package net.sf.oxclient.communication;
17  
18  import java.io.IOException;
19  import java.io.UnsupportedEncodingException;
20  import java.net.URLDecoder;
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import net.sf.oval.constraint.AssertFieldConstraints;
25  import net.sf.oval.constraint.NotEmpty;
26  import net.sf.oval.constraint.NotNull;
27  import net.sf.oval.guard.Guarded;
28  import net.sf.oxclient.IUser;
29  
30  import org.apache.commons.httpclient.Cookie;
31  import org.apache.commons.httpclient.HttpClient;
32  import org.apache.commons.httpclient.HttpException;
33  import org.apache.commons.httpclient.HttpMethod;
34  import org.apache.commons.httpclient.HttpState;
35  import org.apache.commons.httpclient.URI;
36  import org.apache.commons.httpclient.URIException;
37  import org.apache.commons.httpclient.methods.GetMethod;
38  
39  @Guarded
40  public class HttpCommunicationClient implements ICommunicationClient {
41  
42  	private final HttpClient httpClient;
43  	private String oxSessionid = null;
44  
45  	@NotNull
46  	@NotEmpty
47  	private String base = "/ajax/";
48  
49  	@NotNull
50  	@NotEmpty
51  	private String urlDecoding = "UTF-8";
52  
53  	@NotNull
54  	@NotEmpty
55  	private String cookiePrefix = "open-xchange-session-";
56  
57  	public HttpCommunicationClient(@NotNull final HttpClient httpClient) {
58  		this.httpClient = httpClient;
59  	}
60  
61  	public void setBase(@AssertFieldConstraints final String base) {
62  		this.base = base;
63  	}
64  
65  	public void setUrlDecoding(@AssertFieldConstraints final String urlDecoding) {
66  		this.urlDecoding = urlDecoding;
67  	}
68  
69  	public void setCookiePrefix(@AssertFieldConstraints final String cookiePrefix) {
70  		this.cookiePrefix = cookiePrefix;
71  	}
72  
73  	public String getOxSessionid() {
74  		return this.oxSessionid;
75  	}
76  
77  
78  	public void setOxSessionid(final String sessionid) {
79  		this.oxSessionid = sessionid;
80  	}
81  
82  
83  	public HttpClient getHttpClient() {
84  		return this.httpClient;
85  	}
86  
87  
88  	public String getBase() {
89  		return this.base;
90  	}
91  
92  
93  	public String getUrlDecoding() {
94  		return this.urlDecoding;
95  	}
96  
97  
98  	public String getCookiePrefix() {
99  		return this.cookiePrefix;
100 	}
101 
102 
103 	public String generateURI(
104 			final Map<String, String> params,
105 			final String modul) {
106 		final StringBuffer sb = new StringBuffer();
107 		sb.append(this.base);
108 		sb.append(modul);
109 		sb.append("?");
110 		for (final Map.Entry<String, String> entry : params.entrySet()) {
111 			sb.append(entry.getKey());
112 			sb.append("=");
113 			try {
114 				sb.append(URLDecoder.decode(entry.getValue(), this.urlDecoding));
115 			} catch (final UnsupportedEncodingException e) {
116 				throw new CommunicationException(e.getMessage(), e);
117 			}
118 			sb.append("&");
119 		}
120 		sb.append("session=");
121 		sb.append(this.oxSessionid);
122 		return sb.toString();
123 	}
124 
125 	public String execute(final HttpMethod method) {
126 		try {
127 			this.httpClient.executeMethod(method);
128 			final String response = method.getResponseBodyAsString();
129 			System.out.println(response);
130 			return response;
131 		} catch (final HttpException e) {
132 			throw new CommunicationException(e.getMessage(), e);
133 		} catch (final IOException e) {
134 			throw new CommunicationException(e.getMessage(), e);
135 		} finally {
136 			method.releaseConnection();
137 		}
138 	}
139 
140 
141 
142 	public String findSessionId() {
143 		for (final Cookie cookie : this.httpClient.getState()
144 				.getCookies()) {
145 			final String name = cookie.getName();
146 			if (name.startsWith(this.cookiePrefix)) {
147 				return name.substring(this.cookiePrefix.length(), name.length());
148 			}
149 		}
150 		return null;
151 	}
152 
153 	public String login(final IUser user) {
154 		final Map<String, String> params = new HashMap<String, String>(2);
155 		params.put("action", "login");
156 		params.put("name", user.getLogin());
157 		params.put("password", user.getPassword());
158 		final String uri = generateURI(
159 				params,
160 				"login");
161 		execute(new GetMethod(uri));
162 		this.oxSessionid = findSessionId();
163 		return this.oxSessionid;
164 	}
165 
166 	public String query(final Map<String, String> params, final String modul) {
167 		final String uri = generateURI(
168 				params,
169 				modul);
170 		System.out.println(uri);
171 		final GetMethod getMethod = new GetMethod(uri);
172 		return execute(getMethod);
173 	}
174 
175 
176 	/* (non-Javadoc)
177 	 * @see net.sf.oxclient.communication.ICommunicationClient#getOXSessionCookie()
178 	 */
179 	public Cookie getSessionCookie() {
180 		final HttpState state = this.httpClient.getState();
181 		if (state != null ) {
182 			for (final Cookie cookie : state.getCookies()) {
183 				if (cookie.getName().startsWith(this.cookiePrefix)) {
184 					return cookie;
185 				}
186 			}
187 		}
188 		return null;
189 	}
190 
191 	/* (non-Javadoc)
192 	 * @see net.sf.oxclient.communication.ICommunicationClient#getServer()
193 	 */
194 	public String getServerURL() {
195 		return this.httpClient.getHostConfiguration().getHostURL();
196 	}
197 
198 	/* (non-Javadoc)
199 	 * @see net.sf.oxclient.communication.ICommunicationClient#setServer(java.lang.String)
200 	 */
201 	public void setServerURL(final String url) {
202 		try {
203 			this.httpClient.getHostConfiguration().setHost(new URI(url, false));
204 		} catch (final URIException e) {
205 			throw new IllegalArgumentException("can't parse '"+url+"' into uri", e);
206 		}
207 	}
208 
209 
210 
211 }