2016-11-25 15:42:29 +01:00
|
|
|
package app;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2022-03-29 15:05:28 +02:00
|
|
|
import java.util.Collections;
|
2022-03-21 11:17:07 +01:00
|
|
|
import java.util.HashMap;
|
2016-11-25 15:42:29 +01:00
|
|
|
import java.util.List;
|
2022-03-21 11:17:07 +01:00
|
|
|
import java.util.Map;
|
2016-11-25 15:42:29 +01:00
|
|
|
import java.util.Map.Entry;
|
|
|
|
import java.util.concurrent.CountDownLatch;
|
2022-03-21 11:17:07 +01:00
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
|
|
import javax.ws.rs.HttpMethod;
|
2022-03-29 15:05:28 +02:00
|
|
|
import javax.ws.rs.WebApplicationException;
|
2022-03-21 11:17:07 +01:00
|
|
|
import javax.ws.rs.client.Client;
|
|
|
|
import javax.ws.rs.client.ClientBuilder;
|
|
|
|
import javax.ws.rs.client.Invocation.Builder;
|
2016-11-25 15:42:29 +01:00
|
|
|
|
|
|
|
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
|
|
|
|
import org.gcube.common.authorization.library.provider.UserInfo;
|
|
|
|
import org.gcube.common.authorization.library.utils.Caller;
|
2022-03-29 15:05:28 +02:00
|
|
|
import org.gcube.smartgears.extensions.HttpExtension.Method;
|
2022-03-21 11:17:07 +01:00
|
|
|
import org.glassfish.jersey.client.ClientConfig;
|
|
|
|
import org.glassfish.jersey.client.ClientResponse;
|
|
|
|
import org.glassfish.jersey.logging.LoggingFeature;
|
2016-11-25 15:42:29 +01:00
|
|
|
|
|
|
|
import utils.TestUtils;
|
|
|
|
|
|
|
|
public class Request {
|
|
|
|
|
|
|
|
private String path="";
|
|
|
|
private String scope = TestUtils.scope;
|
|
|
|
|
2022-03-21 11:17:07 +01:00
|
|
|
//private OutBoundHeaders headers = new OutBoundHeaders();
|
|
|
|
private String method = HttpMethod.GET;
|
2016-11-25 15:42:29 +01:00
|
|
|
private String body = null;
|
|
|
|
private boolean logged = false;
|
2022-03-21 11:17:07 +01:00
|
|
|
Map<String, List<Object>> headers = new HashMap<>();
|
2016-11-25 15:42:29 +01:00
|
|
|
|
|
|
|
public static Request request() {
|
|
|
|
return new Request();
|
|
|
|
}
|
|
|
|
|
|
|
|
private Request() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public Request at(String path) {
|
|
|
|
this.path=path;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Request logging() {
|
|
|
|
logged=true;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Request inScope(String scope) {
|
|
|
|
this.scope=scope;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Request with(String body) {
|
|
|
|
this.body=body;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Request with(String name, String value) {
|
2022-03-29 15:05:28 +02:00
|
|
|
this.headers.put(name, Collections.singletonList(value));
|
2016-11-25 15:42:29 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2022-03-29 15:05:28 +02:00
|
|
|
public Request using(Method method) {
|
|
|
|
this.method=method.getValue();
|
2016-11-25 15:42:29 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String path() {
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String body() {
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
|
2022-03-21 11:17:07 +01:00
|
|
|
public String method() {
|
2016-11-25 15:42:29 +01:00
|
|
|
return method;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String scope() {
|
|
|
|
return scope;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClientResponse make(final int port) {
|
|
|
|
|
|
|
|
|
|
|
|
// we make a scoped call in a separate thread, with which we then synchronize for completion.
|
|
|
|
// this helps isolate the caller's thread (Main normally) from the app's thread,
|
|
|
|
// starting with the scope itself.
|
|
|
|
final CountDownLatch latch = new CountDownLatch(1);
|
|
|
|
|
|
|
|
class Box {
|
|
|
|
|
2022-03-29 15:05:28 +02:00
|
|
|
volatile WebApplicationException failure;
|
2016-11-25 15:42:29 +01:00
|
|
|
volatile ClientResponse response;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
final Box box = new Box();
|
|
|
|
|
|
|
|
new Thread() {
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
|
|
|
|
AuthorizationProvider.instance.set(new Caller(new UserInfo("test", new ArrayList<String>()),"DEFAULT"));
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
2022-03-21 11:17:07 +01:00
|
|
|
ClientConfig config = new ClientConfig();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Client client = ClientBuilder.newClient();
|
|
|
|
|
|
|
|
|
2016-11-25 15:42:29 +01:00
|
|
|
if (logged)
|
2022-03-21 11:17:07 +01:00
|
|
|
client.register(new LoggingFeature(Logger.getLogger(getClass().getName())));
|
|
|
|
|
|
|
|
Builder builder = client.target(address(path,port)).request();
|
|
|
|
builder.header("gcube-scope", scope);
|
2016-11-25 15:42:29 +01:00
|
|
|
|
|
|
|
|
2022-03-21 11:17:07 +01:00
|
|
|
|
2016-11-25 15:42:29 +01:00
|
|
|
for (Entry<String,List<Object>> header : headers.entrySet())
|
|
|
|
for (Object value : header.getValue())
|
|
|
|
builder.header(header.getKey(), value);
|
|
|
|
|
2022-03-21 11:17:07 +01:00
|
|
|
if (method.equals(HttpMethod.DELETE))
|
2016-11-25 15:42:29 +01:00
|
|
|
builder.delete();
|
|
|
|
else {
|
|
|
|
|
|
|
|
System.err.println("making request @ "+address(path,port));
|
|
|
|
|
2022-03-21 11:17:07 +01:00
|
|
|
ClientResponse response = builder.method(method,ClientResponse.class);
|
2016-11-25 15:42:29 +01:00
|
|
|
|
|
|
|
//throws an exception if there response has error status
|
|
|
|
if (response.getStatus()>300)
|
2022-03-29 15:05:28 +02:00
|
|
|
throw new WebApplicationException(response.getStatus());
|
2016-11-25 15:42:29 +01:00
|
|
|
|
|
|
|
box.response=response;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-29 15:05:28 +02:00
|
|
|
} catch (WebApplicationException t) {
|
2016-11-25 15:42:29 +01:00
|
|
|
box.failure=t;
|
|
|
|
}
|
|
|
|
|
|
|
|
latch.countDown();
|
|
|
|
};
|
|
|
|
}.start();
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
2022-03-21 11:17:07 +01:00
|
|
|
if (!latch.await(2000, TimeUnit.MILLISECONDS))
|
2016-11-25 15:42:29 +01:00
|
|
|
throw new RuntimeException("application has not responded in time");
|
|
|
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (box.failure !=null)
|
|
|
|
throw box.failure;
|
|
|
|
|
|
|
|
else
|
|
|
|
return box.response;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private String address(String path, long port) {
|
|
|
|
|
|
|
|
path = (path.isEmpty() || path.startsWith("/"))?path:"/"+path;
|
|
|
|
|
2022-03-29 15:05:28 +02:00
|
|
|
return "http://localhost:" + port+ "/" + TestUtils.context_root+path;
|
2016-11-25 15:42:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|