BusinessLogic.Controller.RestController.java Source code

Java tutorial

Introduction

Here is the source code for BusinessLogic.Controller.RestController.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package BusinessLogic.Controller;

import DataAccess.Entity.Plan;
import DataAccess.Entity.Hotel;
import DataAccess.Entity.Viaje;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.*;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.List;
import java.net.MalformedURLException;
import org.apache.http.client.methods.HttpDelete;

/**
 *
 * @author Danilo
 */
public class RestController {

    public List<Plan> getPlans() throws Exception {
        StringBuilder responseS = new StringBuilder();
        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpGet getRequest = new HttpGet("http://localhost:8080/JoyCenter/resources/plans");
            getRequest.addHeader("accept", "application/json");
            HttpResponse response = httpClient.execute(getRequest);
            if (response.getStatusLine().getStatusCode() != 200) {
                throw new RuntimeException(
                        "Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
            }
            BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
            String output;
            System.out.println("Output from Server .... \n");
            while ((output = br.readLine()) != null) {
                responseS.append(output);
            }
            httpClient.getConnectionManager().shutdown();
        } catch (IOException e) {
        }

        Gson gson = new Gson();
        List<Plan> planes = gson.fromJson(responseS.toString(), new TypeToken<List<Plan>>() {
        }.getType());

        return planes;

    }

    public Plan getPlan(int planId) throws Exception {
        StringBuilder responseS = new StringBuilder();
        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpGet getRequest = new HttpGet("http://localhost/JoyCenter/resources/plans/" + planId);
            getRequest.addHeader("accept", "application/json");
            HttpResponse response = httpClient.execute(getRequest);

            if (response.getStatusLine().getStatusCode() != 200) {
                throw new RuntimeException(
                        "Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
            }
            BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
            String output;
            System.out.println("Output from Server .... \n");
            while ((output = br.readLine()) != null) {
                responseS.append(output);
            }
            httpClient.getConnectionManager().shutdown();
        } catch (IOException e) {
        }

        //Receive response, parse to POJO array.
        Gson gson = new Gson();
        return gson.fromJson(responseS.toString(), Plan.class);
    }

    public Plan updatePlan(Plan plan) throws Exception {
        StringBuilder responseS = new StringBuilder();

        try {

            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost postRequest = new HttpPost("http://locahost/JoyCenter/resources/plans/" + plan.getId());

            Gson gson = new Gson();
            StringEntity input = new StringEntity(gson.toJson(plan));
            input.setContentType("application/json");
            postRequest.setEntity(input);

            HttpResponse response = httpClient.execute(postRequest);

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

            BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));

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

            httpClient.getConnectionManager().shutdown();

        } catch (MalformedURLException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

        Gson gson = new Gson();
        return gson.fromJson(responseS.toString(), Plan.class);

    }

    public String deletePlan(Integer planId) throws Exception {
        StringBuilder responseS = new StringBuilder();
        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpDelete delete = new HttpDelete("http://localhost/JoyCenter/resources/plans" + planId);
            HttpResponse response = httpClient.execute(delete);
            if (response.getStatusLine().getStatusCode() != 201) {
                throw new RuntimeException(
                        "Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
            }
            BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));

            String output;
            System.out.println("Output from Server .... \n");

            httpClient.getConnectionManager().shutdown();

        } catch (IOException e) {

            e.printStackTrace();

        }

        return "null";

    }

    public List<Hotel> getHotels() throws Exception {
        StringBuilder responseS = new StringBuilder();

        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();

            HttpGet getRequest = new HttpGet("http://192.168.162.121/Hoex/hoexAPI/hoteles");
            getRequest.addHeader("accept", "application/json");

            HttpResponse response = httpClient.execute(getRequest);

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

            BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));

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

            httpClient.getConnectionManager().shutdown();

        } catch (IOException e) {
        }

        Gson gson = new Gson();
        List<Hotel> hoteles = gson.fromJson(responseS.toString(), new TypeToken<List<Hotel>>() {
        }.getType());

        return hoteles;

    }

    public List<Viaje> getViajes() throws Exception {
        StringBuilder responseS = new StringBuilder();
        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpGet getRequest = new HttpGet("http://192.168.162.167/Terminal/resources/viajes/");
            getRequest.addHeader("accept", "application/json");
            HttpResponse response = httpClient.execute(getRequest);
            if (response.getStatusLine().getStatusCode() != 200) {
                throw new RuntimeException(
                        "Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
            String output;
            System.out.println("Output from Server .... \n");
            while ((output = br.readLine()) != null) {
                responseS.append(output);
            }
            httpClient.getConnectionManager().shutdown();
        } catch (IOException e) {
        }
        Gson gson = new Gson();
        List<Viaje> viajes = gson.fromJson(responseS.toString(), new TypeToken<List<Viaje>>() {
        }.getType());
        return viajes;
    }
}