Required Info
In order for JEMH to process a mail, it needs to use a Profile. Until now Profiles are tied to mailboxes, allowing arbitrary external use of those profiles may trigger unexpected scenarios, therefore:
Profiles will need to explicitly opt-in to API use
Lists of profiles via REST will only show those that have opted-in.
All REST API's require an authenticated user
API
URLS must follow this pattern
create POST /urlOfRestResource
read GET /urlOfRestResource[/id]
update PUT /urlOfRestResource/id
delete DELETE /urlOfRestResource/id
URL | Method | Response | ||||||||||
.../rest/jemh/latest/public/profile/list | GET | List of JSON profile objects that identify Profile NAME and ID. Requires authenticated user (base 64 headers?) Request:
Response:
| ||||||||||
/rest/jemh/latest/public/mailbox/deliver | POST | Request Using an email like:
A CURL command would be of format (NOTE: initially application/x-www-form-urlencoded was supported, since 1.6.15, multipart/form-data is also supported):
URLEncoding the content (shown above) eg through services like http://www.url-encode-decode.com/ and replacing .... with the result:
RESPONSE format:
Example Response:
|
Worked JAVA Examples
Example Test Case
Code Block | ||
---|---|---|
| ||
package com.javahollic.jira.emh.publicapi; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import org.apache.commons.codec.binary.Base64; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.io.IOUtils; import org.junit.Assert; import org.junit.Test; public class TestPost { @Test public void testPost() throws IOException { InputStream is = getClass().getClassLoader().getResourceAsStream("plaintextEmail.txt"); Assert.assertNotNull(is); String emailStr=IOUtils.toString(is,"UTF-8"); String name = "admin"; String password = "admin"; String authString = name + ":" + password; System.out.println("auth string: " + authString); byte[] authEncBytes = Base64.encodeBase64(authString.getBytes()); String authStringEnc = new String(authEncBytes); System.out.println("Base64 encoded auth string: " + authStringEnc); HttpClient client = new HttpClient(); client.getParams().setParameter("http.useragent", "Test Client"); BufferedReader br = null; PostMethod method = new PostMethod("http://dev-jira:8080/rest/jemh/latest/public/mailbox/deliver"); method.addParameter("profileId", "1"); method.addParameter("email", emailStr); method.addRequestHeader("Authorization", "Basic " + authStringEnc); try { int returnCode = client.executeMethod(method); if (returnCode == HttpStatus.SC_NOT_IMPLEMENTED) { System.err.println("The Post method is not implemented by this URI"); // still consume the response body System.err.println("\n"+method.getResponseBodyAsString()); } else { String response=IOUtils.toString(method.getResponseBodyAsStream()); System.out.println("\n-------------------------\n"+response+"\n-------------------------\n"); } } catch (Exception e) { System.err.println(e); } finally { method.releaseConnection(); if (br != null) try { br.close(); } catch (Exception fe) { } } } @Test public void testGet() { try { String webPage = "http://dev-jira:8080/rest/jemh/latest/public/profile/list"; String name = "admin"; String password = "admin"; String authString = name + ":" + password; System.out.println("auth string: " + authString); byte[] authEncBytes = Base64.encodeBase64(authString.getBytes()); String authStringEnc = new String(authEncBytes); System.out.println("Base64 encoded auth string: " + authStringEnc); URL url = new URL(webPage); URLConnection urlConnection = url.openConnection(); urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc); InputStream is = urlConnection.getInputStream(); InputStreamReader isr = new InputStreamReader(is); int numCharsRead; char[] charArray = new char[1024]; StringBuffer sb = new StringBuffer(); while ((numCharsRead = isr.read(charArray)) > 0) { sb.append(charArray, 0, numCharsRead); } String result = sb.toString(); System.out.println("*** BEGIN ***"); System.out.println(result); System.out.println("*** END ***"); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } |
Related articles
Filter by label (Content by label) | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Page Properties | ||
---|---|---|
| ||
|