Skip to content

Commit

Permalink
Merge pull request #25 from opengeospatial/reorganised_main_branch
Browse files Browse the repository at this point in the history
Reorganised ConfClasses and packages in main branch
  • Loading branch information
arieslai committed Jan 13, 2023
2 parents 5c24b01 + e88a1de commit 8de1251
Show file tree
Hide file tree
Showing 10 changed files with 846 additions and 713 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
/target/
161 changes: 161 additions & 0 deletions src/main/java/org/opengis/cite/wps20/CommonFixture.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,29 @@
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientRequest;
import com.sun.jersey.api.client.ClientResponse;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.Map;
import javax.ws.rs.core.MediaType;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.*;

import org.opengis.cite.wps20.util.ClientUtils;
import org.testng.ITestContext;
import org.testng.SkipException;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;

/**
* A supporting base class that sets up a common test fixture. These
Expand Down Expand Up @@ -134,5 +148,152 @@ public ClientRequest buildGetRequest(URI endpoint,
Map<String, String> qryParams, MediaType... mediaTypes) {
return ClientUtils.buildGetRequest(endpoint, qryParams, mediaTypes);
}
/**
* Description: Send POST request with parameters and return Response as String
*
* @param any_url
* @param xml_doc
* @return
*/
public String GetContentFromPOSTXMLRequest(String any_url, Document xml_doc) {
StringBuilder sb = new StringBuilder();
HttpURLConnection urlConn = null;
InputStreamReader in = null;
try {
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tf.setOutputProperty(OutputKeys.INDENT, "yes");
Writer out = new StringWriter();
tf.transform(new DOMSource(xml_doc), new StreamResult(out));
byte[] postDataBytes = out.toString().getBytes("UTF-8");

URL url = new URL(any_url);
urlConn = (HttpURLConnection) url.openConnection();
urlConn.setRequestMethod("POST");
urlConn.setRequestProperty("Content-Type", "application/xml");
urlConn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
urlConn.setDoOutput(true);
urlConn.getOutputStream().write(postDataBytes);
if (urlConn != null)
urlConn.setReadTimeout(60 * 1000);
if (urlConn != null && urlConn.getInputStream() != null) {
in = new InputStreamReader(urlConn.getInputStream(), Charset.defaultCharset());
BufferedReader bufferedReader = new BufferedReader(in);
if (bufferedReader != null) {
int cp;
while ((cp = bufferedReader.read()) != -1) {
sb.append((char) cp);
}
bufferedReader.close();
}
}
in.close();
} catch (Exception e) {
throw new RuntimeException("Exception while calling URL:" + any_url, e);
}
return sb.toString();
}
/**
* @param xmlString
* @return
*/
public Document TransformXMLStringToXMLDocument(String xmlString) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = null;
try {
builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(xmlString)));
return doc;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* Description: Send GET request with parameters and return Response as String
*
* @param any_url
* @param params
* @return
*/
public String GetContentFromGETKVPRequest(String any_url, Map<String, Object> params) {
StringBuilder sb = new StringBuilder();
HttpURLConnection urlConn = null;
InputStreamReader in = null;
try {
StringBuilder Data = new StringBuilder();
for (Map.Entry<String, Object> param : params.entrySet()) {
if (Data.length() != 0)
Data.append('&');
Data.append(URLEncoder.encode(param.getKey(), "UTF-8"));
Data.append('=');
Data.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}

URL url = new URL(any_url + "?" + Data.toString());
urlConn = (HttpURLConnection) url.openConnection();
urlConn.setRequestMethod("GET");
urlConn.setDoOutput(true);
if (urlConn != null)
urlConn.setReadTimeout(60 * 1000);
if (urlConn != null && urlConn.getInputStream() != null) {
in = new InputStreamReader(urlConn.getInputStream(), Charset.defaultCharset());
BufferedReader bufferedReader = new BufferedReader(in);
if (bufferedReader != null) {
int cp;
while ((cp = bufferedReader.read()) != -1) {
sb.append((char) cp);
}
bufferedReader.close();
}
}
in.close();
} catch (Exception e) {
throw new RuntimeException("Exception while calling URL:" + any_url, e);
}
return sb.toString();
}
/**
* @param URI
* @return
*/
public Document TransformXMLFileToXMLDocument(String URI) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = null;
try {
builder = factory.newDocumentBuilder();
Document doc = builder.parse(new File(URI));
return doc;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* @param xmlDoc
* @throws Exception
*/
public String TransformXMLDocumentToXMLString(Document xmlDoc) throws Exception {
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tf.setOutputProperty(OutputKeys.INDENT, "yes");
Writer out = new StringWriter();
tf.transform(new DOMSource(xmlDoc), new StreamResult(out));
return out.toString();
}

/**
* @param xmlDoc
* @throws Exception
*/
public void prettyPrint(Document xmlDoc) throws Exception {
String str = TransformXMLDocumentToXMLString(xmlDoc);
System.out.println(str);
}
public HttpURLConnection GetConnection(String serviceURL) throws IOException {
URL urlObj = new URL(serviceURL);
return (HttpURLConnection) urlObj.openConnection();
}
}

0 comments on commit 8de1251

Please sign in to comment.