db.dao.Dao.java Source code

Java tutorial

Introduction

Here is the source code for db.dao.Dao.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 db.dao;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.primefaces.json.JSONArray;
import org.primefaces.json.JSONException;

/**
 *
 * @author Pepijn
 */
public class Dao {

    private boolean local = true;
    private String urlLocal = "http://localhost:8081/RestTomcat/rest/";
    private String urlRemote = "http://192.168.128.13:8081/RestTomcat/rest/";

    public String checkIfLocal(String url) {
        if (local) {
            url = urlLocal + url;
        } else {
            url = urlRemote + url;
        }
        return url;
    }

    public HttpResponse add(String url, Object entity) {
        url = checkIfLocal(url);

        String usernamepassword = "Pepijn:Mores";
        String encoded = Base64.encodeBase64String(usernamepassword.getBytes());

        HttpClient client = HttpClientBuilder.create().build();
        HttpPost postRequest = new HttpPost(url);
        postRequest.setHeader("content-type", "application/json");
        postRequest.setHeader("Authorization", encoded);

        String entityToJson = null;
        try {
            ObjectMapper mapper = new ObjectMapper();
            entityToJson = mapper.writeValueAsString(entity);
        } catch (JsonProcessingException ex) {
            Logger.getLogger(ArticleDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        StringEntity jsonString = null;
        try {
            jsonString = new StringEntity(entityToJson);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
        }

        postRequest.setEntity(jsonString);
        HttpResponse response = null;
        try {
            response = client.execute(postRequest);
        } catch (IOException ex) {
            Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return response;
    }

    public JSONArray listAllHttpClient(String url) {
        url = checkIfLocal(url);
        HttpClient client = HttpClientBuilder.create().build();
        HttpGet getRequest = new HttpGet(url);
        String usernamepassword = "Pepijn:Mores";
        String encoded = Base64.encodeBase64String(usernamepassword.getBytes());
        getRequest.addHeader("accept", "application/json");
        getRequest.setHeader("Authorization", encoded);
        ;
        HttpResponse response = null;

        try {
            response = client.execute(getRequest);
        } catch (IOException ex) {
            Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
        }

        if (response.getStatusLine().getStatusCode() != 200) {
            throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
        }
        JSONArray json = null;
        try (BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()))) {
            String inputLine;
            StringBuilder responseString = new StringBuilder();
            while ((inputLine = in.readLine()) != null) {
                responseString.append(inputLine);
            }
            json = new JSONArray(responseString.toString());
        } catch (IOException ex) {
            Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (JSONException ex) {
            Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return json;
    }

    public HttpResponse update(String url, Object entity) {
        url = checkIfLocal(url);
        HttpClient client = HttpClientBuilder.create().build();
        HttpPut putRequest = new HttpPut(url);
        String usernamepassword = "Pepijn:Mores";
        String encoded = Base64.encodeBase64String(usernamepassword.getBytes());
        putRequest.setHeader("content-type", "application/json");
        putRequest.setHeader("Authorization", encoded);
        ;

        String entityToJson = null;
        try {
            ObjectMapper mapper = new ObjectMapper();
            entityToJson = mapper.writeValueAsString(entity);
        } catch (JsonProcessingException ex) {
            Logger.getLogger(ArticleDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        StringEntity jsonString = null;
        try {
            jsonString = new StringEntity(entityToJson);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
        }

        putRequest.setEntity(jsonString);
        HttpResponse response = null;
        try {
            response = client.execute(putRequest);
        } catch (IOException ex) {
            Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return response;
    }
}