Java tutorial
/* * Copyright (c) 2012. Piraso Alvin R. de Leon. All Rights Reserved. * * See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The Piraso 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 org.piraso.client.net; import org.piraso.api.JacksonUtils; import org.apache.commons.lang.Validate; import org.apache.http.*; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HttpContext; import org.apache.http.util.EntityUtils; import org.codehaus.jackson.map.ObjectMapper; import org.xml.sax.SAXException; import javax.xml.parsers.ParserConfigurationException; import java.io.IOException; import java.util.ArrayList; import java.util.List; import static org.piraso.api.PirasoConstants.*; /** * Create test handler */ public class HttpPirasoTestHandler extends AbstractHttpHandler { private ObjectMapper mapper; private HttpEntity responseEntity; private Status status; public HttpPirasoTestHandler(HttpClient client, HttpContext context) { super(client, context); this.mapper = JacksonUtils.MAPPER; } @Override public void execute() throws IOException, SAXException, ParserConfigurationException { try { doExecute(); } finally { EntityUtils.consume(responseEntity); } } private void doExecute() throws IOException, SAXException, ParserConfigurationException { Validate.notNull(uri, "uri should not be null."); HttpPost post = new HttpPost(uri.getPath()); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair(SERVICE_PARAMETER, SERVICE_TEST_PARAMETER_VALUE)); post.setEntity(new UrlEncodedFormEntity(params, ENCODING_UTF_8)); HttpResponse response = client.execute(targetHost, post, context); StatusLine status = response.getStatusLine(); if (status.getStatusCode() != HttpStatus.SC_OK) { throw new HttpPirasoException(status.toString()); } responseEntity = response.getEntity(); String contentType = responseEntity.getContentType().getValue().toLowerCase(); if (!contentType.contains(JSON_CONTENT_TYPE)) { throw new HttpPirasoException("Invalid response content type: " + responseEntity.getContentType()); } this.status = mapper.readValue(responseEntity.getContent(), Status.class); } public boolean isSuccess() { return status != null && STATUS_OK.equals(status.getStatus()); } public boolean isBridgetSupported() { return isSuccess() && status.isBridgeSupported(); } public static class Status { private String status; private String version; private boolean bridgeSupported; public boolean isBridgeSupported() { return bridgeSupported; } public void setBridgeSupported(boolean bridgeSupported) { this.bridgeSupported = bridgeSupported; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public String getVersion() { return version; } public void setVersion(String version) { this.version = version; } } }