I have a url
http://localhost:8080/api/search/lookup?limit=3000&m=airid.appname.mapping{appname=*}
path("api"/"search"/"lookup?limit=3000&m=appname.server.mapping") is unavailable
how to match the entire url including parameters and values?
The part of the url after the '?' is referred to as the query string and is technically not part of the path:
a query string is the part of a uniform resource locator (URL)
containing data that does not fit conveniently into a hierarchical
path structure
Therefore, you cannot access it using the path directive. If you want the entire uri then you need extractUri:
val myRoute = extractUri { uri =>
//rest of Route logic
}
Related
I am using a the emicklei/go-restful framework to deal with rest API.
I wish to access the route path from the request. Meaning that when I configure a dummy route like this:
ws := new(restful.WebService)
ws.Path("/resources/names")
ws.Route(ws.GET("/{name}").To(getName))
restful.DefaultContainer.Add(ws)
I wish to access the information that the route was "/resources/names/{name}"
I can access the actual URL which is call by calling:
req.Request.URL.Path
But this will return the specific URL, not the generic one.
Any suggestion?
After more research I finally found that the method req.SelectedRoutePath() will return expected value.
I'm currently trying to mock external server using Wiremock.
One of my external server endpoint takes a payload.
This endpoint is defined as follow :
def sendRequestToMockServer(payload: String) = {
for {
request_entity <- Marshal(payload).to[RequestEntity]
response <- Http().singleRequest(
HttpRequest(
method = HttpMethods.GET,
uri = "http://localhost:9090/login",
entity = request_entity
)
)
} yield {
response
}
}
To mock this endpoint using Wiremock, I have written the following code :
stubFor(
get(urlEqualTo("/login"))
.willReturn(
aResponse()
.withHeader("Content-Type","application/json")
.withBodyFile("wireMockResponse.json")
.withStatus(200)
)
.withRequestBody(matchingJsonPath("requestBody.json"))
)
where I Have defined the request body in the requestBody.json file.
But when I run tests , I keep getting an error indicating that the requested Url is not found.
I'm thinking that the error is related to this line withRequestBody(matchingJsonPath("requestBody.json")), because when I comment it the error disappear.
Any suggestions on how to work around this?
matchingJsonPath does not populate a file at a provided filepath, but instead evaluates the JsonPath provided. See documentation.
I'm not entirely sure there is a way to provide the request body as a .json file. If you copy the contents of the file into the withRequest(equalToJson(_yourJsonHere_)), does it work? If it does, you could get the file contents as a JSON string above the definition and provide it to the function (or I guess, make a function to return a JSON string from a .json file).
Additionally, you could make a custom request matcher that does the parsing for you. I think I'd recommend this only if the above does not work.
See the scenario below
http://localhost:8016/api/vendor/self/member/group/
#api.route('/<url_type:url_type>/<vendor_type:vendor_type>/<path:path>')
def mock_endpoint(url_type, vendor_type, path):
OR
http://localhost:8016/api/vendor?path=/self/member/group/
#api.route('/<url_type:url_type>/<vendor_type:vendor_type>/')
def mock_endpoint(url_type, vendor_type):
# get path queryparam here
which of the above scenario is restful and better? passing relative URL (/self/member/group/) as a part of url or path as a querystring?
Note: This (/self/member/group/) part is dynamic in terms of slashes. can be anything i.e. /groups or /venue/
When I face a URL like:
http://localhost:8016/api/vendor/self/member/group/
I understand group is a subresource of member which is a subresource of self which is a subresource of the vendor resource. It is a hierarchy. If it's what you mean, go for this approach.
Otherwise, consider the query string approach and don't forget to URL encode the slashes:
http://localhost:8016/api/vendor?path=%2Fself%2Fmember%2Fgroup%2F
According to ResourceResolver Interface:
http://dev.day.com/docs/en/cq/current/javadoc/org/apache/sling/api/resource/ResourceResolver.html
There are three ways to resolve either path or request to a Resource:
Resource resolve(HttpServletRequest request)
Deprecated. as of 2.0.4, use resolve(HttpServletRequest, String) instead.
Resource resolve(HttpServletRequest request, String absPath)
Resolves the resource from the given absPath optionally taking
HttpServletRequest into account, such as the value of the Host request header.
Resource resolve(String absPath)
Resolves the resource from the given absolute path.
But if I have a random given URL string (e.g. http://www.mycompany.com/whatever.html), how do I programmatically find out the corresponding Resource of the given URL?
If the hostname/port from the URL is mapped to a content repository location CQ will attempt to resolve the URL supplied.
In a servlet the ResourceResolver can be obtained from the slingRequest:
ResourceResolver resourceResolver = slingRequest.getResourceResolver();
String resourcePath = new URI("http://www.mycompany.com/whatever.html").getPath();
Resource res = resourceResolver.resolve(resourcePath);
Bear in mind that for short urls and domains like the above to work you would need to configure mapping on your instance.
In a JSP, as long as you have called the <sling:defineObjects/> or <cq:defineObjects/> tag you will be able to use:
<sling:defineObjects>
<%
String resourcePath = new URI("http://www.mycompany.com/whatever.html").getPath();
Resource res = resourceResolver.resolve(resourcePath);
%>
Some more information is provided in "Getting Resources and Properties in Sling"
Test out on a couple of URLs you know are good. For example:
Resource res = resourceResolver.resolve("http://localhost:4502/content/geometrixx.html");
Resource res = resourceResolver.resolve("/content/geometrixx.html");
Both of the above should resolve to the same resource.
If you want to test whether CQ can resolve the URL you are providing, try the jcr resolver page in the system console
http://localhost:4502/system/console/jcrresolver to see if the url is mapped if it does not contain the full /content/.. in the path. Any mapped should be able to be resolved.
ResourceResolver class was implemented to return Resource. Specifically, the resolve() functions exist for this type of resolution. However, even there are three overloaded resolve() functions, none of them takes in a URL String.
Given ResourceResolver takes in HttpServletRequest as input, if I can transform (adapt) a given URL into a HttpServletRequest using HttpServletRequestWrapper, the problem will be solved. Therefore, the solution is to implement a ResolverRequest class which extends HttpServletRequestWrapper.
For the complete solution and code sample please see "Programmatically find the Sling Resource from a Given a URL"
I had made a REST webservice using redirecting to various paths like if i need to delete some user then i will redirect the user to this address in the #Path annotation :
user/delete
and therefore there is no thing like RESPONSE i have used.
While going through a code given to me by my senior i came accross these lines :
java.net.URI uri = uriInfo.getAbsolutePathBuilder().path(id).build();
Response.created(uri).build();
What are these lines doing, i have no idea.
Can someone explain me this w/o wiki links or any other 'Basic Rest Service' links.
Without any explicit details about the uriInfo object I can only speculate its type is the JAX-RS UriInfo class.
The first line can be broken down as below:
java.net.URI uri = uriInfo.getAbsolutePathBuilder().path(id).build();
The getAbsolutePathBuilder is documented http://jackson.codehaus.org/javadoc/jax-rs/1.0/javax/ws/rs/core/UriInfo.html#getAbsolutePathBuilder%28%29
java.net.URI uri = uriInfo.getAbsolutePathBuilder().path(id).build();
The method returns a UriBuilder object. On which the 'path(...)' method is called passing the id so if the absolute path returned http://www.host.com (this may or may not have a port number) adding the id in this method will then result in the effectively Builder holding the two parts. The base URI and the path. The two values have not yet been put together
The build method then concatenates the two values resulting a full URI. For example http://www.google.com/id (Where http://www.google.com is the absolute path)
The second line
Response.created(uri).build();
Is basically saying 'Respond with a created (201) response code, and set a Location header containing the build uri value'