java generic HTTP & send token request

Signed-off-by: Luca Fulchir <luca@fulchir.it>
parent 5ecc5cae
JAVAC=javac
BUILDDIR=../build/
CWD=${PWD}
BUILDDIR=${CWD}/../build/
JARDIR=${CWD}/../lib
JOLIEDIR=/opt/jolie/
PACKAGE_NAME="utils.jar"
LIBRARY="httpcore-4.2.2.jar httpclient-4.2.3.jar commons-io-2.4.jar"
java:
- mkdir -p ${BUILDDIR} 2>/dev/null
${JAVAC} -cp ${JOLIEDIR}/jolie.jar -d ${BUILDDIR} jolie/net/utils/URLEncoder.java
jar cvf ${BUILDDIR}/utils.jar ${BUILDDIR}/jolie/net/utils/*class
${JAVAC} -cp ${JOLIEDIR}/jolie.jar:${JARDIR}/httpcore-4.2.2.jar:${JARDIR}/httpclient-4.2.3.jar:${JARDIR}/commons-io-2.4.jar -d ${BUILDDIR} jolie/net/utils/genericHTTP.java
cd ${BUILDDIR} && jar cvf ${PACKAGE_NAME} ./jolie/net/utils/*class && cd ${CWD}
clean:
- rm -rf ${BUILDDIR}
demo1:
javaServices: java
cp ${BUILDDIR}/utils.jar ${JOLIEDIR}/javaServices/
demo1: javaServices
jolie jolie/net/OAuth/demo.ol
include "OAuth1.iol"
include "console.iol"
interface OAuth1Interface_http {
RequestResponse:
RequestToken_http (OAuth1TokenReq) (string)
}
outputPort OAuth1_http {
Interfaces: OAuth1Interface_http
}
include "jolie/net/utils/genericHTTP.iol"
inputPort OAuth1 {
Location: "local"
......@@ -17,13 +9,16 @@ inputPort OAuth1 {
main {
RequestToken (where) (answer) {
OAuth1_http.location = where.location;
proto = "http";
proto.method = where.method;
OAuth1_http.protocol = proto;
DataRequestToken@OAuth1Data (where) (post_data);
RequestToken_http@OAuth1_http(post_data) (answer)
http_data.URI = where.location;
http_data.method = where.method;
http_data.headers[0].name = "userAgent";
http_data.headers[0].value= "Jolie-OAuth";
http_data.headers[1].name = "Authorization";
DataRequestToken@OAuth1Data (where) (http_data.headers[1].value);
sendRequest@genericHTTP(http_data) (answer)
}
}
......
......@@ -7,7 +7,8 @@ main
{
// data needed for the connection: where to connect, how and who we are
data.location = "socket://api.dropbox.com:80/1/oauth/request_token";
//data.location = "http://api.dropbox.com:80/1/oauth/request_token";
data.location = "http://codepedia.eu:80/oauth";
data.method = "post";
data.callback = "";
createSecureToken@SecurityUtils(void)(data.consumer_key);
......
......@@ -14,10 +14,13 @@ type OAuth1TokenReq:void {
.oauth_nonce :string
.oauth_signature :string
}
type OAuthTkn:void {
.Authorization :string
}
interface OAuth1DataInterface {
RequestResponse:
DataRequestToken (OAuth1Locations) (OAuth1TokenReq)
DataRequestToken (OAuth1Locations) (string)
}
outputPort OAuth1Data {
Interfaces: OAuth1DataInterface
......
......@@ -9,28 +9,22 @@ inputPort OAuth1Data{
}
main {
DataRequestToken (where) (answ) {
// optional. we would need a web server.
answ.oauth_callback = where.callback;
answ.oauth_version = "1.0";
answ.oauth_consumer_key = where.consumer_key;
answ.oauth_signature_method="HMAC-SHA1";
DataRequestToken (where) (postString) {
// getCurrentTimeMillis is a temporary interface, but there's
// nothing better :(
getCurrentTimeMillis@Time(void)(tmp_timestamp);
valueToPrettyString@StringUtils(tmp_timestamp / 1000) (answ.oauth_timestamp);
tmp_timestamp = tmp_timestamp / 1000;
// a little too much as a nonce, but it's easier this way :p
createSecureToken@SecurityUtils(void)(answ.oauth_nonce);
// we are requesting a token, there is no secret, the signature
// has no meaning here.
answ.oauth_signature=""
createSecureToken@SecurityUtils(void)(oauth_nonce);
postString = "oauth_consumer_key=\"" + where.consumer_key + "\"," +
"oauth_signature_method=\"HMAC-SHA1\"," +
"oath_timestamp=\"" + tmp_timestamp + "\"," +
"oauth_nonce=\"" + oauth_nonce + "\"," +
"oauth_callback=\"" + where.callback + "\"," +
"oauth_version=\"1.0\"," +
"oauth_signature=\"\""
}
}
......
......@@ -20,6 +20,7 @@
***************************************************************************/
include "console.iol"
include "security_utils.iol"
include "jolie/net/utils/genericHTTP.iol"
include "jolie/net/utils/URLEncoder.iol"
main
......@@ -27,13 +28,22 @@ main
createSecureToken@SecurityUtils(void)(uff);
println@Console( "uuid: " + uff )();
input = URLEncoderRequest;
input.input = "http://printer.example.com/ready";
input.encoding = "UTF-8";
encode@URLEncoder(input)(output);
println@Console( "Answer is " + output )()
println@Console( "Answer is " + output )();
http.URI = "http://codepedia.eu/test";
http.method = "POST";
http.headers[0].name="Auth";
http.headers[0].value="authvalue";
http.parameters[0].name="lol";
http.parameters[0].value="lolvalue 42/asdf";
sendRequest@genericHTTP (http) (answer);
println@Console( "Post is " + answer )()
}
/***************************************************************************
* Copyright (C) 2013 by Luca Fulchir <luca@fulchir.it> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Library General Public License as *
* published by the Free Software Foundation; either version 2 of the *
* License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
* *
* For details about the authors of this software, see the AUTHORS file. *
***************************************************************************/
type HTTPpair: void {
.name :string
.value :string
}
type HTTPRequest:void {
.URI :string
.method :string
.userAgent? :string
.parameters* :HTTPpair
.cookies? :string
.headers* :HTTPpair
}
type HTTPResponse:void {
.output :string
}
interface genericHTTPInterface {
RequestResponse:
sendRequest (HTTPRequest) (string)
}
outputPort genericHTTP {
Interfaces:
genericHTTPInterface
}
embedded {
Java:
"jolie.net.utils.genericHTTP" in genericHTTP
}
/* *************************************************************************
* Copyright (C) 2013 by Luca Fulchir <luca@fulchir.it> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Library General Public License as *
* published by the Free Software Foundation; either version 2 of the *
* License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
* *
* For details about the authors of this software, see the AUTHORS file. *
***************************************************************************/
package jolie.net.utils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.commons.io.IOUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import java.util.ArrayList;
import java.util.List;
import jolie.runtime.JavaService;
import jolie.runtime.Value;
import jolie.runtime.ValueVector;
import jolie.runtime.embedding.RequestResponse;
import jolie.runtime.FaultException;
public class genericHTTP extends JavaService
{
@RequestResponse
public static String sendRequest (Value req) throws FaultException {
HttpClient httpclient = new DefaultHttpClient();
if (req.getFirstChild("method").strValue().toLowerCase().equals("post")) {
return sendPost(httpclient, req);
} else {
return sendGet(httpclient, req);
}
}
private static String sendGet (HttpClient httpclient, Value req)
throws FaultException
{
return new String("Not implemented");
}
private static String sendPost (HttpClient httpclient, Value req)
throws FaultException
{
HttpPost httppost = new HttpPost(req.getFirstChild("URI").strValue());
if (req.hasChildren("headers")) {
for (Value entry : req.getChildren("headers")) {
// add every header
httppost.addHeader(
entry.getFirstChild("name").strValue(),
entry.getFirstChild("value").strValue());
}
}
try {
if (req.hasChildren("parameters")) {
for (Value pair : req.getChildren("parameters")) {
List<NameValuePair> postParams =
new ArrayList<NameValuePair>();
BasicNameValuePair tmp = new BasicNameValuePair(
pair.getFirstChild("name").strValue(),
pair.getFirstChild("value").strValue());
postParams.add(tmp);
UrlEncodedFormEntity entity =
new UrlEncodedFormEntity(postParams);
entity.setContentEncoding("UTF_8");
entity.setContentType("application/json");
httppost.setEntity(entity);
}
}
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
return IOUtils.toString(entity.getContent(), "UTF-8");
} catch (Exception e) {
throw new FaultException(e.getMessage());
}
}
}
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