build referral OK

Signed-off-by: Luca Fulchir <luca@fulchir.it>
parent b53d148d
......@@ -8,6 +8,8 @@ inputPort OAuth1 {
Interfaces: OAuth1Interface
}
execution { concurrent}
main {
[getReferral (info) (output) {
......@@ -20,7 +22,7 @@ main {
http_data.query[1].value = "";
http_data.method = "GET";
sendRequest@genericHTTP (http_data) (output)
buildURI@genericHTTP (http_data) (output)
}] {nullProcess}
......
......@@ -11,6 +11,8 @@ inputPort OAuth1Data{
Interfaces: OAuth1DataInterface
}
execution {concurrent}
main {
[DataRequestToken (where) (postString)
{
......
......@@ -39,7 +39,8 @@ type HTTPResponse:void {
interface genericHTTPInterface {
RequestResponse:
sendRequest (HTTPRequest) (string)
sendRequest (HTTPRequest) (string) throws send_error,
buildURI (HTTPRequest) (string) throws buildURI_fault
}
outputPort genericHTTP {
......
......@@ -44,19 +44,20 @@ public class genericHTTP extends JavaService
{
@RequestResponse
public static String sendRequest (Value req) throws FaultException {
public static String sendRequest (Value req) throws FaultException
{
HttpClient httpclient = new DefaultHttpClient();
if (req.getFirstChild("method").strValue().toLowerCase().equals("post")) {
if (req.getFirstChild("method").strValue().toUpperCase()
.equals("POST")) {
return sendPost(httpclient, req);
} else {
return sendGet(httpclient, req);
}
}
private static String sendGet (HttpClient httpclient, Value req)
throws FaultException
@RequestResponse
public static String buildURI (Value req) throws FaultException
{
boolean notFirst = false;
String query = new String("");
try {
......@@ -75,14 +76,23 @@ private static String sendGet (HttpClient httpclient, Value req)
notFirst = true;
}
}
HttpGet httpget;
if (query.isEmpty()) {
httpget = new HttpGet(req.getFirstChild("URI")
.strValue());
return new String (req.getFirstChild("URI").strValue());
} else {
httpget = new HttpGet(req.getFirstChild("URI")
.strValue() + "?" + query);
return new String (req.getFirstChild("URI").strValue()
+ "?" + query);
}
} catch (Exception e) {
throw new FaultException("buildURI_fault", e);
}
}
private static String sendGet (HttpClient httpclient, Value req)
throws FaultException
{
try {
HttpGet httpget = new HttpGet(buildURI(req));
if (req.hasChildren("headers")) {
for (Value entry : req.getChildren("headers")) {
// add every header
......@@ -95,8 +105,10 @@ private static String sendGet (HttpClient httpclient, Value req)
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
return IOUtils.toString(entity.getContent(), "UTF-8");
} catch (FaultException f) {
throw f;
} catch (Exception e) {
throw new FaultException(e.getMessage());
throw new FaultException("send_error", e);
}
}
......@@ -106,29 +118,7 @@ private static String sendPost (HttpClient httpclient, Value req)
boolean notFirst = false;
String query = new String("");
try {
URLCodec codec = new URLCodec("UTF8");
if (req.hasChildren("query")) {
for (Value pair : req.getChildren("query")) {
String name = codec.encode(
pair.getFirstChild("name").strValue(), "UTF8");
String value = codec.encode(
pair.getFirstChild("value").strValue(),
"UTF8");
if (notFirst) {
query += "&";
}
query += name + "=" + value;
notFirst = true;
}
}
HttpPost httppost;
if (query.isEmpty()) {
httppost = new HttpPost(req.getFirstChild("URI")
.strValue());
} else {
httppost = new HttpPost(req.getFirstChild("URI")
.strValue() + "?" + query);
}
HttpPost httppost = new HttpPost(buildURI(req));
if (req.hasChildren("headers")) {
for (Value entry : req.getChildren("headers")) {
// add every header
......@@ -158,8 +148,10 @@ private static String sendPost (HttpClient httpclient, Value req)
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
return IOUtils.toString(entity.getContent(), "UTF-8");
} catch (FaultException f) {
throw f;
} catch (Exception e) {
throw new FaultException(e.getMessage());
throw new FaultException("send_error", e);
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment