View Javadoc
1   /*
2    * JBoss, Home of Professional Open Source
3    * Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
4    * contributors by the @authors tag. See the copyright.txt in the
5    * distribution for a full listing of individual contributors.
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * http://www.apache.org/licenses/LICENSE-2.0
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.jboss.as.quickstarts.jaxrsclient;
18  
19  /**
20   * This example demonstrates the use an external JAX-RS RestEasy client
21   * which interacts with a JAX-RS Web service that uses CDI 1.0 and JAX-RS 
22   * in Red Hat JBoss Enterprise Application Platform 6.  Specifically, 
23   * this client "calls" the HelloWorld JAX-RS Web Service created in  
24   * quickstart helloworld-rs.  Please refer to the helloworld-rs README.md 
25   * for instructions on how to build and deploy helloworld-rs.
26   */
27  import static org.junit.Assert.*;
28  
29  import java.io.BufferedReader;
30  import java.io.ByteArrayInputStream;
31  import java.io.IOException;
32  import java.io.InputStreamReader;
33  
34  import javax.ws.rs.core.MediaType;
35  
36  import org.apache.http.client.ClientProtocolException;
37  import org.jboss.resteasy.client.ClientRequest;
38  import org.jboss.resteasy.client.ClientResponse;
39  import org.junit.BeforeClass;
40  import org.junit.Test;
41  
42  /**
43   * JUnit4 Test class which makes a request to the RESTful helloworld-rs web service.
44   * 
45   * @author bmincey (Blaine Mincey)
46   * 
47   */
48  public class JaxRsClientTest {
49      /**
50       * Request URLs pulled from system properties in pom.xml
51       */
52      private static String XML_URL;
53      private static String JSON_URL;
54  
55      /**
56       * Property names used to pull values from system properties in pom.xml
57       */
58      private static final String XML_PROPERTY = "xmlUrl";
59      private static final String JSON_PROPERTY = "jsonUrl";
60  
61      /**
62       * Responses of the RESTful web service
63       */
64      private static final String XML_RESPONSE = "<xml><result>Hello World!</result></xml>";
65      private static final String JSON_RESPONSE = "{\"result\":\"Hello World!\"}";
66  
67      /**
68       * Method executes BEFORE the test method. Values are read from system properties that can be modified in the pom.xml.
69       */
70      @BeforeClass
71      public static void beforeClass() {
72          JaxRsClientTest.XML_URL = System.getProperty(JaxRsClientTest.XML_PROPERTY);
73          JaxRsClientTest.JSON_URL = System.getProperty(JaxRsClientTest.JSON_PROPERTY);
74      }
75  
76      /**
77       * Test method which executes the runRequest method that calls the RESTful helloworld-rs web service.
78       */
79      @Test
80      public void test() {
81          assertEquals("XML Response", JaxRsClientTest.XML_RESPONSE,
82                  this.runRequest(JaxRsClientTest.XML_URL, MediaType.APPLICATION_XML_TYPE));
83  
84          assertEquals("JSON Response", JaxRsClientTest.JSON_RESPONSE,
85                  this.runRequest(JaxRsClientTest.JSON_URL, MediaType.APPLICATION_JSON_TYPE));
86      }
87  
88      /**
89       * The purpose of this method is to run the external REST request.
90       * 
91       * @param url The url of the RESTful service
92       * @param mediaType The mediatype of the RESTful service
93       */
94      private String runRequest(String url, MediaType mediaType) {
95          String result = null;
96  
97          System.out.println("===============================================");
98          System.out.println("URL: " + url);
99          System.out.println("MediaType: " + mediaType.toString());
100 
101         try {
102             // Using the RESTEasy libraries, initiate a client request
103             // using the url as a parameter
104             ClientRequest request = new ClientRequest(url);
105 
106             // Be sure to set the mediatype of the request
107             request.accept(mediaType);
108 
109             // Request has been made, now let's get the response
110             ClientResponse<String> response = request.get(String.class);
111 
112             // Check the HTTP status of the request
113             // HTTP 200 indicates the request is OK
114             if (response.getStatus() != 200) {
115                 throw new RuntimeException("Failed request with HTTP status: " + response.getStatus());
116             }
117 
118             // We have a good response, let's now read it
119             BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(response.getEntity()
120                     .getBytes())));
121 
122             // Loop over the br in order to print out the contents
123             System.out.println("\n*** Response from Server ***\n");
124             String output = null;
125             while ((output = br.readLine()) != null) {
126                 System.out.println(output);
127                 result = output;
128             }
129         } catch (ClientProtocolException cpe) {
130             System.err.println(cpe);
131         } catch (IOException ioe) {
132             System.err.println(ioe);
133         } catch (Exception e) {
134             System.err.println(e);
135         }
136 
137         System.out.println("\n===============================================");
138 
139         return result;
140     }
141 
142 }