// Path: src/main/java/com/ibm/ecm/extension/aspera/faspex/MaxRequestsReachedException.java
// public class MaxRequestsReachedException extends RuntimeException {
// 	MaxRequestsReachedException(final String message) {
// 		super(message);
// 	}
// }


import com.ibm.ecm.configuration.Config;
import com.ibm.ecm.extension.PluginLogger;
import com.ibm.ecm.extension.PluginResponseUtil;
import com.ibm.ecm.extension.PluginService;
import com.ibm.ecm.extension.PluginServiceCallbacks;
import com.ibm.ecm.extension.aspera.faspex.MaxRequestsReachedException;
import com.ibm.ecm.json.JSONMessage;
import com.ibm.ecm.json.JSONResponse;
import com.ibm.json.java.JSONObject;
import org.apache.struts.util.MessageResources;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;
/*
 * Licensed Materials - Property of IBM
 * (C) Copyright IBM Corp. 2010, 2017
 * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 */

package com.ibm.ecm.extension.aspera;



/**
 * The service to send packages.
 */
public class SendService extends PluginService {
	private static final String ID = "send";
	private static final String POST = "POST";
	private static final String execute = "execute";

	/**
	 * Returns the Id of the service.
	 *
	 * @return The Id of the service
	 */
	@Override
	public String getId() {
		return ID;
	}

	/**
	 * Validates and sends the package requested by the client. An error is returned if any of the parameters are invalid
	 * or the maximum number of concurrent requests configured in the plug-in configuration page has been reached.
	 *
	 * @param callbacks The PluginServiceCallbacks object provided by the ICN API
	 * @param request   The HttpServletRequest object provided by the ICN API
	 * @param response  The HttpServletResponse object provided by the ICN API
	 */
	@Override
	public void execute(final PluginServiceCallbacks callbacks, final HttpServletRequest request, final HttpServletResponse response) {
		final PluginLogger logger = callbacks.getLogger();
		logger.logEntry(this, execute);
		final JSONResponse responseJson = new JSONResponse();
		final MessageResources resources = callbacks.getPluginResources();
		final Locale locale = request.getLocale();

		try {
			validateRequest(request);
			final PackageSender sender = new PackageSender(Config.getDesktopConfig(Config.APPLICATION_NAME, request.getParameter("desktop")), callbacks);
			final JSONObject pkg = sender.sendPackage(request, response, callbacks);
			responseJson.put("pkg", pkg);
			responseJson.addInfoMessage(createMessage(resources, locale, "sendAction.started", pkg.get("title")));
		} catch (final MaxRequestsReachedException e) {
			logger.logDebug(this, execute, "max # of requests reached");
			responseJson.addErrorMessage(createMessage(resources, locale, "sendAction.maxError"));
// Path: src/main/java/com/ibm/ecm/extension/aspera/faspex/MaxRequestsReachedException.java
// public class MaxRequestsReachedException extends RuntimeException {
// 	MaxRequestsReachedException(final String message) {
// 		super(message);
// 	}
// }


// Path: src/main/java/com/ibm/ecm/extension/aspera/SendService.java
import com.ibm.ecm.configuration.Config;
import com.ibm.ecm.extension.PluginLogger;
import com.ibm.ecm.extension.PluginResponseUtil;
import com.ibm.ecm.extension.PluginService;
import com.ibm.ecm.extension.PluginServiceCallbacks;
import com.ibm.ecm.extension.aspera.faspex.MaxRequestsReachedException;
import com.ibm.ecm.json.JSONMessage;
import com.ibm.ecm.json.JSONResponse;
import com.ibm.json.java.JSONObject;
import org.apache.struts.util.MessageResources;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/*
 * Licensed Materials - Property of IBM
 * (C) Copyright IBM Corp. 2010, 2017
 * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 */

package com.ibm.ecm.extension.aspera;



/**
 * The service to send packages.
 */
public class SendService extends PluginService {
	private static final String ID = "send";
	private static final String POST = "POST";
	private static final String execute = "execute";

	/**
	 * Returns the Id of the service.
	 *
	 * @return The Id of the service
	 */
	@Override
	public String getId() {
		return ID;
	}

	/**
	 * Validates and sends the package requested by the client. An error is returned if any of the parameters are invalid
	 * or the maximum number of concurrent requests configured in the plug-in configuration page has been reached.
	 *
	 * @param callbacks The PluginServiceCallbacks object provided by the ICN API
	 * @param request   The HttpServletRequest object provided by the ICN API
	 * @param response  The HttpServletResponse object provided by the ICN API
	 */
	@Override
	public void execute(final PluginServiceCallbacks callbacks, final HttpServletRequest request, final HttpServletResponse response) {
		final PluginLogger logger = callbacks.getLogger();
		logger.logEntry(this, execute);
		final JSONResponse responseJson = new JSONResponse();
		final MessageResources resources = callbacks.getPluginResources();
		final Locale locale = request.getLocale();

		try {
			validateRequest(request);
			final PackageSender sender = new PackageSender(Config.getDesktopConfig(Config.APPLICATION_NAME, request.getParameter("desktop")), callbacks);
			final JSONObject pkg = sender.sendPackage(request, response, callbacks);
			responseJson.put("pkg", pkg);
			responseJson.addInfoMessage(createMessage(resources, locale, "sendAction.started", pkg.get("title")));
		} catch (final MaxRequestsReachedException e) {
			logger.logDebug(this, execute, "max # of requests reached");
			responseJson.addErrorMessage(createMessage(resources, locale, "sendAction.maxError"));
		} catch (final Exception e) { // NOPMD - log and continue writing the JSON response