I am building GAE standard endpoint with Java using Maven dependency:
<dependency>
<groupId>com.google.endpoints</groupId>
<artifactId>endpoints-framework</artifactId>
<version>${endpoints.framework.version}</version>
</dependency>
where
<endpoints.framework.version>2.0.9</endpoints.framework.version>
Annotating API with:
#Api( name = "blah",
version = "v1",
apiKeyRequired = AnnotationBoolean.TRUE,
#ApiMethod( name = "taxdocument.store",
path = "taxdoc/store",
httpMethod = HttpMethod.POST )
But API is not enforcing API keys.
Perhaps I am not understanding. I am expecting that POST to this would work.
https://blah.appspot.com/_ah/api/blah/v1/taxdoc/store?key=valid-key
But this would fail
https://blah.appspot.com/_ah/api/blah/v1/taxdoc/store?key=invalid-key
https://blah.appspot.com/_ah/api/blah/v1/taxdoc/store
But ALL succeed with no errors.
Can anyone steer me in the right direction? Thanks.
You need to include the API management library and filters.
Related
I have written simple REST web service client class which uses the JAX-RS 2.0 client API to make REST requests. I am trying to figure out how to set a request timeout for each invocation. Here is the code for a request:
Client client = ClientBuilder.newBuilder().build();
WebTarget resourceTarget = client.target(restServiceUrl)
.path("{regsysID}/{appointmentID}/")
.resolveTemplate("regsysID", regSysId)
.resolveTemplate("appointmentID", apptId);
Invocation invocation = resourceTarget.request(MediaType.APPLICATION_JSON).buildPut(null);
String createSessionJson = invocation.invoke(String.class);
Note: this is a new method available on JAX-RS 2.1
This is a very old post but the below code will work for both jersey and resteasy.
ClientBuilder clientBuilder = ClientBuilder.newBuilder();
clientBuilder.connectTimeout(10, TimeUnit.SECONDS);
clientBuilder.readTimeout(12, TimeUnit.SECONDS);
Client client = clientBuilder.build();
You can do this by creating a ClientConfig first and providing it as an argument when creating the new client.
import org.glassfish.jersey.client.ClientProperties;
ClientConfig configuration = new ClientConfig();
configuration.property(ClientProperties.CONNECT_TIMEOUT, 1000);
configuration.property(ClientProperties.READ_TIMEOUT, 1000);
Client client = ClientBuilder.newClient(configuration);
With Resteasy this can be accomplished by building your Client as such.
Client client = new ResteasyClientBuilder()
.establishConnectionTimeout(2, TimeUnit.SECONDS)
.socketTimeout(2, TimeUnit.SECONDS)
.build();
I have not seen a list of standard configuration properties you could set via ClientBuilder.newClient(Configuration configuration) which would be needed to make this portable.
First, you have to add relevant dependencies (here is for the WildFly 10.1):
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>3.0.14.Final</version>
<scope>provided</scope>
</dependency>
Next - create a normal Apache HttpClient and push it the RestEasy Enginge with overriding one method, which causes the problem:
// create here a normal Apache HttpClient with all parameters, that you need
HttpClient httpClient = createHttpClient(connectTimeout,
socketTimeout,
connectionRequestTimeout,
maxTotalHTTPConnections);
// Deprecated Apache classes cleanup https://issues.jboss.org/browse/RESTEASY-1357
// Client Framework not honoring connection timeouts Apache Client 4.3 https://issues.jboss.org/browse/RESTEASY-975
ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient) {
#Override
protected void loadHttpMethod(ClientInvocation request, HttpRequestBase httpMethod) throws Exception {
super.loadHttpMethod(request, httpMethod);
httpMethod.setParams(new BasicHttpParams());
}
};
return new ResteasyClientBuilder().httpEngine(engine).build();
Have a look at https://issues.jboss.org/browse/RESTEASY-975 Seems, that the problem was just resolved in the version 3.1.0.Final.
For people stuck with older JAX-RS 2.0 API and old Resteasy implementation, you may use this method:
Client client = new ResteasyClientBuilder()
.establishConnectionTimeout(3, TimeUnit.SECONDS)
.socketTimeout(5, TimeUnit.SECONDS).build();
Despite the name, socketTimeout stands for "read timeout", since by the docs, it stands for "The timeout for waiting for data".
If you are using Jersey 2.x Here it is the simple solution it's work for me
import com.jclient.JClient;
Client c = Client.create();
WebResource webResource = c.resource("requestUrl");
c.setConnectTimeout(yourMins*60*1000);
This code makes GET request to RadosGW (I don't use Keystone)
String srcEndpoint = "http://myhost/auth/v1.0";
SwiftApi api = ContextBuilder.newBuilder(PROVIDER).endpoint(srcEndpoint)
.credentials(srcIdentity, srcCredential).buildApi(SwiftApi.class);
If PROVIDER is openstack-swift my code throws
org.jclouds.http.HttpResponseException: command: POST http://myhost/auth/v1.0/tokens HTTP/1.1 failed with response: HTTP/1.1 405 Method Not Allowed; content: [{"Code":"MethodNotAllowed"}]
If PROVIDER is swift my code throws
Exception in thread "main" com.google.inject.ConfigurationException: Guice configuration errors:
1) No implementation for org.jclouds.openstack.swift.v1.SwiftApi was bound.
while locating org.jclouds.openstack.swift.v1.SwiftApi
My dependencies are
<dependency>
<groupId>org.apache.jclouds.api</groupId>
<artifactId>swift</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.apache.jclouds.api</groupId>
<artifactId>openstack-swift</artifactId>
<version>1.9.2</version>
</dependency>
How can I list all containers with all their metadata, without downloading the list of blobs it contains?
What's the difference between swift and openstack-swift?
The primary difference is that swift supports v1 auth and openstack-swift supports v2 auth. Unfortunately, swift is also deprecated and no longer maintained.
The reason you are getting that error is because SwiftApi is specific to the openstack-swift API implementation. Despite the heroic efforts that jclouds makes to abstract away all the implementation details, it's not perfect. The swift API implementation returns SwiftClient, which extends CommonSwiftClient (where all the interesting methods are defined).
Also, as you may have guessed, SwiftClient is in a different package. So be sure to include package org.jclouds.openstack.swift; (no ".v1")
You can list all containers with their metadata by calling listContainers(ListContainerOptions... options) on your SwiftClient instance. This will return Set<ContainerMetadata>.
I am starting with REST Assured, getting error while executing below code :
Code 1-
RestAssured.expect().statusCode(200).
body(
"name", equalTo("Russia")
).
when().
get("http://restcountries.eu/rest/v1/callingcode/7");
Exception-
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method equalTo(String) is undefined for the type
Code 2 -
RestAssured.expect().statusCode(200).
body(
"name", Matchers.equalTo("Russia")
).
when().
get("http://restcountries.eu/rest/v1/callingcode/7");
Exception-
Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: com.jayway.restassured.internal.ContentParser.parse() is applicable for argument types: (com.jayway.restassured.internal.RestAssuredResponseImpl, com.jayway.restassured.internal.ResponseParserRegistrar, com.jayway.restassured.config.RestAssuredConfig, java.lang.Boolean) values: [com.jayway.restassured.internal.RestAssuredResponseImpl#753455ab, ...] Possible solutions: wait(), any(), grep()
Below are the only 2 methods in my class, I am having issue with first one, second one is running fine. Please let me know what I am missing in first method.
Method -1
public static void testCountriesCallingCode() {
RestAssured.expect().statusCode(200).
body(
"name", equalTo("Russia")
).
when().
get("http://restcountries.eu/rest/v1/callingcode/7");
System.out.println(RestAssured.get("http://restcountries.eu/rest/v1/callingcode/7").asString());
}
Method-2
public static void testCountriesCallingCodeUsingJSONPATH(){
Response res = RestAssured.get("http://restcountries.eu/rest/v1/callingcode/7");
System.out.println(res.getStatusCode());
String json = res.asString();
JsonPath jp = new JsonPath(json);
System.out.println(jp.get("name"));
}
Thanks Hti, your answer worked. Without the other dependencies, Rest Assured kind of works. I have no idea why Rest Assured website does not note this. Following in pom.xml worked
<properties>
<rest-assured.version>3.0.2</rest-assured.version>
<resteasy.version>3.0.17.Final</resteasy.version>
</properties>
...
<!-- Jackson is for allowing you to convert pojo (plain old Java object) into JSON -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>${resteasy.version}</version>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>${rest-assured.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-path</artifactId>
<version>${rest-assured.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>xml-path</artifactId>
<version>${rest-assured.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-xml</artifactId>
<version>2.4.11</version>
<scope>test</scope>
</dependency>
Change the body of your first example to:
body(
"[0].name", equalTo("Russia")
)
That is because the JSON response from the server is not an object, but an array, and you have to query for the first object ([0]), then the name (.name).
For the Code-1, for equalTo() method you have to import org.hamcrest.Matchers.*;
For the exception in code 2, it is very hard to mention without looking at the RESPONSE but try to follow below link if you have nested generic parameters in your response.
How to validate nested response using REST Assured?
Please let me know if you have any issue or question. Thanks!
Even though this question is old, I just stumpled upon the second problem:
Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: com.jayway.restassured.internal.ContentParser.parse() is applicable for argument types: (com.jayway.restassured.internal.RestAssuredResponseImpl, com.jayway.restassured.internal.ResponseParserRegistrar, com.jayway.restassured.config.RestAssuredConfig, java.lang.Boolean) values: [com.jayway.restassured.internal.RestAssuredResponseImpl#753455ab, ...] Possible solutions: wait(), any(), grep()
This is due to missing dependencies. In my case I needed to add the dependencies for xml-path and groovy-xml, even though I'm just working with JSON data. So the best thing to do is resolving the dependencies transitively.
equalTo comes from Hamcrest which is a JUnit dependency contained within the JUnit jar. You probably just need to import the static method for it from Hamcrest.
import static org.hamcrest.core.IsEqual.*;
Add a static package for equal to:
import static org.hamcrest.Matchers.*;
Using below code sample from tutorial i can successfully make post to a jax-rs service on glassfish-4.
Client client = ClientFactory.newClient();
WebTarget root = client.target("http://localhost:8080/roast-house/api/coffeebeans");
Bean origin = new Bean("arabica", RoastType.DARK, "mexico");
final String mediaType = MediaType.APPLICATION_XML;
final Entity<Bean> entity = Entity.entity(origin, mediaType);
Response response = root.request().post(entity, Response.class);
response.close();
But it forces to bring a dependency that totals about 4.5mb (resteasy 3.0.5 was ~5mb)
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.4.1</version>
</dependency>
I have the feeling i'm using only a portion of the client API, is there any more lightweight clients out there, or how would i go about to construct the request using only standard libraries?
couldnt find somewhere else advice.
I am writing a Restlet JSE Client for a Jersey(!) Restful Service. I already wrote a Jersey client for that and it is working, so the jersey service is alright. Now I get problems in writing a restlet client:
My Service root adress is:
http://localhost:8080/com-project-core/rest, so I call:
ClientResource = service = new ClientResource("http://localhost:8080/com-project-core/rest");
My Basic Auth Credentiels are admin and xxx, so I call:
service.setChallengeResponse(ChallengeScheme.HTTP_BASIC, "admin", "xxx");
Now the problems:
ClientResource service = new ClientResource("http://localhost:8080/com-project-core/rest/ping");
calls up my service. After that I try
String myString = service.get(String.class);
System.out.println(myString);
I get a:
08.07.2012 17:41:48 org.restlet.engine.http.connector.HttpClientHelper start
INFO: Starting the default HTTP client
in my output. Not more! The Junit Test says:
Not Acceptable (406) - Not Acceptable
So he can find the resource but cannot produce #Produces("text/plain") ??
So when I remove #Produces("text/plain") on server side it works!!
For the resourcey my server side looks like this:
#Path("/ping")
#RolesAllowed({"admin", "user"})
public class ConnectedResourceBean implements ConnectedResourceIF {
#GET
#Produces("text/plain")
public String getPingMessage() throws NamingException {
return "Hello World";
}
}
For my pom in set this dependencies:
<dependency>
<groupId>org.restlet.jse</groupId>
<artifactId>org.restlet</artifactId>
<version>${restlet.version}</version>
</dependency>
<dependency>
<groupId>org.restlet.jse</groupId>
<artifactId>org.restlet.ext.xstream</artifactId>
<version>${restlet.version}</version>
</dependency>
As I said, its working with my jersey client.
No way: Restlet had problems with
#Produces("text/plain")
on jersey server side. Can someone explain me that fact?
Edit:
Made it work with
<properties>
<restlet.version>2.1-M3</restlet.version>
</properties>