package ca.leblanc.appdirect.controller;

import java.net.HttpURLConnection;
import java.net.URL;

import oauth.signpost.OAuthConsumer;
import oauth.signpost.basic.DefaultOAuthConsumer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import ca.leblanc.appdirect.domain.Result;

@Controller()
public class EventController {
	
	private static final Logger logger = LoggerFactory.getLogger(EventController.class);

	/**
     * <p>Buy event.</p>
     * 
     * <p>Expected HTTP GET and request '/event/buy'.</p>
     */
    @RequestMapping(value="/event/subscriptionOrder", method=RequestMethod.GET)
	public @ResponseBody Result subscriptionOrder(@RequestParam("eventUrl") String eventUrl) throws Exception  {
		
    	// log everything!
    	logger.info("************** Entering method");
    	 
    	// TODO: verify encryption
    	
    	// callback
    	OAuthConsumer consumer = new DefaultOAuthConsumer("bijoux-8197", "RHDwlCp4EhN6Mtmm");
    	
    	logger.info("************** opening url:" + eventUrl);
    	URL url = new URL(eventUrl);
    	HttpURLConnection request = (HttpURLConnection) url.openConnection();
    	consumer.sign(request);
    	request.connect();    	

    	logger.info("About to save!");

    	
    	// TODO: save order
    	
    	// read xml
    	String type = request.getContentType();
    	
    	logger.info("Content type is " + type);
    	
    	// send answer:
    	/*
    	<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    	<result>
    	    <success>true</success>
    	    <message>Account creation successful</message>
    	    <accountIdentifier>new-account-identifier</accountIdentifier>
    	</result>
*/
    	//or for errors:http://info.appdirect.com/developers/docs/event_references/api_error_codes/ 
    	
    	Result result = new Result(true, "Account creation successful", "fake124");
    	
    	/*
    	 * <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<result>
    <success>false</success>
    <errorCode>ACCOUNT_NOT_FOUND</errorCode>
    <message>The account TEST123 could not be found.</message>
</result>
For more information about the supported error codes, see the error code documentation.
    	 */
    	
    	return result;
    }
}
@@ -2557,9 +2557,1090 @@
 ;%0A    %7D%0A
+    %0A    /**%0A     * %3Cp%3EBuy event.%3C/p%3E%0A     * %0A     * %3Cp%3EExpected HTTP GET and request '/event/buy'.%3C/p%3E%0A     */%0A    @RequestMapping(value=%22/event/subscriptionCancel%22, method=RequestMethod.GET)%0A%09public @ResponseBody Result subscriptionCancel(@RequestParam(%22eventUrl%22) String eventUrl) throws Exception  %7B%0A%09%09%0A    %09// log everything!%0A    %09logger.info(%22************** Entering method%22);%0A    %09 %0A    %09// TODO: verify encryption%0A    %09%0A    %09// callback%0A    %09OAuthConsumer consumer = new DefaultOAuthConsumer(%22bijoux-8197%22, %22RHDwlCp4EhN6Mtmm%22);%0A    %09%0A    %09logger.info(%22************** opening url:%22 + eventUrl);%0A    %09URL url = new URL(eventUrl);%0A    %09HttpURLConnection request = (HttpURLConnection) url.openConnection();%0A    %09consumer.sign(request);%0A    %09request.connect();    %09%0A%0A    %09logger.info(%22About to save!%22);%0A%0A    %09%0A    %09// TODO: cancel order%0A    %09%0A    %09// read xml%0A    %09String type = request.getContentType();%0A    %09%0A    %09logger.info(%22Content type is %22 + type);%0A    %09%0A    %09Result result = new Result(true, %22Account creation successful%22, %22fake124%22);%0A    %0A    %09return result;%0A    %7D    %0A
 %7D

Add cancel order