IoTatWork.NotifyResolutionManager.java Source code

Java tutorial

Introduction

Here is the source code for IoTatWork.NotifyResolutionManager.java

Source

/*******************************************************************************
 * Copyright (c) 2011 - 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:
 *     Gaetano Scigliuto (TXT e-solutions SpA)
 *
 * Contributors:
 *      Domenico Rotondi (TXT e-solutions SpA)
 *******************************************************************************/
package IoTatWork;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Queue;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class NotifyResolutionManager implements Runnable {

    public static Queue<PendingResolutionNotification> concurrentQueue;

    private static final String JSON_PROCESSING_STATUS_CODE = "processingStatusCode";

    public NotifyResolutionManager(Queue<PendingResolutionNotification> queue) {
        concurrentQueue = queue;
    }

    @Override
    public void run() {
        //boolean stopCondition = (concurrentQueue.size() == 0);

        while (true) {
            if (concurrentQueue.size() > 0) {
                PendingResolutionNotification notification = concurrentQueue.remove();
                sendNotification(notification);
                /*
                synchronized (concurrentQueue) {
                   try {
                  concurrentQueue.wait();
                } catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
                }
                  }
                  */
            }

        }
        /*
        while (!stopCondition) {
               
            for (int i = 0; i < concurrentQueue.size(); i++) {
              
          System.out.println("Client dequeue item "
                 + concurrentQueue.dequeueItem());
          try {
              Thread.sleep(1500);
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
            }
            stopCondition = (concurrentQueue.size() == 0);
        }
            
        System.out.println("Client thread exiting...");
        */
    }

    /**
     * 
     * @param notification
     * @return
     */
    private boolean sendNotification(PendingResolutionNotification notification) {
        String xmlString = notification.toXML();

        String revocationPut = notification.getNotificationUrl() + notification.getRevocationHash();
        System.out.println("\nProcessing notification: " + revocationPut + "\n" + notification.toXML());
        // TODO logEvent
        //IoTatWorkApplication.logger.info("Sending pending resolution notification: "+revocationPut);
        IoTatWorkApplication.logger.info(LogMessage.SEND_RESOLUTION_NOTIFICATION + revocationPut);

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPut putRequest = new HttpPut(revocationPut);

        try {
            StringEntity input = new StringEntity(xmlString);

            input.setContentType("application/xml");
            putRequest.setEntity(input);

            HttpResponse response = httpClient.execute(putRequest);

            int statusCode = response.getStatusLine().getStatusCode();

            switch (statusCode) {
            case 200:
                IoTatWorkApplication.logger.info(LogMessage.RESOLUTION_NOTIFICATION_RESPONSE_STATUSCODE_200);
                break;
            case 400:
                BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
                String output = br.toString();
                /*
                while ((output = br.readLine()) != null) {
                   System.out.println(output);
                }
                */
                JsonParser parser = new JsonParser();
                JsonObject jsonObj = (JsonObject) parser.parse(output);
                String code = jsonObj.get(JSON_PROCESSING_STATUS_CODE).getAsString();

                if (code.equals("NPR400")) {
                    IoTatWorkApplication.logger.info(LogMessage.RESOLUTION_NOTIFICATION_RESPONSE_STATUSCODE_400);
                } else if (code.equals("NPR450")) {
                    IoTatWorkApplication.logger.info(LogMessage.RESOLUTION_NOTIFICATION_RESPONSE_STATUSCODE_450);
                } else if (code.equals("NPR451")) {
                    IoTatWorkApplication.logger.info(LogMessage.RESOLUTION_NOTIFICATION_RESPONSE_STATUSCODE_451);
                }

                break;
            case 404:
                IoTatWorkApplication.logger.info(LogMessage.RESOLUTION_NOTIFICATION_RESPONSE_STATUSCODE_404);
                break;
            case 500:
                IoTatWorkApplication.logger.info(LogMessage.RESOLUTION_NOTIFICATION_RESPONSE_STATUSCODE_500);
                break;
            default:
                break;
            }

            httpClient.getConnectionManager().shutdown();

        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return true;

    }

}