package com.github.dynamicextensionsalfresco.controlpanel;

import com.github.dynamicextensionsalfresco.osgi.Configuration;
import com.github.dynamicextensionsalfresco.webscripts.annotations.*;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.extensions.surf.util.Content;
import org.springframework.extensions.webscripts.WebScriptException;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WebScriptResponse;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Handles requests for the Bundle REST API.
 * <p>
 * The the endpoints defined are intended to be called by a REST client rather a web browser. than a web browser.
 * 
 * @author Laurens Fridael
 * 
 */
@Component
@WebScript(baseUri = "/dynamic-extensions/api/bundles")
@Authentication(value = AuthenticationType.ADMIN)
public class BundleRestApi {

	private static final String JAR_MIME_TYPE = "application/java-archive";

	/* Dependencies */

	@Autowired
	private BundleHelper bundleHelper;

	@Autowired
	private Configuration configuration;

	/* Main operations */

	@Uri(method = HttpMethod.POST)
	public void installBundle(final Content content, @Header("Content-Type") final String contentType,
			@Attribute final JsonResponseHelper response) throws IOException, BundleException {
		response.checkBundleInstallConfiguration();
		if (JAR_MIME_TYPE.equalsIgnoreCase(contentType) == false) {
			throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST,
					String.format("Can only accept content of type '%s'.", JAR_MIME_TYPE));
		}
		final Bundle bundle = bundleHelper.installBundleInRepository(content);
		response.sendBundleInstalledMessage(bundle);
	}

	/* Utility operations */

	@Attribute
	protected JsonResponseHelper getJsonResponseHelper(final WebScriptRequest request, final WebScriptResponse response) {
		return new JsonResponseHelper(request, response, configuration);
	}

}

@@ -1765,18 +1765,24 @@
 ));%0A%09%09%7D%0A
-%09%09
+        
 final Bu
@@ -1786,16 +1786,50 @@
  Bundle 
+bundle;%0A        try %7B%0A            
 bundle =
@@ -1878,18 +1878,28 @@
 ntent);%0A
-%09%09
+            
 response
@@ -1935,16 +1935,160 @@
 undle);%0A
+        %7D catch (BundleException e) %7B%0A            response.sendMessage(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());%0A        %7D%0A
 %09%7D%0A%0A%09/* 

Return Bundle installation errors as a JSON message. Provides better feedback for the installBundle task.