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 iotAtWork; import java.io.IOException; import java.io.InputStreamReader; import java.security.NoSuchAlgorithmException; import org.apache.log4j.Logger; import org.json.JSONException; import org.json.JSONObject; import org.restlet.data.MediaType; import org.restlet.data.Status; import org.restlet.ext.json.JsonRepresentation; import org.restlet.representation.Representation; import org.restlet.representation.StringRepresentation; import org.restlet.representation.Variant; import org.restlet.resource.ResourceException; import org.restlet.resource.ServerResource; import org.xml.sax.InputSource; import pendingRevocationsManagement.PendingRevocationsManagement; import pendingRevocationsManagement.RevocationOutCome; import pendingRevocationsManagement.StatusCode; import database.MessageLog; import database.UpdateDB; public class PendingResource extends ServerResource { private String revocationHash; Logger loggerPendingResource = RevServApplication.loggerPendingResource; public PendingResource() throws IOException, NoSuchAlgorithmException { } @Override protected void doInit() throws ResourceException { this.revocationHash = (String) getRequestAttributes().get("revocationHash"); getVariants().add(new Variant(MediaType.APPLICATION_XML)); getVariants().add(new Variant(MediaType.APPLICATION_XHTML)); getVariants().add(new Variant(MediaType.APPLICATION_JSON)); } public RevServApplication getApplication() { return (RevServApplication) super.getApplication(); } protected Representation put(Representation entity, Variant variant) throws ResourceException { Representation res = null; InputStreamReader stream = null; try { stream = new InputStreamReader(entity.getStream()); } catch (IOException e) { loggerPendingResource.error(e); } InputSource is = new InputSource(); is.setCharacterStream(stream); loggerPendingResource.info(MessageLog.REQUESTPUT_RECEIVED); UpdateDB updateDB = null; try { updateDB = new UpdateDB(is, revocationHash); Object result = updateDB.UpdateFields(); if (result == "NotFound") { setStatus(Status.CLIENT_ERROR_NOT_FOUND); } else { if (result == "OK") { setStatus(Status.SUCCESS_OK); RevocationOutCome.code = StatusCode.NPR200; String notification = "NotificationPendingResult"; PendingRevocationsManagement.concurrentQueue.offer(notification); loggerPendingResource.debug(MessageLog.ADD_CONCURRENTQUEUE); } else { setStatus(Status.CLIENT_ERROR_BAD_REQUEST); JSONObject json = new JSONObject(); String processingStatusCode = RevocationOutCome.code; try { json.accumulate("ProcessingStatusCode", processingStatusCode); } catch (JSONException e) { loggerPendingResource.error(e); } res = new JsonRepresentation(json); } } } catch (Exception e) { setStatus(Status.SERVER_ERROR_INTERNAL); res = new StringRepresentation( "INTERNAL SERVER ERROR (an unexpected error occured while processing the notification of pending revocation resolution)"); } return res; } }