summaryrefslogtreecommitdiffstats
path: root/java/sca/samples/webapps/helloworld-rest/src/main/java/helloworldrest/ClientTestServiceWebapp.java
blob: 31ff9561852f2bcd6817432ecea14ccf6c0b3dd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */
package helloworldrest;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Formatter;

/**
 *
 * To test, deploy the application as a webapp.
 * Then, run this file to access the REST web service by making HTTP GET/POST requests 
 * 
 */
public class ClientTestServiceWebapp {

    final static String UrlBase = "http://localhost:8080/helloworld-rest-webapp/HelloWorldService";

    final static class HttpResponse {
        Object content;
        int code;
        String message;
    }

    static HttpResponse makeHttpGetRequest(String method, String url, String contentType) throws Exception {
        HttpResponse response = new HttpResponse();
        URL urlAddress = new URL(url);
        HttpURLConnection huc = (HttpURLConnection)urlAddress.openConnection();
        huc.setRequestMethod(method);
        huc.setRequestProperty("Content-type", contentType);
        huc.connect();
        InputStreamReader isr = new InputStreamReader(huc.getInputStream());

        BufferedReader in = new BufferedReader(isr);
        String uline = in.readLine();
        response.content = uline;

        //        huc.disconnect();
        //        System.out.println("####  huc disconnected ###");

        return response;
    }

    static HttpResponse makeHttpRequest(String method, String url, String contentType, InputStream is) throws Exception {
        HttpResponse response = new HttpResponse();
        URL urlAddress = new URL(url);
        HttpURLConnection huc = (HttpURLConnection)urlAddress.openConnection();
        huc.setRequestMethod(method);
        if (null != is) {
            huc.setDoOutput(true);
            huc.setRequestProperty("Content-Type", contentType);
            OutputStream os = huc.getOutputStream();
            byte[] buf = new byte[1024];
            int read;
            while ((read = is.read(buf)) != -1) {
                os.write(buf, 0, read);
            }
        }
        InputStreamReader isr = new InputStreamReader(huc.getInputStream());
        BufferedReader in = new BufferedReader(isr);
        String uline = in.readLine();
        response.content = uline;
        return response;
    }

    static HttpResponse makeHttpGetRequest(String method, String url, String contentType, String content)
        throws Exception {
        return makeHttpRequest(method, url, contentType, new ByteArrayInputStream(content.getBytes("UTF-8")));
    }

    static HttpResponse makeHttpRequest(String method, String url) throws Exception {
        return makeHttpRequest(method, url, null, (InputStream)null);
    }

    public static void main(String[] args) {
        try {

            HttpResponse response;

            System.out.println("Getting the name *BEFORE* setting it:");
            response = makeHttpGetRequest("GET", UrlBase + "/helloworld/getname", "text/plain");
            System.out.println(new Formatter().format("---- Received String:\n%s", response.content.toString()));

            System.out.println("Setting the name:");
            String newText = "Morpheus";
            InputStream inputStream = new ByteArrayInputStream(newText.getBytes());
            response = makeHttpRequest("PUT", UrlBase + "/helloworld/setname", "text/plain", inputStream);

            System.out.println("Getting the name *AFTER* setting it:");
            response = makeHttpGetRequest("GET", UrlBase + "/helloworld/getname", "text/plain");
            System.out.println(new Formatter().format("---- Received String:\n%s", response.content.toString()));

            System.out.println("POST Operation:");
            response = makeHttpGetRequest("POST", UrlBase + "/helloworld/postoperation/prateek", "text/plain");
            //System.out.println(new Formatter().format("---- Received String:\n%s", response.content.toString()));

            System.out.println("Getting the name *AFTER* the POST operation:");
            response = makeHttpGetRequest("GET", UrlBase + "/helloworld/getname", "text/plain");
            System.out.println(new Formatter().format("---- Received String:\n%s", response.content.toString()));
        } catch (Exception e) {
            System.out.println("TEST FAILED! :-(");
            e.printStackTrace(System.out);
        }
    }
}