Skip to content

Instantly share code, notes, and snippets.

@mkdika
Created August 28, 2017 04:56
Show Gist options
  • Save mkdika/31973e671cf8473020a1a985d0becaaa to your computer and use it in GitHub Desktop.
Save mkdika/31973e671cf8473020a1a985d0becaaa to your computer and use it in GitHub Desktop.
Java HTTP client consume REST API demo
package updater;
import co.id.waruna.wpayroll.updater.entity.UpdateItemFile;
import co.id.waruna.wpayroll.updater.entity.UpdateItem;
import co.id.waruna.wpayroll.updater.helper.Utilz;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Maikel Chandika <mkdika@gmail.com>
*/
public class HttpClientTester {
public static void main(String[] args) throws IOException, FileNotFoundException, NoSuchAlgorithmException {
// File file = new File("/home/maikel/javalib/jeneric-1.0.jar");
//
// System.out.println(Utilz.calculateSHA1(file));
try {
ObjectMapper mapper = new ObjectMapper();
String SQL_URL = "http://wpayroll-updater.waruna-group.co.id/api/sql/latest/?sqlVersion=1.0.0";
String BINARY_URL = "http://wpayroll-updater.waruna-group.co.id/api/binary/latest/";
URL url = new URL(SQL_URL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("X-SECRET", Utilz.SECRET_TOKEN);
System.out.println("Response Code: " + connection.getResponseCode());
System.out.println("Response Message: " + connection.getResponseMessage());
System.out.println("Response Content:");
BufferedReader br = new BufferedReader(new InputStreamReader(
(connection.getInputStream())));
StringBuilder sb = new StringBuilder();
String output;
while ((output = br.readLine()) != null) {
sb.append(output);
}
System.out.println(sb.toString());
// for binary
// UpdateItem bfile = mapper.readValue(sb.toString(), UpdateItem.class);
// System.out.println("version: " + bfile.getVersion());
// System.out.println("filelist: "+bfile.getFileList().size());
// for (UpdateItemFile f : bfile.getFileList()) {
// System.out.println("\tFilename: "+f.getFilename());
// System.out.println("\tSize: "+f.getSize());
// System.out.println("\tSH1: "+f.getSha1());
// System.out.println("\tURL: "+f.getUrl());
// }
// System.out.println("date: "+bfile.getDate());
// System.out.println("description: "+bfile.getDescription());
// System.out.println("url: "+bfile.getUrl());
// for sql
List<UpdateItem> sqlfiles = Arrays.asList(mapper.readValue(sb.toString(), UpdateItem[].class));
for (UpdateItem b : sqlfiles) {
System.out.println("Version: " + b.getVersion());
System.out.println("Date: " + b.getDate());
System.out.println("Description: " + b.getDescription());
System.out.println("URL: " + b.getUrl());
System.out.println("File list size: " + b.getFileList().size());
for (UpdateItemFile f : b.getFileList()) {
System.out.println("\tFilename: " + f.getFilename());
System.out.println("\tSize: " + f.getSize());
System.out.println("\tSH1: " + f.getSha1());
System.out.println("\tURL: " + f.getUrl());
System.out.println("\tDestination: "+f.getDestination());
}
}
connection.disconnect();
} catch (MalformedURLException | ProtocolException ex) {
Logger.getLogger(Tester.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Tester.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment