KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > http > HttpSpringTest


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.http;
18
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.File JavaDoc;
21
22 import javax.jbi.messaging.ExchangeStatus;
23 import javax.jbi.messaging.InOut;
24 import javax.xml.namespace.QName JavaDoc;
25
26 import org.apache.commons.httpclient.HttpClient;
27 import org.apache.commons.httpclient.methods.PostMethod;
28 import org.apache.commons.httpclient.methods.RequestEntity;
29 import org.apache.commons.httpclient.methods.multipart.FilePart;
30 import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
31 import org.apache.commons.httpclient.methods.multipart.Part;
32 import org.apache.commons.httpclient.methods.multipart.StringPart;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.apache.servicemix.client.DefaultServiceMixClient;
36 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
37 import org.apache.servicemix.jbi.jaxp.StringSource;
38 import org.apache.servicemix.tck.SpringTestSupport;
39 import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
40 import org.springframework.context.support.AbstractXmlApplicationContext;
41
42 public class HttpSpringTest extends SpringTestSupport {
43
44     private static Log logger = LogFactory.getLog(HttpSpringTest.class);
45
46     protected void setUp() throws Exception JavaDoc {
47         System.setProperty("javax.net.debug", "all");
48         super.setUp();
49     }
50     
51     public void test() throws Exception JavaDoc {
52         DefaultServiceMixClient client = new DefaultServiceMixClient(jbi);
53         InOut me = client.createInOutExchange();
54         me.setService(new QName JavaDoc("http://test", "MyProviderService"));
55         me.getInMessage().setContent(new StringSource("<echo xmlns='http://test'><echoin0>world</echoin0></echo>"));
56         client.sendSync(me);
57         if (me.getStatus() == ExchangeStatus.ERROR) {
58             if (me.getFault() != null) {
59                 fail("Received fault: " + new SourceTransformer().toString(me.getFault().getContent()));
60             } else if (me.getError() != null) {
61                 throw me.getError();
62             } else {
63                 fail("Received ERROR status");
64             }
65         } else {
66             logger.info(new SourceTransformer().toString(me.getOutMessage().getContent()));
67         }
68     }
69
70     public void testSsl() throws Exception JavaDoc {
71         DefaultServiceMixClient client = new DefaultServiceMixClient(jbi);
72         InOut me = client.createInOutExchange();
73         me.setService(new QName JavaDoc("http://test/ssl", "MyProviderService"));
74         me.getInMessage().setContent(new StringSource("<echo xmlns='http://test'><echoin0>world</echoin0></echo>"));
75         client.sendSync(me);
76         if (me.getStatus() == ExchangeStatus.ERROR) {
77             if (me.getFault() != null) {
78                 fail("Received fault: " + new SourceTransformer().toString(me.getFault().getContent()));
79             } else if (me.getError() != null) {
80                 throw me.getError();
81             } else {
82                 fail("Received ERROR status");
83             }
84         } else {
85             logger.info(new SourceTransformer().toString(me.getOutMessage().getContent()));
86         }
87     }
88
89     public void testMimeWithHttpClient() throws Exception JavaDoc {
90         File JavaDoc f = new File JavaDoc(getClass().getResource("servicemix.jpg").getFile());
91         PostMethod filePost = new PostMethod("http://localhost:8192/Service/");
92         Part[] parts = {
93             new StringPart("request", "<dummy/>"),
94             new FilePart(f.getName(), f) };
95         RequestEntity entity = new MultipartRequestEntity(parts, filePost.getParams());
96         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
97         entity.writeRequest(baos);
98         System.err.println(baos);
99         filePost.setRequestEntity(entity);
100         HttpClient client = new HttpClient();
101         int status = client.executeMethod(filePost);
102         assertEquals(200, status);
103         filePost.releaseConnection();
104     }
105
106     protected AbstractXmlApplicationContext createBeanFactory() {
107         return new ClassPathXmlApplicationContext("org/apache/servicemix/http/spring.xml");
108     }
109     
110 }
111
Popular Tags