All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.adobe.aem.formsndocuments.rnc.ReviewManagerServlet Maven / Gradle / Ivy

/***************************************************************************
 * ADOBE CONFIDENTIAL
 * ___________________
 *
 *  Copyright 2013 Adobe Systems Incorporated
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and may be covered by U.S. and Foreign Patents,
 * patents in process, and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 **************************************************************************/
package com.adobe.aem.formsndocuments.rnc;

import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.Servlet;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.jcr.api.SlingRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adobe.aem.formsndocuments.exception.FormsNDocumentsException;
import com.adobe.aem.formsndocuments.util.RnCUtil;
import com.adobe.aemforms.fm.exception.FormsMgrException;

/**
 * The ReviewManagerServlet is the servlet handling requests for
 * management of reviews.
 * 
 * @author sakarora
 */
@Component(immediate = true, label = "ReviewManagerServlet", description = "ReviewManagerServlet")
@Service(value = Servlet.class)
@Properties(value = {
		@Property(name = "sling.servlet.extensions", value = { "json" }),
		@Property(name = "sling.servlet.resourceTypes", value = "fd/fm/reviewServlet"),
		@Property(name = "sling.servlet.methods", value = { "GET", "POST" }) })
public class ReviewManagerServlet extends SlingAllMethodsServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1059891247634439445L;
	private static final String REVIEW_ACTION = "reviewAction";
	private static final String CREATE_REVIEW = "createReview";
	private static final String UPDATE_REVIEW = "updateReview";
	private static final String END_REVIEW = "endReview";

	private static final String UTF8_CHAR_ENCODING = "utf-8";
	private static final String CONTENT_JSON = "application/json";

	private static final String ERROR_MESSAGE = "errorMessage";
	private static final String ERROR_CODE = "errorCode";

	@Reference(referenceInterface = ReviewManagementService.class)
	ReviewManagementService reviewManagementService;

	@Reference(referenceInterface = SlingRepository.class)
	SlingRepository slingRepository;

	private static Logger logger = LoggerFactory
			.getLogger(ReviewManagerServlet.class);

	private static void writeErrorResponseHeader(
			SlingHttpServletResponse response) {
		response.setContentType(CONTENT_JSON);
		response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);
	}

	private static void writeResponseError(OutputStream outputStream,
			FormsNDocumentsException fndException) {
		logger.trace("Entering writeResponseError.");
		Map errorMap = new HashMap();
		errorMap.put(ERROR_CODE, fndException.getCode());
		errorMap.put(ERROR_MESSAGE, fndException.getMessage());
		JSONArray jarray = new JSONArray();
		jarray.put(errorMap);
		try {
			outputStream.write(jarray.toString().getBytes());
		} catch (IOException e) {
			logger.error("Error while writting error information " , e);
		}
		logger.trace("Exiting writeResponseError.");
	}

	public void doGet(SlingHttpServletRequest request,
			SlingHttpServletResponse response) {
		logger.trace("Entering doGet.");
		String assetPath = request.getParameter(RnCUtil.ASSET_PATH);
		Map reviewInfo = null;
		response.setCharacterEncoding(UTF8_CHAR_ENCODING);
		OutputStream outputStream = null;
		try {
			response.setContentType(CONTENT_JSON);
			outputStream = response.getOutputStream();
			RnCUtil.checkAssetPathArgument(assetPath);
			reviewInfo = reviewManagementService.fetchReviewInfo(request.getResourceResolver(), assetPath);
			JSONArray jarray = new JSONArray();
			jarray.put(reviewInfo);
			outputStream.write(jarray.toString().getBytes());
		} catch (FormsMgrException fmException) {
			logger.error("Error fetching the review information " , fmException);
			if (fmException instanceof FormsNDocumentsException) {
				writeErrorResponseHeader(response);
				writeResponseError(outputStream,
						(FormsNDocumentsException) fmException);
			}
		} catch (IOException e) {
			logger.error("Error while writting data " , e);
		} finally {
			if (outputStream != null) {
				try {
					outputStream.close();
				} catch (IOException e) {
					logger.error("Failed to close the object output stream" , e);
				}
			}
		}
		logger.trace("Exiting doGet.");
	}

	public void doPost(SlingHttpServletRequest request,
			SlingHttpServletResponse response) {
		String action = request.getParameter(REVIEW_ACTION);
		String assetPath = request.getParameter(RnCUtil.ASSET_PATH);
		OutputStream outputStream = null;
		try {
			RnCUtil.checkAssetPathArgument(assetPath);
			if (CREATE_REVIEW.equals(action)) {
				String reviewName = request.getParameter(RnCUtil.REVIEW_NAME);
				String reviewDescription = request
						.getParameter(RnCUtil.REVIEW_DESCRIPTION);
				String deadline = request.getParameter(RnCUtil.REVIEW_DEADLINE);
				String[] reviewerList = request
						.getParameterValues(RnCUtil.REVIEWERS);
				reviewManagementService.beginReview(reviewName,
						reviewDescription, deadline, reviewerList, assetPath);
			} else if (UPDATE_REVIEW.equals(action)) {
				String reviewDescription = request
						.getParameter(RnCUtil.REVIEW_DESCRIPTION);
				String deadline = request.getParameter(RnCUtil.REVIEW_DEADLINE);
				String[] reviewerList = request
						.getParameterValues(RnCUtil.REVIEWERS);
				reviewManagementService.updateReview(assetPath,
						reviewDescription, deadline, reviewerList);
			} else if (END_REVIEW.equals(action)) {
				reviewManagementService.endReview(assetPath);
			} else {
				Object[] args = { action };
				throw new FormsNDocumentsException(
						FormsNDocumentsException.ERROR_AEM_FMG_700_018, args);
			}
		} catch (FormsMgrException fmException) {
			logger.error("Error fetching the review information " , fmException);
			if (fmException instanceof FormsNDocumentsException) {
				FormsNDocumentsException fndException = (FormsNDocumentsException) fmException;
				try {
					writeErrorResponseHeader(response);
					outputStream = response.getOutputStream();
					writeResponseError(outputStream, fndException);
				} catch (IOException e) {
					logger.error("Error while writting data " , e);
				}
			}
		} finally {
			if (outputStream != null) {
				try {
					outputStream.close();
				} catch (IOException e) {
					logger.error("Failed to close the object output stream" , e);
				}
			}
		}

	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy