Commits

Anonymous committed b7c16b8

minor additions to Connection

Comments (0)

Files changed (5)

datastoretest.php

 curl_close ($ch);
 print_r($auth_ticket);
 
-// should get back {"action": "/storage/upload_handle", "fields": [{"name": "key", "value": "2013-11-05T05:22:49.268Z/datastoretest.php"}]}
+// should get back {"action": "/storage/upload_handle", "fields": 
+//         [{"name": "key", "value": "2013-11-05T05:22:49.268Z/datastoretest.php"}]}
 // if file url already exists, you get an error 409
 
 // upload file via POST request
 			<version>4.3</version>
 		</dependency>
 		<dependency>
+			<groupId>org.xml-cml</groupId>
+			<artifactId>euclid-dev</artifactId>
+			<version>1.1-SNAPSHOT</version>
+		</dependency>
+
+		<dependency>
 			<groupId>junit</groupId>
 			<artifactId>junit</artifactId>
 			<version>4.11</version>

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

 import org.apache.http.HttpResponse;
 import org.apache.http.client.ClientProtocolException;
 import org.apache.http.client.HttpClient;
-import org.apache.http.client.config.RequestConfig;
 import org.apache.http.client.methods.HttpPost;
-import org.apache.http.conn.params.ConnRoutePNames;
 import org.apache.http.entity.StringEntity;
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.impl.client.HttpClients;
 import org.apache.log4j.Logger;
 
 /**
     			port = Integer.parseInt(System.getProperty("http.proxyPort"));
     		}
     		HttpHost proxy = new HttpHost(System.getProperty("http.proxyHost"), port, "http");
-    		httpClient = HttpClientBuilder.create().setProxy(proxy).build(); 
-//    		RequestConfig requestConfig = RequestConfig.DEFAULT;
-//    		requestConfig.
-    		httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
+    		// old code getParams() is @Deprecated
+//    		httpClient = HttpClientBuilder.create().setProxy(proxy).build(); 
+//    		httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
+    		// try new code
+    		httpClient = HttpClients.custom()
+    			    .useSystemProperties()
+    			    .setProxy(proxy)
+    			    .build();
     	}
 		return httpClient;
 	}

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

 	private String timestamp;
 	private String message;
 	private String author;
-	//private String approved_timestamp;
 	
 	public String getID() {
 		return id;

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

 package org.xmlcml.ckan;
 
+import java.util.Date;
+
+import org.junit.Test;
+
 public class UploadDataTest {
 
+	@Test
+	public void PHPExample() {
+	/**
+
+
+// make a unique url for this file as per 
+ * https://github.com/okfn/ckan/blob/118c2b62988956b5bc80d42a8a3e12964b3f9543/ckan/public/scripts/application.js#L790
+$upload_key = date('c') // use ISO date format
+. "/"
+. str_replace(" ", "-", basename($file_path)); // replace spaces with dashes in from file name
+
+// check unique url is actually unique before uploading this file using filestore auth api
+$ch = curl_init();
+curl_setopt($ch, CURLOPT_URL,$ckan_url."/api/storage/auth/form/".$upload_key);
+curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization:'.$api_key
+                                          ,'X-CKAN-API-Key:'.$api_key));
+curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+$auth_ticket = json_decode(curl_exec ($ch),true);
+curl_close ($ch);
+print_r($auth_ticket);
+
+// should get back {"action": "/storage/upload_handle", "fields": [{"name": "key", "value": "2013-11-05T05:22:49.268Z/datastoretest.php"}]}
+// if file url already exists, you get an error 409
+
+// upload file via POST request
+$post = array('key' => $auth_ticket['fields'][0]['value'], // use echoed back url to ensure we fail if the url was not unique
+            'file'=> curl_file_create($file_path,'',basename($file_path)));
+            print_r($post);
+$ch = curl_init();
+curl_setopt($ch, CURLOPT_URL,$ckan_url."/storage/upload_handle"); // note, no trailing slash, /api/ or upload filename in this URL!
+curl_setopt($ch, CURLOPT_POST,1);
+curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:' // Avoid PHP CURL file upload causing 417 - expectation failed errors http://www.php.net/manual/en/function.curl-setopt.php#82418
+                                          ,'Authorization:'.$api_key
+                                          ,'X-CKAN-API-Key:'.$api_key));
+curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
+if( ! $result = curl_exec($ch))
+    {
+        trigger_error(curl_error($ch));
+    }
+curl_close($ch);
+echo print_r(preg_grep('/storage\/f/',explode('<p>',$result))); // upload result is a whole HTML page, grep out the returned URL as a sanity test
+
+print_r(json_decode(file_get_contents($ckan_url.'/api/storage/metadata/'.$upload_key))); // json metadata checks that the file uploaded and gives final URL to download file!
+
+	 */
+	}
+	
+	@Test
+	public void pythonExample() {
+		/**
+		def upload_file (self, file_path):
+	        '''Upload a file to a CKAN instance via CKAN's FileStore API.
+
+	The CKAN instance must have file storage enabled.
+
+	A timestamped directory is created on the server to store the file as
+	if it had been uploaded via the graphical interface. On success, the
+	URL of the file is returned along with an empty error message. On
+	failure, the URL is an empty string.
+
+	:param file_path: path to the file to upload, on the local filesystem
+	:type file_path: string
+
+	:returns: a (url, errmsg) 2-tuple containing the URL of the
+	successufully uploaded file on the CKAN server (string, an empty
+	string if the upload failed) and any error message from the server
+	(string, an empty string if there was no error)
+	:rtype: (string, string) 2-tuple
+
+	'''
+	        # see ckan/public/application.js:makeUploadKey for why the file_key
+	        # is derived this way.
+	        ts = datetime.isoformat(datetime.now()).replace(':','').split('.')[0]
+	        */
+		String ts = new Date().toString();
+		/**
+
+	        norm_name = os.path.basename(file_path).replace(' ', '-')
+	        file_key = os.path.join(ts, norm_name)
+
+	        auth_dict = self.storage_auth_get('/form/'+file_key, {})
+
+	        fields = [(kv['name'].encode('ascii'), kv['value'].encode('ascii'))
+	                  for kv in auth_dict['fields']]
+	        files = [('file', file_path, open(file_path, 'rb').read())]
+	 
+	        errcode, body = self._post_multipart(auth_dict['action'].encode('ascii'), fields, files)
+
+	        if errcode == 200:
+	            file_metadata = self.storage_metadata_get(file_key)
+	            return file_metadata['_location'], ''
+	        elif self.last_status == 404:
+	            raise CkanApiNotFoundError(body)
+	            return '', body
+	        elif self.last_status == 403:
+	            raise CkanApiNotAuthorizedError(body)
+	            return '', body
+	        elif self.last_status == 409:
+	            raise CkanApiConflictError(body)
+	            return '', body
+	        else:
+	            raise CkanApiError(body)
+	            return '', body	}
+	            */
+	}
 }