001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018: package org.apache.roller.webservices.adminapi;
019:
020: import java.io.IOException;
021: import java.io.InputStream;
022: import org.apache.commons.httpclient.HttpClient;
023: import org.apache.commons.httpclient.HttpMethod;
024: import org.apache.commons.httpclient.methods.DeleteMethod;
025: import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
026: import org.apache.commons.httpclient.methods.GetMethod;
027: import org.apache.commons.httpclient.methods.PostMethod;
028: import org.apache.commons.httpclient.methods.PutMethod;
029: import org.apache.commons.httpclient.util.Base64;
030: import org.apache.roller.webservices.adminapi.sdk.MemberEntry;
031: import org.apache.roller.webservices.adminapi.sdk.MemberEntrySet;
032: import org.apache.roller.webservices.adminapi.sdk.UnexpectedRootElementException;
033: import org.apache.roller.webservices.adminapi.sdk.UserEntry;
034: import org.apache.roller.webservices.adminapi.sdk.UserEntrySet;
035: import org.apache.roller.webservices.adminapi.sdk.WeblogEntry;
036: import org.apache.roller.webservices.adminapi.sdk.WeblogEntrySet;
037: import org.jdom.JDOMException;
038:
039: public abstract class HandlerBaseTest extends AappTest {
040: public static class HttpResponse {
041: private int status;
042: private InputStream responseBody;
043:
044: public HttpResponse(int status) {
045: this (status, null);
046: }
047:
048: public HttpResponse(int status, InputStream responseBody) {
049: this .status = status;
050: this .responseBody = responseBody;
051: }
052:
053: public int getStatus() {
054: return status;
055: }
056:
057: public InputStream getResponseBody() {
058: return responseBody;
059: }
060: }
061:
062: protected void setUp() throws Exception {
063: //System.err.println("HandlerTest.setUp()");
064: try {
065: deleteSampleMember(false);
066: } catch (Throwable t) {
067: t.printStackTrace();
068: }
069: try {
070: deleteSampleWeblog(false);
071: } catch (Throwable t) {
072: t.printStackTrace();
073: }
074: try {
075: deleteSampleUser(false);
076: } catch (Throwable t) {
077: t.printStackTrace();
078: }
079: }
080:
081: protected void tearDown() throws Exception {
082: //System.err.println("HandlerTest.tearDown()");
083: try {
084: deleteSampleMember(false);
085: } catch (Throwable t) {
086: t.printStackTrace();
087: }
088: try {
089: deleteSampleWeblog(false);
090: } catch (Throwable t) {
091: t.printStackTrace();
092: }
093: try {
094: deleteSampleUser(false);
095: } catch (Throwable t) {
096: t.printStackTrace();
097: }
098: }
099:
100: protected UserEntrySet createSampleUser() throws IOException,
101: JDOMException, UnexpectedRootElementException {
102: UserEntrySet ues = getSampleUserEntrySet();
103:
104: String url = ues.getHref();
105: String user = getUser();
106: String password = getPassword();
107:
108: String body = ues.toString();
109:
110: HttpResponse res = post(url, user, password, body);
111: assertEquals(201, res.getStatus());
112:
113: UserEntrySet uesResponse = null;
114: InputStream responseBody = res.getResponseBody();
115: if (responseBody != null) {
116: uesResponse = new UserEntrySet(responseBody,
117: getEndpointUrl());
118: }
119:
120: return uesResponse;
121: }
122:
123: protected UserEntrySet updateSampleUser() throws IOException,
124: JDOMException, UnexpectedRootElementException {
125: UserEntrySet ues = updateSampleUserEntrySet(getSampleUserEntrySet());
126:
127: String url = ues.getHref();
128: String user = getUser();
129: String password = getPassword();
130:
131: String body = ues.toString();
132:
133: HttpResponse res = put(url, user, password, body);
134: assertEquals(200, res.getStatus());
135:
136: UserEntrySet uesResponse = null;
137: InputStream responseBody = res.getResponseBody();
138: if (responseBody != null) {
139: uesResponse = new UserEntrySet(responseBody,
140: getEndpointUrl());
141: }
142:
143: return uesResponse;
144: }
145:
146: protected WeblogEntrySet updateSampleWeblog() throws IOException,
147: JDOMException, UnexpectedRootElementException {
148: WeblogEntrySet wes = updateSampleWeblogEntrySet(getSampleWeblogEntrySet());
149:
150: String url = wes.getHref();
151: String user = getUser();
152: String password = getPassword();
153:
154: String body = wes.toString();
155:
156: HttpResponse res = put(url, user, password, body);
157: assertEquals(200, res.getStatus());
158:
159: WeblogEntrySet wesResponse = null;
160: InputStream responseBody = res.getResponseBody();
161: if (responseBody != null) {
162: wesResponse = new WeblogEntrySet(responseBody,
163: getEndpointUrl());
164: }
165:
166: return wesResponse;
167: }
168:
169: protected MemberEntrySet updateSampleMember() throws IOException,
170: JDOMException, UnexpectedRootElementException {
171: MemberEntrySet mes = updateSampleMemberEntrySet(getSampleMemberEntrySet());
172:
173: String url = mes.getHref();
174: String user = getUser();
175: String password = getPassword();
176:
177: String body = mes.toString();
178:
179: HttpResponse res = put(url, user, password, body);
180: assertEquals(200, res.getStatus());
181:
182: MemberEntrySet mesResponse = null;
183: InputStream responseBody = res.getResponseBody();
184: if (responseBody != null) {
185: mesResponse = new MemberEntrySet(responseBody,
186: getEndpointUrl());
187: }
188:
189: return mesResponse;
190: }
191:
192: protected WeblogEntrySet createSampleWeblog() throws IOException,
193: JDOMException, UnexpectedRootElementException {
194: WeblogEntrySet wes = getSampleWeblogEntrySet();
195:
196: String url = wes.getHref();
197: String user = getUser();
198: String password = getPassword();
199:
200: String body = wes.toString();
201:
202: HttpResponse res = post(url, user, password, body);
203: assertEquals(201, res.getStatus());
204:
205: WeblogEntrySet wesResponse = null;
206: InputStream responseBody = res.getResponseBody();
207: if (responseBody != null) {
208: wesResponse = new WeblogEntrySet(responseBody,
209: getEndpointUrl());
210: }
211:
212: return wesResponse;
213: }
214:
215: protected MemberEntrySet createSampleMember() throws IOException,
216: JDOMException, UnexpectedRootElementException {
217: MemberEntrySet mes = getSampleMemberEntrySet();
218:
219: String url = mes.getHref();
220: String user = getUser();
221: String password = getPassword();
222:
223: String body = mes.toString();
224:
225: HttpResponse res = post(url, user, password, body);
226: assertEquals(201, res.getStatus());
227:
228: MemberEntrySet mesResponse = null;
229: InputStream responseBody = res.getResponseBody();
230: if (responseBody != null) {
231: mesResponse = new MemberEntrySet(responseBody,
232: getEndpointUrl());
233: }
234:
235: return mesResponse;
236: }
237:
238: protected UserEntrySet deleteSampleUser(boolean test)
239: throws IOException, JDOMException,
240: UnexpectedRootElementException {
241: UserEntry ue = getSampleUserEntry();
242:
243: HttpResponse res = delete(ue.getHref(), getUser(),
244: getPassword());
245: if (test) {
246: assertEquals(200, res.getStatus());
247: } else if (res.getStatus() != 200) {
248: return null;
249: }
250:
251: UserEntrySet uesResponse = null;
252: InputStream responseBody = res.getResponseBody();
253: if (responseBody != null) {
254: uesResponse = new UserEntrySet(responseBody,
255: getEndpointUrl());
256: }
257:
258: return uesResponse;
259: }
260:
261: protected UserEntrySet fetchSampleUser() throws IOException,
262: JDOMException, UnexpectedRootElementException {
263: UserEntry ue = getSampleUserEntry();
264:
265: HttpResponse res = get(ue.getHref(), getUser(), getPassword());
266: assertEquals(200, res.getStatus());
267:
268: UserEntrySet uesResponse = null;
269: InputStream responseBody = res.getResponseBody();
270: if (responseBody != null) {
271: uesResponse = new UserEntrySet(responseBody,
272: getEndpointUrl());
273: }
274:
275: return uesResponse;
276: }
277:
278: protected WeblogEntrySet fetchSampleWeblog() throws IOException,
279: JDOMException, UnexpectedRootElementException {
280: WeblogEntry we = getSampleWeblogEntry();
281:
282: HttpResponse res = get(we.getHref(), getUser(), getPassword());
283: assertEquals(200, res.getStatus());
284:
285: WeblogEntrySet wesResponse = null;
286: InputStream responseBody = res.getResponseBody();
287: if (responseBody != null) {
288: wesResponse = new WeblogEntrySet(responseBody,
289: getEndpointUrl());
290: }
291:
292: return wesResponse;
293: }
294:
295: protected MemberEntrySet fetchSampleMember() throws IOException,
296: JDOMException, UnexpectedRootElementException {
297: MemberEntry me = getSampleMemberEntry();
298:
299: HttpResponse res = get(me.getHref(), getUser(), getPassword());
300: assertEquals(200, res.getStatus());
301:
302: MemberEntrySet mesResponse = null;
303: InputStream responseBody = res.getResponseBody();
304: if (responseBody != null) {
305: mesResponse = new MemberEntrySet(responseBody,
306: getEndpointUrl());
307: }
308:
309: return mesResponse;
310: }
311:
312: protected WeblogEntrySet deleteSampleWeblog(boolean test)
313: throws IOException, JDOMException,
314: UnexpectedRootElementException {
315: WeblogEntry we = getSampleWeblogEntry();
316:
317: HttpResponse res = delete(we.getHref(), getUser(),
318: getPassword());
319: if (test) {
320: assertEquals(200, res.getStatus());
321: } else if (res.getStatus() != 200) {
322: return null;
323: }
324:
325: WeblogEntrySet wesResponse = null;
326: InputStream responseBody = res.getResponseBody();
327: if (responseBody != null) {
328: wesResponse = new WeblogEntrySet(responseBody,
329: getEndpointUrl());
330: }
331:
332: return wesResponse;
333: }
334:
335: protected MemberEntrySet deleteSampleMember(boolean test)
336: throws IOException, JDOMException,
337: UnexpectedRootElementException {
338: MemberEntry me = getSampleMemberEntry();
339:
340: HttpResponse res = delete(me.getHref(), getUser(),
341: getPassword());
342: if (test) {
343: assertEquals(200, res.getStatus());
344: } else if (res.getStatus() != 200) {
345: return null;
346: }
347:
348: MemberEntrySet mesResponse = null;
349: InputStream responseBody = res.getResponseBody();
350: if (responseBody != null) {
351: mesResponse = new MemberEntrySet(responseBody,
352: getEndpointUrl());
353: }
354:
355: return mesResponse;
356: }
357:
358: protected static HttpResponse post(String url, String user,
359: String password, String body) throws IOException {
360: HttpClient httpClient = new HttpClient();
361: EntityEnclosingMethod method = new PostMethod(url);
362: addAuthHeader(method, user, password);
363:
364: method.setRequestBody(body);
365:
366: String contentType = "application/xml; charset=utf-8";
367: method.setRequestHeader("Content-type", contentType);
368:
369: int status = httpClient.executeMethod(method);
370: InputStream responseBody = method.getResponseBodyAsStream();
371:
372: HttpResponse res = new HttpResponse(status, responseBody);
373: return res;
374: }
375:
376: protected static HttpResponse put(String url, String user,
377: String password, String body) throws IOException {
378: HttpClient httpClient = new HttpClient();
379: EntityEnclosingMethod method = new PutMethod(url);
380: addAuthHeader(method, user, password);
381:
382: method.setRequestBody(body);
383:
384: String contentType = "application/xml; charset=utf-8";
385: method.setRequestHeader("Content-type", contentType);
386:
387: int status = httpClient.executeMethod(method);
388: InputStream responseBody = method.getResponseBodyAsStream();
389:
390: HttpResponse res = new HttpResponse(status, responseBody);
391: return res;
392: }
393:
394: protected static HttpResponse get(String url, String user,
395: String password) throws IOException {
396: HttpClient httpClient = new HttpClient();
397: HttpMethod method = new GetMethod(url);
398: addAuthHeader(method, user, password);
399:
400: String contentType = "application/xml; charset=utf-8";
401: method.setRequestHeader("Content-type", contentType);
402:
403: int status = httpClient.executeMethod(method);
404: InputStream responseBody = method.getResponseBodyAsStream();
405:
406: HttpResponse res = new HttpResponse(status, responseBody);
407: return res;
408: }
409:
410: protected static HttpResponse delete(String url, String user,
411: String password) throws IOException {
412: HttpClient httpClient = new HttpClient();
413: HttpMethod method = new DeleteMethod(url);
414: addAuthHeader(method, user, password);
415:
416: String contentType = "application/xml; charset=utf-8";
417: method.setRequestHeader("Content-type", contentType);
418:
419: int status = httpClient.executeMethod(method);
420: InputStream responseBody = method.getResponseBodyAsStream();
421:
422: HttpResponse res = new HttpResponse(status, responseBody);
423: return res;
424: }
425:
426: private static void addAuthHeader(HttpMethod method, String user,
427: String password) {
428: String credentials = user + ":" + password;
429: method.setRequestHeader("Authorization", "Basic "
430: + new String(Base64.encode(credentials.getBytes())));
431: }
432:
433: }
|