Java tutorial
/* * Copyright 2013 Red Hat, Inc. * * Red Hat licenses this file to you under the Apache License, version 2.0 * (the "License"); you may not use this file except in compliance with the * License. You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. * * */ package com.c9.dashboard; import org.vertx.java.busmods.BusModBase; import org.vertx.java.core.Handler; import org.vertx.java.core.buffer.Buffer; import org.vertx.java.core.eventbus.Message; import org.vertx.java.core.http.HttpClient; import org.vertx.java.core.http.HttpClientRequest; import org.vertx.java.core.http.HttpClientResponse; import org.vertx.java.core.json.JsonObject; import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.StringUtils; public class DashboardModule extends BusModBase { Handler<Message<JsonObject>> apiHandler; private String baseUrl; private String username; private String password; private String apiVersion; private String encodedUserPwd; private HttpClient client; public void start() { super.start(); baseUrl = getOptionalStringConfig("klipfolioBaseUrl", null); username = getOptionalStringConfig("username", null); password = getOptionalStringConfig("password", null); apiVersion = getOptionalStringConfig("apiVersion", "/api/1/"); if (baseUrl == null || username == null || password == null) { throw new RuntimeException("Mandatory Fields were not set!"); } encodedUserPwd = Base64.encodeBase64String(StringUtils.getBytesUtf8(username + ":" + password)); client = vertx.createHttpClient().setHost(baseUrl).setSSL(true).setPort(443).setMaxPoolSize(10); apiHandler = new Handler<Message<JsonObject>>() { @Override public void handle(Message<JsonObject> event) { logger.debug("Recieved Event : " + event.body()); String action = getMandatoryString("action", event); try { if ("get".equalsIgnoreCase(action)) { doGet(event); } else if ("add".equalsIgnoreCase(action)) { // Call the getAPI and reply doPost(event); } else if ("update".equalsIgnoreCase(action)) { doPut(event); } else if ("delete".equalsIgnoreCase(action)) { doDelete(event); } } catch (Exception e) { sendError(event, e.getMessage(), e); } } }; vertx.eventBus().registerHandler("klipfolio.api", apiHandler); } protected void doDelete(Message<JsonObject> event) { String resource = getMandatoryString("resource", event); HttpClientRequest request = client.delete(apiVersion + resource, handle(event)); request.putHeader("Authorization", "Basic " + encodedUserPwd); request.putHeader("Content-Type", "application/json"); request.exceptionHandler(handleException(event)); request.end(); } protected void doPut(Message<JsonObject> event) { String resource = getMandatoryString("resource", event); JsonObject jsonObject = getMandatoryObject("data", event); HttpClientRequest request = client.put(apiVersion + resource, handle(event)); request.write(new Buffer(jsonObject.toString())); request.putHeader("Authorization", "Basic " + encodedUserPwd); request.putHeader("Content-Type", "application/json"); request.exceptionHandler(handleException(event)); request.end(); } protected void doPost(Message<JsonObject> event) { String resource = getMandatoryString("resource", event); JsonObject jsonObject = getMandatoryObject("data", event); HttpClientRequest request = client.post(apiVersion + resource, handle(event)); request.write(new Buffer(jsonObject.toString())); request.putHeader("Authorization", "Basic " + encodedUserPwd); request.putHeader("Content-Type", "application/json"); request.exceptionHandler(handleException(event)); request.end(); } protected void doGet(final Message<JsonObject> event) { String resource = getMandatoryString("resource", event); HttpClientRequest request = client.get(apiVersion + resource, handle(event)); request.putHeader("Authorization", "Basic " + encodedUserPwd); request.exceptionHandler(handleException(event)); request.end(); } private Handler<Throwable> handleException(final Message<JsonObject> event) { return new Handler<Throwable>() { @Override public void handle(Throwable e) { e.printStackTrace(); logger.debug("Error in request to Klipfolio.", e); sendError(event, "Exception in request.", new Exception(e)); } }; } private Handler<HttpClientResponse> handle(final Message<JsonObject> event) { return new Handler<HttpClientResponse>() { @Override public void handle(HttpClientResponse response) { response.exceptionHandler(handleException(event)); if (response.statusCode() != 200) { logger.debug("Request to Klipfolio did not return with http code 200"); sendError(event, "Http Status returned : " + response.statusCode() + " Message : " + response.statusMessage()); return; } // Handle json response response.bodyHandler(new Handler<Buffer>() { @Override public void handle(Buffer buffer) { logger.debug("Recieved Response : " + buffer.toString()); event.reply(new JsonObject(buffer.toString())); } }); } }; } }