Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.gommehd.gonet.sailor.handler;
- import com.eclipsesource.json.Json;
- import com.eclipsesource.json.JsonObject;
- import net.gommehd.gonet.sailor.FailureDescription;
- import net.gommehd.gonet.sailor.FailureHandler;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.util.Base64;
- import java.util.HashMap;
- import java.util.Map;
- /**
- * @author Fabian
- * @version 1.0
- */
- public class WebAPIHandler implements FailureHandler {
- private static final String API_HOST = "api.xsrv.eu";
- private static final String API_USER = "GoNetSailor";
- private static final String API_PASS = "uIF52Wu2nTVLqOIbUBKg";
- private String sessionID;
- public WebAPIHandler() {
- this.login();
- }
- private void login() {
- if ( sessionID == null || sessionID.isEmpty() ) {
- // Generate Basic auth
- byte[] encodedBytes = Base64.getEncoder().encode( ( API_USER + ":" + API_PASS ).getBytes() );
- String authorization = "Basic " + new String( encodedBytes );
- // Request and parse
- JsonObject result = request( "/v1/authorization/login", null, new HashMap<String, String>(){{
- put( "Authorization", authorization );
- }} );
- // Read the session ID on success
- if ( result.getBoolean( "success", false ) ) {
- this.sessionID = result.get( "result" ).asObject().getString( "sessionID", "" );
- } else {
- System.out.println( "Sailor API did return error: " + result.get( "errors" ).asArray().get( 0 ).asObject().getString( "errorMessage", "none" ) );
- }
- } else {
- this.logout();
- }
- }
- private void logout() {
- request( "/v1/authorization/logout", null, null );
- sessionID = null;
- }
- private JsonObject request( String fileOnHost, String body, Map<String, String> header ) {
- String response = "{}";
- try {
- URL url = new URL( "http", API_HOST, 80, fileOnHost );
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- connection.setRequestMethod( "POST" );
- connection.setDoInput( true );
- connection.setDoOutput( true );
- connection.setUseCaches( false );
- connection.setRequestProperty( "Content-Type", "application/json" );
- // Overtake header
- if ( header != null ) {
- for ( Map.Entry<String, String> stringStringEntry : header.entrySet() ) {
- connection.setRequestProperty( stringStringEntry.getKey(), stringStringEntry.getValue() );
- }
- }
- // Append SessionID to Header
- if ( this.sessionID != null && !this.sessionID.isEmpty() ) {
- connection.setRequestProperty( "X-ApiSessionID", this.sessionID );
- }
- OutputStreamWriter writer = null;
- if ( body != null && !body.isEmpty() ) {
- connection.setRequestProperty( "Content-Length", String.valueOf( body.length() ) );
- writer = new OutputStreamWriter( connection.getOutputStream() );
- writer.write( body );
- writer.flush();
- } else {
- connection.setRequestProperty( "Content-Length", "0" );
- }
- BufferedReader reader = new BufferedReader( new InputStreamReader( connection.getInputStream() ) );
- response = "";
- for ( String line; ( line = reader.readLine() ) != null; ) {
- response += line + "\n";
- }
- if ( writer != null ) {
- writer.close();
- }
- reader.close();
- } catch ( IOException e ) {
- e.printStackTrace();
- }
- return Json.parse( response ).asObject();
- }
- @Override
- public void handleFailure( FailureDescription description ) {
- String jsonBody = writeJSON( description );
- JsonObject result = request( "/v1/sailor", jsonBody, null );
- if ( !result.getBoolean( "success", false ) ) {
- int errorCode = result.get( "errors" ).asArray().get( 0 ).asObject().getInt( "errorCode", 0 );
- if ( errorCode == 101 ) {
- this.login();
- handleFailure( description );
- return;
- }
- System.out.println( "Sailor API did return error: " + result.get( "errors" ).asArray().get( 0 ).asObject().getString( "errorMessage", "none" ) );
- }
- }
- private String writeJSON( FailureDescription failureDescription ) {
- JsonObject jsonObject = Json.object();
- jsonObject.set( "errorIdentifier", failureDescription.getErrorIdentifier() );
- jsonObject.set( "priority", failureDescription.getPriority().name() );
- JsonObject metaData = Json.object();
- for ( String metaKey : failureDescription.getMetaKeys() ) {
- metaData.set( metaKey, failureDescription.getMetaEntry( metaKey ) );
- }
- jsonObject.set( "metadata", metaData );
- jsonObject.set( "timestamp", System.currentTimeMillis() );
- return jsonObject.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement