Skip to content

Commit

Permalink
added methods getDocumentCollection and getDocument
Browse files Browse the repository at this point in the history
issue #289
  • Loading branch information
rsoika committed Aug 18, 2017
1 parent a9da999 commit 4e59b50
Showing 1 changed file with 57 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,30 @@
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.CookieManager;
import java.net.HttpCookie;
import java.net.HttpURLConnection;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.ws.rs.core.MediaType;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import org.imixs.workflow.ItemCollection;
import org.imixs.workflow.util.Base64;
import org.imixs.workflow.xml.DocumentCollection;
import org.imixs.workflow.xml.XMLItemCollection;
import org.imixs.workflow.xml.XMLItemCollectionAdapter;

/**
* This ServiceClient is a WebService REST Client which encapsulate the
Expand Down Expand Up @@ -461,8 +467,7 @@ public void setCookies(CookieManager cookieManager) {
*/
public int get(String uri) throws Exception {
URL obj = new URL(uri);
HttpURLConnection urlConnection = (HttpURLConnection) obj
.openConnection();
HttpURLConnection urlConnection = (HttpURLConnection) obj.openConnection();

// optional default is GET
urlConnection.setRequestMethod("GET");
Expand All @@ -471,17 +476,15 @@ public int get(String uri) throws Exception {
urlConnection.setDoInput(true);
urlConnection.setAllowUserInteraction(false);

if (requestProperties!=null) {
if (requestProperties != null) {
for (Map.Entry<String, String> entry : requestProperties.entrySet()) {
urlConnection.setRequestProperty(entry.getKey() , entry.getValue());
urlConnection.setRequestProperty(entry.getKey(), entry.getValue());
}
}



// Authorization
if (user != null) {
urlConnection.setRequestProperty("Authorization",
"Basic " + this.getAccessByUser());
urlConnection.setRequestProperty("Authorization", "Basic " + this.getAccessByUser());
}

addCookies(urlConnection);
Expand All @@ -495,6 +498,52 @@ public int get(String uri) throws Exception {
return responseCode;
}

/**
* Returns a list of ItemCollections from a XML data source
*
* @return
* @throws Exception
*/
public List<ItemCollection> getDocumentCollection(String url) throws Exception {
this.setRequestProperty("Accept", MediaType.APPLICATION_XML);
this.get(url);
String xmlResult = this.getContent();

// convert into ItemCollection list
JAXBContext context = JAXBContext.newInstance(DocumentCollection.class);
Unmarshaller u = context.createUnmarshaller();
StringReader reader = new StringReader(xmlResult);
DocumentCollection xmlDocuments = (DocumentCollection) u.unmarshal(reader);

List<ItemCollection> documents = XMLItemCollectionAdapter.getCollection(xmlDocuments);
return documents;

}

/**
* Returns a list of ItemCollections from a XML data source
*
* @return
* @throws Exception
*/
public ItemCollection getDocument(String url) throws Exception {
this.setRequestProperty("Accept", MediaType.APPLICATION_XML);
this.get(url);
String xmlResult = this.getContent();

// convert into ItemCollection list
JAXBContext context = JAXBContext.newInstance(XMLItemCollection.class);
//JAXBContext context = JAXBContext.newInstance( "org.imixs.workflow.xml" );

Unmarshaller u = context.createUnmarshaller();
StringReader reader = new StringReader(xmlResult);
XMLItemCollection xmlDocument = (XMLItemCollection) u.unmarshal(reader);

ItemCollection document = XMLItemCollectionAdapter.getItemCollection(xmlDocument);
return document;

}

public void readCookies(HttpURLConnection connection) throws URISyntaxException {
String COOKIES_HEADER = "Set-Cookie";
cookieManager = new java.net.CookieManager();
Expand Down

0 comments on commit 4e59b50

Please sign in to comment.