Commits

Anonymous committed ab0526e

added Json reflection for analysing returned Json; started metadataUrl

Comments (0)

Files changed (8)

src/main/java/org/xmlcml/ckan/Client.java

     */
     public Dataset getDataset(String name) throws CKANException {
     	
-        String returned_json = this._connection.post(API_ACTION_PACKAGE_SHOW,
+        String returned_json = this._connection.execPostRequest(API_ACTION_PACKAGE_SHOW,
                                                      ID_PREFIX + name + SUFFIX );
-        
         if( returned_json != null ) {
         	log.warn(name + " returned json length "+returned_json.length());
         }else {
     public List<DatasetRevision> getDatasetRevisions(String datasetName) throws CKANException {
     	
     	// e.g. http://datahub.io/api/3/action/package_revision_list?id=klappstuhlclub
-        String returned_json = this._connection.post(API_ACTION_PACKAGE_REVISION_LIST,
+        String returned_json = this._connection.execPostRequest(API_ACTION_PACKAGE_REVISION_LIST,
         											 ID_PREFIX + datasetName + SUFFIX );
         //log.warn("revisions: " + returned_json);
         
     */
     public void deleteDataset(String name) throws CKANException {
         
-    	String returned_json = this._connection.post(API_ACTION_PACKAGE_DELETE,
+    	String returned_json = this._connection.execPostRequest(API_ACTION_PACKAGE_DELETE,
                                                      ID_PREFIX + name + SUFFIX );
         
         Dataset.Response r = loadClass( Dataset.Response.class, returned_json);
         Gson gson = new Gson();
         String data = gson.toJson( dataset );
         log.info("data: "+ data );
-        String returned_json = this._connection.post(API_ACTION_PACKAGE_CREATE, data);
+        String returned_json = this._connection.execPostRequest(API_ACTION_PACKAGE_CREATE, data);
         
         log.debug( "returned length: "+returned_json );
         Dataset.Response r = loadClass( Dataset.Response.class, returned_json);
     	
         Gson gson = new Gson();
         String data = gson.toJson( dataset );
-        System.out.println( data );
-        String returned_json = this._connection.post(API_ACTION_PACKAGE_UPDATE, data);
+        log.debug("JSON: "+ data );
+        String returned_json = this._connection.execPostRequest(API_ACTION_PACKAGE_UPDATE, data);
         
         //System.out.println( returned_json );
         Dataset.Response r = loadClass( Dataset.Response.class, returned_json);
     */
     public Group getGroup(String name) throws CKANException {
     	
-        String returned_json = this._connection.post(API_ACTION_GROUP_SHOW,
+        String returned_json = this._connection.execPostRequest(API_ACTION_GROUP_SHOW,
                                                      ID_PREFIX + name + SUFFIX );
         
         Group.Response r = loadClass( Group.Response.class, returned_json);
     */
     public void deleteGroup(String name) throws CKANException {
     	
-        String returned_json = this._connection.post(API_ACTION_GROUP_DELETE,
+        String returned_json = this._connection.execPostRequest(API_ACTION_GROUP_DELETE,
                                                      ID_PREFIX + name + SUFFIX );
         
         Group.Response r = loadClass( Group.Response.class, returned_json);
     	
         Gson gson = new Gson();
         String data = gson.toJson( group );
-        String returned_json = this._connection.post(API_ACTION_PACKAGE_CREATE, data );
+        String returned_json = this._connection.execPostRequest(API_ACTION_PACKAGE_CREATE, data );
         
         Group.Response r = loadClass( Group.Response.class, returned_json);
         if (r == null || ! r.success ) {
     */
     public Dataset.SearchResults findDatasets(String query) throws CKANException {
 
-        String returned_json = this._connection.post(API_ACTION_PACKAGE_SEARCH,
+        String returned_json = this._connection.execPostRequest(API_ACTION_PACKAGE_SEARCH,
                                                      "{\"q\":\"" + query +SUFFIX );
         
         Dataset.SearchResponse sr = loadClass( Dataset.SearchResponse.class, returned_json);

src/main/java/org/xmlcml/ckan/Connection.java

 import java.net.MalformedURLException;
 import java.net.URL;
 
+import org.apache.http.Header;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpHost;
 import org.apache.http.HttpResponse;
 import org.apache.http.client.ClientProtocolException;
-import org.apache.http.client.HttpClient;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.entity.StringEntity;
  */
 public final class Connection {
 
+	public static final String APIKEY = "APIKEY";
 	private static final String HTTP_THEDATAHUB_ORG = "http://thedatahub.org";
 	public static final String X_CKAN_API_KEY = "X-CKAN-API-Key";
 	public static final String USER_AGENT = "User-Agent";
 	private static final String HTTP_PROXY_HOST = "http.proxyHost";
 	private static final String HTTP_PROXY_PORT = "http.proxyPort";
 
-	private static final Logger log = Logger.getLogger(Connection.class);
+	private static final Logger LOG = Logger.getLogger(Connection.class);
 	
     private String m_host;
     private int    m_port;
     }
 
     public Connection( String host, int port ) {
+
+    	setApiKey(System.getenv(APIKEY));
     	
     	// Hack, since this.Post() does not follow redirects.
     	// http://stackoverflow.com/questions/7546849/httpclient-redirect-for-newbies
      * 
      * @throws A CKANException if the request fails
      */
-    protected String post(String path, String data) throws CKANException {
+    protected String execPostRequest(String path, String data) throws CKANException {
 
     	URL url = createURLFromHostAndPort(path);
-    	return post(url, data);
+    	String body = execPostRequest(url, data);
+    	return body;
     }
 
     /**
     protected String get(String path) throws CKANException {
 
     	URL url = createURLFromHostAndPort(path);
-    	return get(url);
+    	return execGetRequest(url);
     }
 
     /**
      * 
      * @throws A CKANException if the request fails
      */
-    protected String post(URL url, String data) throws CKANException {
+    protected String execPostRequest(URL url, String data) throws CKANException {
 
     	String body = "";
 
     			throw new RuntimeException("Cannout close client: ", ioe);
     		}
     	}
+    	Header[] headers = httpResponse.getAllHeaders();
+    	LOG.trace("headers: "+headers.length);
+    	for (Header header : headers) {
+    		LOG.debug("header: "+header);
+    	}
 
     	return body;
     }
      * 
      * @throws A CKANException if the request fails
      */
-    protected String get(URL url) throws CKANException {
+    protected String execGetRequest(URL url) throws CKANException {
 
     	String body = "";
-
     	httpClient = createHttpClientOrProxy();
     	
     	try {
 	}
 
 	private String processResponse() throws IOException {
+		String body = null;
 		httpEntity = httpResponse.getEntity();
 		statusCode = httpResponse.getStatusLine().getStatusCode();
-		log.info("POST response code " + statusCode);
-		if (statusCode == 301 || statusCode == 302 || statusCode == 303 || statusCode == 307) {
-// see			http://hc.apache.org/httpclient-3.x/redirects.html
-//			Header locationHeader = method.getResponseHeader("location");
-//	        if (locationHeader != null) {
-//	            redirectLocation = locationHeader.getValue();
-	            
-//			postRequest.getMethod().getResponseHeader("location");
+		LOG.info("response code " + statusCode);
+		if (statusCode == 200){
+			body = createLinesFromEntity();
+		} else if (statusCode == 301 || statusCode == 302 || statusCode == 303 || statusCode == 307) {
 			redirect();
+		} else if (statusCode == 404) {
+			LOG.error("NOT FOUND: ");
+		} else if (statusCode == 500) {
+			LOG.error("Aborted");
+		} else {
+			LOG.error("Untrapped HTTP code: "+statusCode);
 		}
-		String body = createLinesFromEntity();
 		return body;
 	}
 
 	}
 
 	private void redirect() {
-		log.debug("Redirect NYI");
+		// see			http://hc.apache.org/httpclient-3.x/redirects.html
+//		Header locationHeader = method.getResponseHeader("location");
+//        if (locationHeader != null) {
+//            redirectLocation = locationHeader.getValue();
+            
+//		postRequest.getMethod().getResponseHeader("location");
+		LOG.debug("Redirect NYI");
 	}
 
 	HttpPost createPostRequest(String data, URL url)
 		postRequest.setHeader( X_CKAN_API_KEY, this._apikey );
 
 		StringEntity input = new StringEntity(data);
-		input.setContentType(APPLICATION_JSON);
-		log.debug("Input: "+ input);
+//		input.setContentType(APPLICATION_JSON);
+		LOG.debug("Input: "+ input);
 		postRequest.setEntity(input);
 		return postRequest;
 	}
     	 * http://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientExecuteProxy.java
     	 */
     	if( isSet(System.getProperty(HTTP_PROXY_HOST)) ) {
-	    	log.warn("http.proxyHost = " + System.getProperty(HTTP_PROXY_HOST) );
-	    	log.warn("http.proxyPort = " + System.getProperty(HTTP_PROXY_PORT));
+	    	LOG.warn("http.proxyHost = " + System.getProperty(HTTP_PROXY_HOST) );
+	    	LOG.warn("http.proxyPort = " + System.getProperty(HTTP_PROXY_PORT));
 	    	
 	    	HttpClientBuilder hcBuilder = HttpClients.custom();
 
 
     	try {
     		url = new URL( this.m_host + ":" + this.m_port + path);
-        	log.warn("POSTing to " + url.toString());
+        	LOG.warn("POSTing to " + url.toString());
     	} catch ( MalformedURLException mue ) {
     		throw new CKANException("malformed URL"+mue.getMessage());
     	}
     private static boolean isSet(String string) {
     	return string != null && string.length() > 0;
     }
+
+	public HttpResponse getHttpResponse() {
+		return httpResponse;
+	}
 }

src/main/java/org/xmlcml/ckan/JsonField.java

+package org.xmlcml.ckan;
+
+public class JsonField {
+
+	/**
+"fields": [
+{"name": "x-goog-meta-uploaded-by",  "value": "was APIKEY"}, 
+{"name": "acl", "value": "public-read"}, 
+{"name": "success_action_redirect", "value": "http://datahub.io/storage/upload/success_empty?label=2013-12-22T13%3A24%3A12.853Z%2Ftest.xml"}, 
+{"name": "policy", "value": "eyJleHBpcmF0aW9uIjogIjIwMTMtMTItMjNUMDk6MjQ6MThaIiwKImNvbmRpdGlvbnMiOiBbeyJ4LWdvb2ctbWV0YS11cGxvYWRlZC1ieSI6ICIzYmFlNDAxMy1kMDNlLTQyN2ItYWMyZS05ZTVkMDg0ZDBlYTkifSx7ImJ1Y2tldCI6ICJja2FubmV0LXN0b3JhZ2UifSx7ImtleSI6ICIyMDEzLTEyLTIyVDEzOjI0OjEyLjg1M1ovdGVzdC54bWwifSx7ImFjbCI6ICJwdWJsaWMtcmVhZCJ9LHsic3VjY2Vzc19hY3Rpb25fcmVkaXJlY3QiOiAiaHR0cDovL2RhdGFodWIuaW8vc3RvcmFnZS91cGxvYWQvc3VjY2Vzc19lbXB0eT9sYWJlbD0yMDEzLTEyLTIyVDEzJTNBMjQlM0ExMi44NTNaJTJGdGVzdC54bWwifSxbImNvbnRlbnQtbGVuZ3RoLXJhbmdlIiwgMCwgMTAwMDAwMDAwMDBdXX0="},
+{"name": "GoogleAccessId", "value": "GOOGC6OU3AYPNY47B66M"}, 
+{"name": "signature", "value": "VFYngO/4ZQRYt4YXnRGXitiJyNE="}, 
+{"name": "key", "value": "2013-12-22T13:24:12.853Z/test.xml"}
+]
+	 */
+	String name;
+	String value;
+	
+	public String getName() {
+		return name;
+	}
+	
+	public String getValue() {
+		return value;
+	}
+}

src/main/java/org/xmlcml/ckan/JsonResponse.java

+package org.xmlcml.ckan;
+
+public class JsonResponse {
+
+	/**
+	 * response looks like:
+	 * 
+{"action": "http://ckannet-storage.commondatastorage.googleapis.com/", 
+"fields": [
+{"name": "x-goog-meta-uploaded-by",  "value": "was APIKEY"}, 
+{"name": "acl", "value": "public-read"}, 
+{"name": "success_action_redirect", "value": "http://datahub.io/storage/upload/success_empty?label=2013-12-22T13%3A24%3A12.853Z%2Ftest.xml"}, 
+{"name": "policy", "value": "eyJleHBpcmF0aW9uIjogIjIwMTMtMTItMjNUMDk6MjQ6MThaIiwKImNvbmRpdGlvbnMiOiBbeyJ4LWdvb2ctbWV0YS11cGxvYWRlZC1ieSI6ICIzYmFlNDAxMy1kMDNlLTQyN2ItYWMyZS05ZTVkMDg0ZDBlYTkifSx7ImJ1Y2tldCI6ICJja2FubmV0LXN0b3JhZ2UifSx7ImtleSI6ICIyMDEzLTEyLTIyVDEzOjI0OjEyLjg1M1ovdGVzdC54bWwifSx7ImFjbCI6ICJwdWJsaWMtcmVhZCJ9LHsic3VjY2Vzc19hY3Rpb25fcmVkaXJlY3QiOiAiaHR0cDovL2RhdGFodWIuaW8vc3RvcmFnZS91cGxvYWQvc3VjY2Vzc19lbXB0eT9sYWJlbD0yMDEzLTEyLTIyVDEzJTNBMjQlM0ExMi44NTNaJTJGdGVzdC54bWwifSxbImNvbnRlbnQtbGVuZ3RoLXJhbmdlIiwgMCwgMTAwMDAwMDAwMDBdXX0="},
+{"name": "GoogleAccessId", "value": "GOOGC6OU3AYPNY47B66M"}, 
+{"name": "signature", "value": "VFYngO/4ZQRYt4YXnRGXitiJyNE="}, 
+{"name": "key", "value": "2013-12-22T13:24:12.853Z/test.xml"}
+]
+}
+	 */
+
+	private String action;
+	private JsonField[] fields;
+	
+	protected String getAction() {
+		return action;
+	}
+	protected JsonField[] getFields() {
+		return fields;
+	}
+}

src/main/java/org/xmlcml/ckan/UploadData.java

 package org.xmlcml.ckan;
 
-import java.io.BufferedReader;
-import java.io.DataOutputStream;
 import java.io.File;
+import java.io.IOException;
 import java.io.InputStream;
-import java.io.InputStreamReader;
 import java.io.UnsupportedEncodingException;
-import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLEncoder;
 import java.util.List;
 import java.util.Map;
 
-import javax.net.ssl.HttpsURLConnection;
-
+import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.FilenameUtils;
+import org.apache.commons.io.IOUtils;
 import org.apache.http.Header;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpRequest;
 import org.apache.http.HttpResponse;
 import org.apache.http.StatusLine;
+import org.apache.http.client.methods.HttpPost;
 import org.apache.log4j.Logger;
 import org.joda.time.DateTime;
 
+import com.google.gson.Gson;
+
 public class UploadData {
 
 
+	private static final String API_STORAGE_METADATA = "/api/storage/metadata/";
+	private static final String KEY = "key";
+	private static final String SUCCESS_ACTION_REDIRECT = "success_action_redirect";
+	
+	private static final String POST_METHOD = "POST";
 	private static final String APPLICATION_JSON = "application/json";
 	private static final String CONTENT_TYPE = "Content-Type";
 	private static final String UTF_8 = "UTF-8";
 	private String date;
 	private String storageKey;
 	private URL uploadUrl;
+	private String responseBody;
+	private String redirectUrlString;
+	private String action;
+	private Connection connection;
+	private URL redirectUrl;
+	private String fileData;
+	private HttpPost httpPost;
+	private String uploadResponseBody;
+	private String uploadKey;
+	private URL metadataUrl;
 	 
 	 
 
 	
 	private void init() {
 		baseLocation = HTTP_DATAHUB_IO_API;
+		
 	}
 	
 	public void createUrlAndKey(File file) {
 		}
 	}
 	
-	public void getAuthTicket(Client client, File file) throws CKANException {
-		String data = "";
+	public void execPostRequestForFileUpload(Client client, File file) {
+		try {
+			fileData = FileUtils.readFileToString(file);
+		} catch (IOException e) {
+			throw new RuntimeException("IOE "+uploadUrl, e);
+		}
 		createUrlAndKey(file);
 		try {
-			client.getConnection().createPostRequest(data, uploadUrl);
+			connection = client.getConnection();
+			connection.createPostRequest(fileData, uploadUrl);
 		} catch (UnsupportedEncodingException e) {
 			throw new RuntimeException("UEE "+uploadUrl, e);
 		}
-		String body = client.getConnection().post(uploadUrl, data);
-		LOG.debug("returned: "+body);
+		try {
+			responseBody = connection.execPostRequest(uploadUrl, fileData);
+		} catch (CKANException e) {
+			throw new RuntimeException("CKANE "+uploadUrl, e);
+		}
+		LOG.trace("returned: "+responseBody);
+		interpretResponse();
+		LOG.debug("UPLOAD: "+uploadKey);
+		
+		/**
+		 * response looks like:
+		 * 
+{"action": "http://ckannet-storage.commondatastorage.googleapis.com/", 
+"fields": [{"name": "x-goog-meta-uploaded-by",  "value": "was APIKEY"}, 
+    {"name": "acl", "value": "public-read"}, 
+    {"name": "success_action_redirect", "value": "http://datahub.io/storage/upload/success_empty?label=2013-12-22T13%3A24%3A12.853Z%2Ftest.xml"}, 
+    {"name": "policy", "value": "eyJleHBpcmF0aW9uIjogIjIwMTMtMTItMjNUMDk6MjQ6MThaIiwKImNvbmRpdGlvbnMiOiBbeyJ4LWdvb2ctbWV0YS11cGxvYWRlZC1ieSI6ICIzYmFlNDAxMy1kMDNlLTQyN2ItYWMyZS05ZTVkMDg0ZDBlYTkifSx7ImJ1Y2tldCI6ICJja2FubmV0LXN0b3JhZ2UifSx7ImtleSI6ICIyMDEzLTEyLTIyVDEzOjI0OjEyLjg1M1ovdGVzdC54bWwifSx7ImFjbCI6ICJwdWJsaWMtcmVhZCJ9LHsic3VjY2Vzc19hY3Rpb25fcmVkaXJlY3QiOiAiaHR0cDovL2RhdGFodWIuaW8vc3RvcmFnZS91cGxvYWQvc3VjY2Vzc19lbXB0eT9sYWJlbD0yMDEzLTEyLTIyVDEzJTNBMjQlM0ExMi44NTNaJTJGdGVzdC54bWwifSxbImNvbnRlbnQtbGVuZ3RoLXJhbmdlIiwgMCwgMTAwMDAwMDAwMDBdXX0="},
+    {"name": "GoogleAccessId", "value": "GOOGC6OU3AYPNY47B66M"}, 
+    {"name": "signature", "value": "VFYngO/4ZQRYt4YXnRGXitiJyNE="}, 
+    {"name": "key", "value": "2013-12-22T13:24:12.853Z/test.xml"}]}
+		 */
 	}
+	
+	private void interpretResponse() {
+        Gson gson = new Gson();
+        JsonResponse response = gson.fromJson(responseBody, JsonResponse.class);
+        if (response == null) {
+        	LOG.error("NULL response");
+        } else {
+	        action = response.getAction();
+	        LOG.debug("Action: "+action);
+	        for (JsonField field : response.getFields()) {
+	        	LOG.debug(field.getName()+" => "+field.getValue());
+	        	String name = field.getName();
+	        	if (SUCCESS_ACTION_REDIRECT.equals(name)) {
+	        		redirectUrlString = field.getValue();
+	        	} else if (KEY.equals(name)) {
+	        		uploadKey = field.getValue();
+	        	}
+	        }
+        }
+	}
+
 	/**
     # see ckan/public/application.js:makeUploadKey for why the file_key
     # is derived this way.
 
 	void makeNormName() {
 		// basename of foo/pom.xml is "pom"
-		normName = FilenameUtils.getBaseName(filename);
+//		normName = FilenameUtils.getBaseName(filename);
+		normName = FilenameUtils.getName(filename);
 		// replace spaces
 		normName = normName.replaceAll(" ",  "-");
 		LOG.debug("normname: "+normName);
 	 */
 	void makeFileKey() {
 		fileKey = ts + "/"+normName;
-		fileKey = date + "/"+normName;
+//		fileKey = date + "/"+normName;
 		LOG.debug("filekey: "+fileKey);
 	}
 
 		String url = storageAuthUrl(label);
 		String payload = dumpstr(headers);
 		try {
-			openUrl(url, payload, new ArrayList<String>(Arrays.asList(new String[]{"POST"})));
+			openUrl(url, payload, new ArrayList<String>(Arrays.asList(new String[]{POST_METHOD})));
 		} catch (CKANException e) {
 			throw new RuntimeException("could not POST", e);
 		}
 	}
 	
 	
-/**
- * 	How to use HttpURLConnection POST data to web server?
- * 
-			The urlParameters is a URL encoded string.
+	public String getResponseBody() {
+		return responseBody;
+	}
 
-			 String urlParameters =
-			        "fName=" + URLEncoder.encode("???", "UTF-8") +
-			        "&lName=" + URLEncoder.encode("???", "UTF-8")
-*/
-	public static String excutePost(String targetURL, String urlParameters) {
-		URL url;
-		HttpURLConnection connection = null;
-		try {
-			// Create connection
-			url = new URL(targetURL);
-			connection = (HttpURLConnection) url.openConnection();
-			connection.setRequestMethod("POST");
-			connection.setRequestProperty("Content-Type",
-					"application/x-www-form-urlencoded");
-
-			connection.setRequestProperty("Content-Length",
-					"" + Integer.toString(urlParameters.getBytes().length));
-			connection.setRequestProperty("Content-Language", "en-US");
-
-			connection.setUseCaches(false);
-			connection.setDoInput(true);
-			connection.setDoOutput(true);
-
-			// Send request
-			DataOutputStream wr = new DataOutputStream(
-					connection.getOutputStream());
-			wr.writeBytes(urlParameters);
-			wr.flush();
-			wr.close();
-
-			// Get Response
-			InputStream is = connection.getInputStream();
-			BufferedReader rd = new BufferedReader(new InputStreamReader(is));
-			String line;
-			StringBuffer response = new StringBuffer();
-			while ((line = rd.readLine()) != null) {
-				response.append(line);
-				response.append('\r');
-			}
-			rd.close();
-			return response.toString();
-
-		} catch (Exception e) {
-
-			e.printStackTrace();
-			return null;
-
-		} finally {
-
-			if (connection != null) {
-				connection.disconnect();
-			}
+	public void upload(File file) {
+		execPostRequestForFileUpload(Fixtures.getDataHubClient(), file);
+		System.out.println("=================================================");
+		if (responseBody != null && redirectUrlString != null) {
+			getMetadataForUrl();
+			debugResponseEntity();
+			System.out.println("---------------------------------------------");
+			postDataToRedirectUrl();
 		}
 	}
-	
-	// HTTP GET request
-	private void sendGet() throws Exception {
- 
-		String url = "http://www.google.com/search?q=mkyong";
- 
-		URL obj = new URL(url);
-		HttpURLConnection con = (HttpURLConnection) obj.openConnection();
- 
-		// optional default is GET
-		con.setRequestMethod("GET");
- 
-		//add request header
-		con.setRequestProperty("User-Agent", USER_AGENT);
- 
-		int responseCode = con.getResponseCode();
-		System.out.println("\nSending 'GET' request to URL : " + url);
-		System.out.println("Response Code : " + responseCode);
- 
-		BufferedReader in = new BufferedReader(
-		        new InputStreamReader(con.getInputStream()));
-		String inputLine;
-		StringBuffer response = new StringBuffer();
- 
-		while ((inputLine = in.readLine()) != null) {
-			response.append(inputLine);
+
+	private void getMetadataForUrl() {
+		try {
+			metadataUrl = new URL(HTTP_DATAHUB_IO_API+API_STORAGE_METADATA+uploadKey);
+			LOG.debug("META "+metadataUrl);
+			String metadataResponseBody = connection.execPostRequest(metadataUrl, "");
+		} catch (Exception e) {
+			throw new RuntimeException("cannot get metadata "+metadataUrl, e);
 		}
-		in.close();
- 
-		//print result
-		System.out.println(response.toString());
- 
 	}
- 
-	// HTTP POST request
-	private void sendPost() throws Exception {
- 
-		String url = "https://selfsolve.apple.com/wcResults.do";
-		URL obj = new URL(url);
-		HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
- 
-		//add reuqest header
-		con.setRequestMethod("POST");
-		con.setRequestProperty("User-Agent", USER_AGENT);
-		con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
- 
-		String urlParameters = "sn=C02G8416DRJM&cn=&locale=&caller=&num=12345";
- 
-		// Send post request
-		con.setDoOutput(true);
-		DataOutputStream wr = new DataOutputStream(con.getOutputStream());
-		wr.writeBytes(urlParameters);
-		wr.flush();
-		wr.close();
- 
-		int responseCode = con.getResponseCode();
-		System.out.println("\nSending 'POST' request to URL : " + url);
-		System.out.println("Post parameters : " + urlParameters);
-		System.out.println("Response Code : " + responseCode);
- 
-		BufferedReader in = new BufferedReader(
-		        new InputStreamReader(con.getInputStream()));
-		String inputLine;
-		StringBuffer response = new StringBuffer();
- 
-		while ((inputLine = in.readLine()) != null) {
-			response.append(inputLine);
+
+	private void debugResponseEntity() {
+		httpResponse = connection.getHttpResponse();
+		HttpEntity httpEntity = httpResponse.getEntity();
+		long entityLength = httpEntity.getContentLength();
+		if (entityLength > 0) {
+			try {
+				LOG.debug("Content: "+IOUtils.toString(httpEntity.getContent()));
+			} catch (Exception e) {
+				throw new RuntimeException("Cannot write content", e);
+			}
+			LOG.debug("UploadResponse: "+uploadResponseBody);
 		}
-		in.close();
- 
-		//print result
-		System.out.println(response.toString());
- 
+		for (Header header : httpResponse.getAllHeaders()) {
+			LOG.debug("header: "+header);
+		}
+	}
+
+	private void execPostRequest() {
+		try {
+			uploadResponseBody = connection.execPostRequest(redirectUrl, fileData);
+		} catch (CKANException e) {
+			throw new RuntimeException("CKANE "+redirectUrl, e);
+		}
+	}
+
+	private void postDataToRedirectUrl() {
+		try {
+			redirectUrl = new URL(redirectUrlString);
+			execPostRequest();
+			debugResponseEntity();
+		} catch (Exception e) {
+			throw new RuntimeException("Cannot post to redirectUrl: "+redirectUrlString, e);
+		}
 	}
 
 }

src/test/java/org/xmlcml/ckan/Fixtures.java

 	}
 
 	public static Client getDataHubClient() {
-		Client client = new Client(new Connection(Fixtures.DATAHUB_IO), "");
+		Client client = new Client(new Connection(Fixtures.DATAHUB_IO), System.getenv(Connection.APIKEY));
 		return client;
 	}
 

src/test/java/org/xmlcml/ckan/UploadDataTest.java

 package org.xmlcml.ckan;
 
 import java.io.File;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 
-import org.apache.commons.io.FilenameUtils;
 import org.apache.log4j.Logger;
-import org.junit.Assert;
 import org.junit.Test;
 
+import com.google.gson.Gson;
+
 public class UploadDataTest {
 
+	private static final String TEST_XML = "src/test/resources/org/xmlcml/ckan/test.xml";
 	private final static Logger LOG = Logger.getLogger(UploadDataTest.class);
 	
 	@Test
-	public void createUrlKeyTest() {
+	public void testCreateUrlKey() {
 		UploadData uploadData = new UploadData();
-		File file = new File("pom.xml"); // good as any
+		File file = new File(TEST_XML); // good as any
 		uploadData.createUrlAndKey(file);
 	}
 
 	@Test
-	public void getAuthTicketTest() throws Exception {
+	public void testExecPostRequestForFileUpload() throws Exception {
 		UploadData uploadData = new UploadData();
-		File file = new File("pom.xml"); // good as any
-		uploadData.getAuthTicket(Fixtures.getDataHubClient(), file);
+		File file = new File(TEST_XML); 
+		uploadData.execPostRequestForFileUpload(Fixtures.getDataHubClient(), file);
+		String body = uploadData.getResponseBody(); // gives
+		/**
+{"action": "http://ckannet-storage.commondatastorage.googleapis.com/", 
+"fields": [{"name": "x-goog-meta-uploaded-by",  "value": "was APIKEY"}, 
+    {"name": "acl", "value": "public-read"}, 
+    {"name": "success_action_redirect", "value": "http://datahub.io/storage/upload/success_empty?label=2013-12-22T13%3A24%3A12.853Z%2Ftest.xml"}, 
+    {"name": "policy", "value": "eyJleHBpcmF0aW9uIjogIjIwMTMtMTItMjNUMDk6MjQ6MThaIiwKImNvbmRpdGlvbnMiOiBbeyJ4LWdvb2ctbWV0YS11cGxvYWRlZC1ieSI6ICIzYmFlNDAxMy1kMDNlLTQyN2ItYWMyZS05ZTVkMDg0ZDBlYTkifSx7ImJ1Y2tldCI6ICJja2FubmV0LXN0b3JhZ2UifSx7ImtleSI6ICIyMDEzLTEyLTIyVDEzOjI0OjEyLjg1M1ovdGVzdC54bWwifSx7ImFjbCI6ICJwdWJsaWMtcmVhZCJ9LHsic3VjY2Vzc19hY3Rpb25fcmVkaXJlY3QiOiAiaHR0cDovL2RhdGFodWIuaW8vc3RvcmFnZS91cGxvYWQvc3VjY2Vzc19lbXB0eT9sYWJlbD0yMDEzLTEyLTIyVDEzJTNBMjQlM0ExMi44NTNaJTJGdGVzdC54bWwifSxbImNvbnRlbnQtbGVuZ3RoLXJhbmdlIiwgMCwgMTAwMDAwMDAwMDBdXX0="},
+    {"name": "GoogleAccessId", "value": "GOOGC6OU3AYPNY47B66M"}, 
+    {"name": "signature", "value": "VFYngO/4ZQRYt4YXnRGXitiJyNE="}, 
+    {"name": "key", "value": "2013-12-22T13:24:12.853Z/test.xml"}]}
+		 */
+	}
+
+	@Test
+	public void testExecFileUpload() throws Exception {
+		UploadData uploadData = new UploadData();
+		File file = new File(TEST_XML); 
+		uploadData.upload(file);
 	}
 
 	@Test

src/test/resources/org/xmlcml/ckan/test.xml

+<project>
+  <test/>
+</project>