Skip to content

Commit

Permalink
migrated API/services clients common directory
Browse files Browse the repository at this point in the history
Signed-off-by: Andres LeonRangel <[email protected]>
  • Loading branch information
aleon1220 committed Sep 26, 2023
1 parent dd37188 commit 2524aea
Show file tree
Hide file tree
Showing 21 changed files with 1,177 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/main/java/clients/rest/JerseyClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package testing.rest;

import java.io.IOException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import org.apache.http.client.ClientProtocolException;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

public class JerseyClient {

/**
* @param args
* @throws ClientProtocolException
* @throws IOException
*/

public void obtainJSONResponse() throws ClientProtocolException, IOException {

// Invoke REST service with GET Method
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);

WebResource service = client
.resource(UriBuilder.fromUri("https://ctpoixww04.execute-api.us-east-1.amazonaws.com").build());

// getting JSON data
System.out.println("With JSON" + service.path("dev").path("session").accept(MediaType.APPLICATION_JSON).header(
"Authorization",
"eyJraWQiOiJKQWJRaHNlTTVldFRBMk1vMlpoNGs5Mlp2ZmFHaU5zWWtVQlFNbG1cL1Mybz0iLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiI2MWI5ZTI4NC1kMzVhLTQ0M2YtOTlhNy04MGY0OWE4YTI4NmQiLCJhdWQiOiIxZTh0ZWZtbGNsMmgyOTB2bmg5cmNlZm52ciIsImNvZ25pdG86Z3JvdXBzIjpbImFkbWluaXN0cmF0b3IiXSwiZW1haWxfdmVyaWZpZWQiOmZhbHNlLCJ0b2tlbl91c2UiOiJpZCIsImF1dGhfdGltZSI6MTUwNTAyMTU0OSwiaXNzIjoiaHR0cHM6XC9cL2NvZ25pdG8taWRwLnVzLWVhc3QtMS5hbWF6b25hd3MuY29tXC91cy1lYXN0LTFfMEhkMUtvTTVhIiwiY29nbml0bzp1c2VybmFtZSI6IjEwMjk1NzY1IiwiZXhwIjoxNTA1MDI1MTQ5LCJpYXQiOjE1MDUwMjE1NDksImVtYWlsIjoiam9uYXRoYW4udEB4dHJhLmNvLm56In0.pL9EmmsbB-pxTCXRmDmAAnfMBhLgEY39tbNf0LMhHWF_sSFTB6gYELxnfNa3ITMEEV_wwgvJ-9USb3nHh1G8JkNDxDVCS0S0q4U81GZDqA8USGzA4mklTg1qROlHQkKFipaKQia8U9oLZSIGp6HrpvtQN7wOm9S_0Z5ePXN2REgYsWKobfkLI3dHICACt0RANRBLlXNlQC1F8Hj7dl7UbeQkCZqgj_J_GcxHHLHBRRERO4Koz09gYBhpVrWZZlbUJpwAk3OVCesj7Wz73xjpE2ckm9x6SLkzxC8W5FLvyH5rbiD7Dif9xIVB6jV1pz9GIwMGqzcSWT0LIujw90hoNg")
.get(String.class));
}

public void obtainXMLResponse() throws ClientProtocolException, IOException {
// getting XML data
// System.out.println("with
// XML"+service.path("dev").path("session").accept(MediaType.APPLICATION_XML).get(String.class));
}
}
32 changes: 32 additions & 0 deletions src/main/java/clients/rest/JerseyClientPost.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package jersey;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;

public class JerseyClientPost {

public static void main(String[] args) {

try {
Client client = Client.create();
WebResource webResource = client.resource("https://ctpoixww04.execute-api.us-east-1.amazonaws.com/dev/login");
String input = "{\"id\":\"10295765\",\"password\":\"Value!12\"}";
ClientResponse response = webResource.type("application/json").post(ClientResponse.class, input);

if (response.getStatus() != 201) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatus());
}

System.out.println("Output from Server .... \n");
String output = response.getEntity(String.class);
System.out.println(output);

} catch (Exception e) {

e.printStackTrace();

}
}
}
37 changes: 37 additions & 0 deletions src/main/java/clients/rest/JerseyGet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package jersey;

import java.io.IOException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import org.apache.http.client.ClientProtocolException;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

public class JerseyGet {

/**
* @param args
* @throws ClientProtocolException
* @throws IOException
*/
public static void main(String[] args) throws ClientProtocolException, IOException {

ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);

WebResource service = client
.resource(UriBuilder.fromUri("https://ctpoixww04.execute-api.us-east-1.amazonaws.com").build());

// getting JSON data
System.out.println("With JSON" + service.path("dev").path("session").accept(MediaType.APPLICATION_JSON).header(
"Authorization",
"eyJraWQiOiJKQWJRaHNlTTVldFRBMk1vMlpoNGs5Mlp2ZmFHaU5zWWtVQlFNbG1cL1Mybz0iLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiI2MWI5ZTI4NC1kMzVhLTQ0M2YtOTlhNy04MGY0OWE4YTI4NmQiLCJhdWQiOiIxZTh0ZWZtbGNsMmgyOTB2bmg5cmNlZm52ciIsImNvZ25pdG86Z3JvdXBzIjpbImFkbWluaXN0cmF0b3IiXSwiZW1haWxfdmVyaWZpZWQiOmZhbHNlLCJ0b2tlbl91c2UiOiJpZCIsImF1dGhfdGltZSI6MTUwNTAyMTU0OSwiaXNzIjoiaHR0cHM6XC9cL2NvZ25pdG8taWRwLnVzLWVhc3QtMS5hbWF6b25hd3MuY29tXC91cy1lYXN0LTFfMEhkMUtvTTVhIiwiY29nbml0bzp1c2VybmFtZSI6IjEwMjk1NzY1IiwiZXhwIjoxNTA1MDI1MTQ5LCJpYXQiOjE1MDUwMjE1NDksImVtYWlsIjoiam9uYXRoYW4udEB4dHJhLmNvLm56In0.pL9EmmsbB-pxTCXRmDmAAnfMBhLgEY39tbNf0LMhHWF_sSFTB6gYELxnfNa3ITMEEV_wwgvJ-9USb3nHh1G8JkNDxDVCS0S0q4U81GZDqA8USGzA4mklTg1qROlHQkKFipaKQia8U9oLZSIGp6HrpvtQN7wOm9S_0Z5ePXN2REgYsWKobfkLI3dHICACt0RANRBLlXNlQC1F8Hj7dl7UbeQkCZqgj_J_GcxHHLHBRRERO4Koz09gYBhpVrWZZlbUJpwAk3OVCesj7Wz73xjpE2ckm9x6SLkzxC8W5FLvyH5rbiD7Dif9xIVB6jV1pz9GIwMGqzcSWT0LIujw90hoNg")
.get(String.class));

// getting XML data
// System.out.println("with
// XML"+service.path("dev").path("session").accept(MediaType.APPLICATION_XML).get(String.class));
}
}
36 changes: 36 additions & 0 deletions src/main/java/clients/rest/JerseyJsonResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package jersey;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;

public class JerseyJsonResponse {

public static void main(String[] args) {
try {
String baseuri = "https://ctpoixww04.execute-api.us-east-1.amazonaws.com/dev/login/";
Client client = Client.create();

WebResource webResource = client.resource(baseuri);

String input = "{\"id\":\"10295765\",\"password\":\"Value!12\"}";

// POST method
ClientResponse response = webResource.accept("application/json").type("type:application/json")
.post(ClientResponse.class, input);

// check response status code
if (response.getStatus() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
}

// display response
String output = response.getEntity(String.class);
System.out.println("Output from Server .... ");
System.out.println(output + "\n");
} catch (Exception e) {
e.printStackTrace();
}

} // main()
}
28 changes: 28 additions & 0 deletions src/main/java/clients/rest/JerseyJsonSimpler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package testing.rest;

import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.representation.Form;

public class JerseyJsonSimpler {

public static void main(String[] args) {

try {
Client client = (Client) ClientBuilder.newClient();
WebTarget target = ((javax.ws.rs.client.Client) client).target("http://localhost:9998").path("resource");

Form form = new Form();
form.add("id", "10295765");
form.add("password", "Value!12");
// ClientResponse response =
// WebResource.class.type(MediaType.TEXT_PLAIN).post(ClientResponse.class,
// form);
} catch (Exception e) {
e.printStackTrace();
}

}
}
36 changes: 36 additions & 0 deletions src/main/java/clients/rest/JerseyPost.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package testing.rest;

import java.io.IOException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriBuilder;
import org.apache.http.client.ClientProtocolException;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.core.util.MultivaluedMapImpl;

public class JerseyPost {

public static void main(String[] args) throws ClientProtocolException, IOException {

ClientConfig config = new DefaultClientConfig();

Client client = Client.create(config);

WebResource webResource = client
.resource(UriBuilder.fromUri("https://ctpoixww04.execute-api.us-east-1.amazonaws.com/").build());

MultivaluedMap<String, String> formData = new MultivaluedMapImpl();

formData.add("id", "10295765");
formData.add("password", "Value!12");

ClientResponse response = webResource.path("dev").path("login").type(MediaType.APPLICATION_FORM_URLENCODED)
.post(ClientResponse.class, formData);

System.out.println("Response " + response.getEntity(String.class));
}
}
24 changes: 24 additions & 0 deletions src/main/java/clients/rest/JerseyPostHeader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package testing.rest;

import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.representation.Form;

public class JerseyPostHeader {

Client client = (Client) ClientBuilder.newClient();
WebTarget target = ((javax.ws.rs.client.Client) client).target("http://localhost:9998").path("resource");

Form form = new Form();
// form.param("x", "foo");
// form.param("y", "bar");

// String bean =
// target.request(MediaType.APPLICATION_JSON_TYPE).post(MediaType.APPLICATION_FORM_URLENCODED_TYPE,
// String.class);
}
36 changes: 36 additions & 0 deletions src/main/java/clients/rest/JerseyTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package testing.rest;

import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;

import bean.AttendanceRegister;

public class JerseyTest {

public static void main(String[] args) {
testAttendanceRegister();
// responseREST();
}

String responseREST() {
// Client client =(Client) ClientBuilder.newClient();
Client client = Client.create();
WebResource target = client.resource("https://ctpoixww04.execute-api.us-east-1.amazonaws.com/dev/login");
String input = "{\"id\":\"10295765\",\"password\":\"Value!12\"}";

Response response = ((WebTarget) target).request("application/json").post(Entity.json(input));
// System.out.println(response.toString());

return response.toString();
}

static void testAttendanceRegister() {
AttendanceRegister att = new AttendanceRegister();
att.confirmAttendance();
}

}
56 changes: 56 additions & 0 deletions src/main/java/clients/rest/NetClientPost.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package testing.rest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class NetClientPost {

// http://localhost:8080/RESTfulExample/json/product/post
public static void main(String[] args) {

try {
URL url = new URL("https://fahze41owc.execute-api.us-east-1.amazonaws.com/dev/user");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");

String input = "{\r\n" +
" \"id\": \"102030\",\r\n" +
" \"firstName\": \"Julito\",\r\n" +
" \"lastName\": \"Schwartzenegger\",\r\n" +
" \"type\": \"administrator\"\r\n" +
"}";

OutputStream os = conn.getOutputStream();
os.write(input.getBytes());
os.flush();

if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
}

BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}

conn.disconnect();

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {
e.printStackTrace();
}
}
}
Loading

0 comments on commit 2524aea

Please sign in to comment.