Java tutorial
/*************************************************************************** * Copyright 2012 TXT e-solutions SpA * Licensed 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. * * This work was performed within the IoT_at_Work Project * and partially funded by the European Commission's * 7th Framework Programme under the research area ICT-2009.1.3 * Internet of Things and enterprise environments. * * Authors: * Donato Andrisani (TXT e-solutions SpA) * * Contributors: * Domenico Rotondi (TXT e-solutions SpA) **************************************************************************/ package revServTest; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import database.RevocationOutComePost; import pendingRevocationsManagement.RevocationOutCome; public class IoTTestForPOST { /** * @param args * @throws IOException * @throws UnsupportedEncodingException */ public static void main(String[] args) throws UnsupportedEncodingException, IOException { IoTTestForPOST testa = new IoTTestForPOST(); // try { // // DefaultHttpClient httpClient = new DefaultHttpClient(); // // // HttpPost postRequest = new // // // HttpPost("http://pc-drotondi7:8080/REVOCATIONSERVICETEST/revocation"); // // HttpPost postRequest = new HttpPost( // "http://localhost:2222/revocation"); // // // StringEntity input = new StringEntity( // // // readFileAsString("C:/Users/andrisani/Desktop/firstRevocation.xml")); // // StringEntity input = new // // // StringEntity(readFileAsString("C:/Users/andrisani/Desktop/secondRevocation.xml")); // // StringEntity input = new // // // StringEntity(readFileAsString("C:/Users/andrisani/Desktop/secondRevocation.xml")); // StringEntity input = new // StringEntity(readFileAsString("C:/Users/andrisani/Desktop/pendingRevocation.xml")); // // input.setContentType("application/xml"); // postRequest.setEntity(input); // // HttpResponse response = httpClient.execute(postRequest); // // if (response.getStatusLine().getStatusCode() != 409 // && response.getStatusLine().getStatusCode() != 201 // && response.getStatusLine().getStatusCode() != 400 // && response.getStatusLine().getStatusCode() != 200 // && response.getStatusLine().getStatusCode() != 404 // && response.getStatusLine().getStatusCode() != 500) { // throw new RuntimeException("Failed : HTTP error code : " // + response.getStatusLine().getStatusCode()); // } // // BufferedReader br = new BufferedReader(new InputStreamReader( // (response.getEntity().getContent()))); // // String output; // System.out.println("Output from Server .... \n"); // while ((output = br.readLine()) != null) { // System.out.println(output); // } // httpClient.getConnectionManager().shutdown(); // // } catch (MalformedURLException e) { // // e.printStackTrace(); // // } catch (IOException e) { // // e.printStackTrace(); // // } } public String testPost(StringEntity input) { // String result; HttpResponse response = null; try { DefaultHttpClient httpClient = new DefaultHttpClient(); // HttpPost postRequest = new // HttpPost("http://pc-drotondi7:8080/REVOCATIONSERVICETEST/revocation"); HttpPost postRequest = new HttpPost("http://localhost:2222/revocation"); // StringEntity input = new StringEntity( // readFileAsString("C:/Users/andrisani/Desktop/firstRevocation.xml")); // StringEntity input = new // StringEntity(readFileAsString("C:/Users/andrisani/Desktop/secondRevocation.xml")); // StringEntity input = new // StringEntity(readFileAsString("C:/Users/andrisani/Desktop/secondRevocation.xml")); // StringEntity input = new // StringEntity(readFileAsString("C:/Users/andrisani/Desktop/pendingRevocation.xml")); input.setContentType("application/xml"); postRequest.setEntity(input); response = httpClient.execute(postRequest); if (response.getStatusLine().getStatusCode() != 409 && response.getStatusLine().getStatusCode() != 201 && response.getStatusLine().getStatusCode() != 400 && response.getStatusLine().getStatusCode() != 200 && response.getStatusLine().getStatusCode() != 404 && response.getStatusLine().getStatusCode() != 500) { throw new RuntimeException( "Failed : HTTP error code : " + response.getStatusLine().getStatusCode()); } BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent()))); String output; System.out.println("Output from Server .... \n"); while ((output = br.readLine()) != null) { System.out.println(output); } httpClient.getConnectionManager().shutdown(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // if (response.getStatusLine().getStatusCode() == 409 || response.getStatusLine().getStatusCode() == 201 || response.getStatusLine().getStatusCode() == 400 // || response.getStatusLine().getStatusCode() == 200 || response.getStatusLine().getStatusCode() == 404 || response.getStatusLine().getStatusCode() == 500) { // // result = RevocationOutCome.code; // // } else { // String res = Integer.toString(response.getStatusLine().getStatusCode()); // result = res; // } // return result; return RevocationOutComePost.code; } public static String readFileAsString(String filePath) throws java.io.IOException { StringBuffer fileData = new StringBuffer(1000); BufferedReader reader = new BufferedReader(new FileReader(filePath)); char[] buf = new char[1024]; int numRead = 0; while ((numRead = reader.read(buf)) != -1) { String readData = String.valueOf(buf, 0, numRead); fileData.append(readData); buf = new char[1024]; } reader.close(); return fileData.toString(); } }