File size: 7,395 Bytes
07c3cdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Main class to sent differen kind of messages to the http server
import org.apache.http.impl.client.DefaultHttpClient;

// Enter CRUD memebers 
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpDelete;

// Used to set JSON or XML messages request
import org.apache.http.entity.StringEntity;

// Needed for response fetch goal
import org.apache.http.HttpResponse;
import org.apache.http.HttpEntity;
import org.apache.http.util.EntityUtils;

/**

* Single class containing functions to show how to use GET,POST,PUT,DELETE methods.

*/
public class CRUD
{
    private static String m_user = "workflow"; // This member variable must be changed to its own dev workspace

    private static void PostSample()
    {
        System.out.println("POST: Enter login params\n");

        String loginParamsXML = "<?xml version='1.0'?>\n"
                +"<request>\n"
                +"<user>admin</user>\n"
                +"<password>admin</password>\n"
                +"</request>";
        String URI = "http://"+m_user+".pmos.colosa.net/rest/"+m_user+"/login/";

        System.out.println( "Request: "+URI + "\n"+ loginParamsXML + "\n");

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost(URI);
        try
        {
            StringEntity input = new StringEntity( loginParamsXML);
            input.setContentType("application/xml");
            postRequest.setEntity(input);
            HttpResponse httpResponse = httpClient.execute(postRequest);
            
            HttpEntity responseEntity = httpResponse.getEntity();
            if( responseEntity != null)
            {
                String response = new String();
                response = EntityUtils.toString( responseEntity);
                System.out.println( "Response: " + response + "\n");
            }
        }
        catch( java.io.IOException x)
        {
            throw new RuntimeException("I/O error: " + x.toString()); 
        }
    }
    
    private static void GetSample()
    {
        System.out.println("GET: Display TRANSLATION table row\n");

        String URI = "http://"+m_user+".pmos.colosa.net/rest/"+m_user+"/TRANSLATION/LABEL/LOGIN/en/";
        System.out.println( "Request: " + URI + "\n");

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet getRequest = new HttpGet(URI);
        try
        {
            HttpResponse httpResponse = httpClient.execute(getRequest);
            
            HttpEntity responseEntity = httpResponse.getEntity();
            if( responseEntity != null)
            {
                String response = new String();
                response = EntityUtils.toString( responseEntity);
                System.out.println( "Response: " + response + "\n");
            }
        }
        catch( java.io.IOException x)
        {
            throw new RuntimeException("I/O error: " + x.toString()); 
        }
    }

    private static void AnotherPostSample()
    {
        System.out.println("POST: Insert new row in TRANLATION\n");

        String URI = "http://"+m_user+".pmos.colosa.net/rest/"+m_user+"/TRANSLATION/";
        String newRow = "BUTTON/ESCAPE/en/sample/2012-05-05/";
        System.out.println( "Request: " + URI + " new row: " + newRow + "\n");
        URI = URI + newRow;

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost(URI);
        try
        {
            HttpResponse httpResponse = httpClient.execute(postRequest);
            
            HttpEntity responseEntity = httpResponse.getEntity();
            if( responseEntity != null)
            {
                String response = new String();
                if(response.isEmpty())
                {
                    System.out.println( "Response: Status code: " + httpResponse.getStatusLine().getStatusCode()+ "\n");
                    return;
                }
                response = EntityUtils.toString( responseEntity);
                System.out.println( "Response: " + response + "\n");
            }
        }
        catch( java.io.IOException x)
        {
            throw new RuntimeException("I/O error: " + x.toString()); 
        }
    }

    private static void PutSample()
    {
        System.out.println("POST: Update a row in TRANLATION\n");

        String URI = "http://"+m_user+".pmos.colosa.net/rest/"+m_user+"/TRANSLATION/";
        String index = "BUTTON/ESCAPE/en/";
        String updateData = "changesample/2011-07-06/";

        System.out.println( "Request: " + URI + " index: " + index + " updateData: " + updateData + "\n");
        URI = URI + index + updateData;

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPut putRequest = new HttpPut(URI);
        try
        {
            HttpResponse httpResponse = httpClient.execute(putRequest);
            
            HttpEntity responseEntity = httpResponse.getEntity();
            if( responseEntity != null)
            {
                String response = new String();
                if(response.isEmpty())
                {
                    System.out.println( "Response: Status code: " + httpResponse.getStatusLine().getStatusCode()+ "\n");
                    return;
                }
                response = EntityUtils.toString( responseEntity);
                System.out.println( "Response: " + response + "\n");
            }
        }
        catch( java.io.IOException x)
        {
            throw new RuntimeException("I/O error: " + x.toString()); 
        }
    }

    private static void DeleteSample()
    {
        System.out.println("DELETE: Remove a row in TRANLATION\n");

        String URI = "http://"+m_user+".pmos.colosa.net/rest/"+m_user+"/TRANSLATION/";
        String index = "BUTTON/ESCAPE/en/";

        System.out.println( "Request: " + URI + "index:" + index + "\n");
        URI = URI + index;

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpDelete deleteRequest = new HttpDelete(URI);
        try
        {
            HttpResponse httpResponse = httpClient.execute(deleteRequest);
            
            HttpEntity responseEntity = httpResponse.getEntity();
            if( responseEntity != null)
            {
                String response = new String();
                if(response.isEmpty())
                {
                    System.out.println( "Response: Status code: " + httpResponse.getStatusLine().getStatusCode()+ "\n");
                    return;
                }
                response = EntityUtils.toString( responseEntity);
                System.out.println( "Response: " + response + "\n");
            }
        }
        catch( java.io.IOException x)
        {
            throw new RuntimeException("I/O error: " + x.toString()); 
        }
    }

    public static void main(String args[])
    {
        System.out.println("CRUD samples.");
        PostSample();
        GetSample();
        AnotherPostSample();
        PutSample();
        DeleteSample();
	
    }
}