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.HttpPut; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import pendingRevocationsManagement.RevocationOutCome; public class IoTTestForPUT { /** * @param args * @throws IOException * @throws UnsupportedEncodingException */ public static void main(String[] args) throws UnsupportedEncodingException, IOException { IoTTestForPUT test = new IoTTestForPUT(); } public String testPut(String url0, StringEntity input) { String result; HttpResponse response = null; try { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPut putRequest = new HttpPut(url0); input.setContentType("application/xml"); putRequest.setEntity(input); response = httpClient.execute(putRequest); 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 RevocationOutCome.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(); } }