Camel Restlet - Context Path Ambiquity - rest

I have Spring Boot Camel application where rest apis are exposed using camel-restlet
Sample route
#Component
public class AppRoute extends RouteBuilder{
public void configure(CamelContext context){
from("restlet:employee?restletMethods=GET").log("${body}");
}
}
The App runs perfect ( spring-boot:run ). but am unable to locate under which path the API is exposed. Log has no information.
Every API i hit returns 404. Log shows the route has been started. Under which path is it running. And how do I change it?
Note: Please dont suggest any XML based configuration. Anything that I can put under #Configuration would be perfect

I would go with the Rest DSL which is supported by the camel-restlet component like this
restConfiguration().component("restlet").port(8080);
rest("/rest")
.get("employee")
.route().log("${body}")
.endRest();
And this route will listen to the following url
http://localhost:8080/rest/employee
EDIT:
I guess you could do something like without using the Rest DSL
String host = InetAddress.getLocalHost().getHostName();
from("restlet:http://" + host + contextPath + "/employee?restletMethods=GET").log("${body}")
The port and context path are configurable with the following properties
camel.component.restlet.port=8686
server.servlet.context-path=/my-path
The context path can be injected in the routeBuilder with
#Value("${server.servlet.context-path}")
private String contextPath;

According to the documentation, the format of the URI in a restlet endpoint definition should be the following:
restlet:restletUrl[?options]
Where restletUrl should have the following format:
protocol://hostname[:port][/resourcePattern]
So in your case you could define the URI in the following way:
from("restlet:http://localhost/employee?restletMethods=GET")
This should make the endpoint available under the following URL:
http://localhost/employee
Which you can test e.g. in a web browser.

Use the first of the three configuration methods described here:
https://restlet.com/open-source/documentation/javadocs/2.0/jee/ext/org/restlet/ext/servlet/ServerServlet.html
You should be able to customize it using the Component:
https://restlet.com/open-source/documentation/javadocs/2.0/jee/api/org/restlet/Component.html?is-external=true
See in particular setServers() methods (or XML equivalent) to change the hostname and port.

Related

No API definition provided. - openApi - springdoc

I have a simple boot application where I have added open api swagger dependency
springdoc-openapi-ui
along with these properties
springdoc.swagger-ui.disable-swagger-default-url=true
springdoc.swagger-ui.configUrl=/v3/api-docs/swagger-config
springdoc.swagger-ui.path=/swagger-ui.html
I am getting these error (when calling http://localhost:8080/swagger-ui/index.html):
No API definition provided.
this is my controller :
#RestController
public class HelloWorld {
#GetMapping("sayHi")
public String sayHi(){
return "Hi Beno";
}
}
Any idea ?
When using a different endpoint to serve the OpenAPI Config, you'll need to set two properties
// This will set UI to fetch the config's URL from "somePath"
springdoc.swagger-ui.configUrl=somePath
// You also need to serve the config file from the endpoint at "somePath"
springdoc.api-docs.path=somePath

Authenticate with ECE ElasticSearch Sink from Apache Fink (Scala code)

Compiler error when using example provided in Flink documentation. The Flink documentation provides sample Scala code to set the REST client factory parameters when talking to Elasticsearch, https://ci.apache.org/projects/flink/flink-docs-stable/dev/connectors/elasticsearch.html.
When trying out this code i get a compiler error in IntelliJ which says "Cannot resolve symbol restClientBuilder".
I found the following SO which is EXACTLY my problem except that it is in Java and i am doing this in Scala.
Apache Flink (v1.6.0) authenticate Elasticsearch Sink (v6.4)
I tried copy pasting the solution code provided in the above SO into IntelliJ, the auto-converted code also has compiler errors.
// provide a RestClientFactory for custom configuration on the internally created REST client
// i only show the setMaxRetryTimeoutMillis for illustration purposes, the actual code will use HTTP cutom callback
esSinkBuilder.setRestClientFactory(
restClientBuilder -> {
restClientBuilder.setMaxRetryTimeoutMillis(10)
}
)
Then i tried (auto generated Java to Scala code by IntelliJ)
// provide a RestClientFactory for custom configuration on the internally created REST client// provide a RestClientFactory for custom configuration on the internally created REST client
import org.apache.http.auth.AuthScope
import org.apache.http.auth.UsernamePasswordCredentials
import org.apache.http.client.CredentialsProvider
import org.apache.http.impl.client.BasicCredentialsProvider
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder
import org.elasticsearch.client.RestClientBuilder
// provide a RestClientFactory for custom configuration on the internally created REST client// provide a RestClientFactory for custom configuration on the internally created REST client
esSinkBuilder.setRestClientFactory((restClientBuilder) => {
def foo(restClientBuilder) = restClientBuilder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
override def customizeHttpClient(httpClientBuilder: HttpAsyncClientBuilder): HttpAsyncClientBuilder = { // elasticsearch username and password
val credentialsProvider = new BasicCredentialsProvider
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(es_user, es_password))
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)
}
})
foo(restClientBuilder)
})
The original code snippet produces the error "cannot resolve RestClientFactory" and then Java to Scala shows several other errors.
So basically i need to find a Scala version of the solution described in Apache Flink (v1.6.0) authenticate Elasticsearch Sink (v6.4)
Update 1: I was able to make some progress with some help from IntelliJ. The following code compiles and runs but there is another problem.
esSinkBuilder.setRestClientFactory(
new RestClientFactory {
override def configureRestClientBuilder(restClientBuilder: RestClientBuilder): Unit = {
restClientBuilder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
override def customizeHttpClient(httpClientBuilder: HttpAsyncClientBuilder): HttpAsyncClientBuilder = {
// elasticsearch username and password
val credentialsProvider = new BasicCredentialsProvider
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(es_user, es_password))
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)
httpClientBuilder.setSSLContext(trustfulSslContext)
}
})
}
}
The problem is that i am not sure if i should be doing a new of the RestClientFactory object. What happens is that the application connects to the elasticsearch cluster but then discovers that the SSL CERT is not valid, so i had to put the trustfullSslContext (as described here https://gist.github.com/iRevive/4a3c7cb96374da5da80d4538f3da17cb), this got me past the SSL issue but now the ES REST Client does a ping test and the ping fails, it throws an exception and the app shutsdown. I am suspecting that the ping fails because of the SSL error and maybe it is not using the trustfulSslContext i setup as part of new RestClientFactory and this makes me suspect that i should not have done the new, there should be a simple way to update the existing RestclientFactory object and basically this is all happening because of my lack of Scala knowledge.
Happy to report that this is resolved. The code i posted in Update 1 is correct. The ping to ECE was not working for two reasons:
The certificate needs to include the complete chain including the root CA, the intermediate CA and the cert for the ECE. This helped get rid of the whole trustfulSslContext stuff.
The ECE was sitting behind an ha-proxy and the proxy did the mapping for the hostname in the HTTP request to the actual deployment cluster name in ECE. this mapping logic did not take into account that the Java REST High Level client uses the org.apache.httphost class which creates the hostname as hostname:port_number even when the port number is 443. Since it did not find the mapping because of the 443 therefore the ECE returned a 404 error instead of 200 ok (only way to find this was to look at unencrypted packets at the ha-proxy). Once the mapping logic in ha-proxy was fixed, the mapping was found and the pings are now successfull.

change default /webservices path for Apache CXF SOAP service

I'm using tomEE 1.7.1 with Apache CXF 2.6.14 inside.
I have a component that serves a WSDL first web service:
#Stateless
#WebService(
endpointInterface = "com.mycompany.SecurityTokenServiceWS",
targetNamespace = "http://sts.mycompany/wsdl/",
serviceName = "SecurityTokenService",
portName = "TokenService")
#SOAPBinding(style = SOAPBinding.Style.RPC, use = SOAPBinding.Use.LITERAL)
public class TokenService implements SecurityTokenServiceWS {
//service methods
}
When I deploy the web app, I see this log:
Jan 30, 2015 12:47:22 PM org.apache.openejb.server.webservices.WsService deployApp
INFORMATION: Webservice(wsdl=http://localhost:8080//webservices/TokenService, qname={http://sts.mycompany.com/wsdl/}SecurityTokenService) --> Ejb(id=TokenService)
In result the web service is available on: http://localhost:8080/webservices/TokenService.
What I like to have is that the service runs directly on: http://localhost:8080/TokenService.
I have no idea where the "webservices" path element comes from. It isn't in the WSDL and not in any configuration file. My web application runs directly under the context path / (ROOT).
Is there a magic CXF servlet that is bonded to /webservices? How can I change this behavior?
this comes from TomEE which uses subcontext webservices by default.
This sample shows how to change it https://git-wip-us.apache.org/repos/asf?p=tomee.git;a=tree;f=examples/change-jaxws-url;h=2f88382bd4f925ec27c7305e74d361c8baf46a92;hb=ebe63371a22709a50e79c42206b5e9a0fd8946cc (the interesting file is https://git-wip-us.apache.org/repos/asf?p=tomee.git;a=blob;f=examples/change-jaxws-url/src/main/resources/META-INF/openejb-jar.xml;h=6c0ba44b14eb2e67a550c65d890d325c8bf409b7;hb=ebe63371a22709a50e79c42206b5e9a0fd8946cc)
Note: if you just want to rename /webservices you can set in conf/system.properties tomee.jaxws.subcontext=/myothersubcontext
PS: if you go with openejb-jar.xml solution note there is the equivalent for openejb-jar.xml 1.1 which is just the property openejb.webservice.deployment.address in your ejb-deployment properties
To change the publishiing address you need to change endpoint configuration. For now I guess you have no configuration and all is default. You need to create file service.xml (any name) and provide path to it either using web.xml CXFServlet init-parameter "config-location" or using Spring.
Here is the file contents http://cxf.apache.org/docs/jax-ws-configuration.html
And here is an example how to do it with spring http://cxf.apache.org/docs/writing-a-service-with-spring.html

How to start rmic registry and server using java 1.7?

I am new to java RMI, actually I wrote, compiled and started rmic, and also tried to start server but failed due to _stub 'ClassNotFound' exception..... I'm using java 7... I searched a lot on Google but nobody told step by step example that could work...I got some idea about stteing codebase and security policy but not very clear suggestion that how to do it.. please help telling me steps including command-line .......... please... I have everything just tell me how to start server, and required settings like codebase or policy settings etc... Thanks
You don't need to use rmic. Instead, create your server object and call one of the exportObject() method overloads that has the port parameter. For example,
MyRemoteIntf stub = UnicastRemoteObject.exportObject(server, 0);
This will cause RMI to generate the stub automatically. (The documentation is horribly unclear on this point. If you use the version without the port parameter, it will use only the old, rmic-generated stubs instead of generating them automatically.)
Also, make sure that your remote interface is in the codebase of both the registry and any clients. You'll get different errors if you haven't done this properly. This has been answered a bunch of times on Stackoverflow already; search for "rmi ClassNotFoundException".
There is no need to use the rmic command since java (J2SE) 5.0 the stubs are dynamically generated at runtime.
Here is a basic step by step example of how to use RMI.
First define the Remote interface that defines what the client can see and do:
public interface FooService extends Remote {
// Don't forget to add throws RemoteException.
public void bar() throws RemoteException;
}
NOTE: if you do not add throws RemoteException to the method declaration you will encounter the following exception:
java.lang.IllegalArgumentException: illegal remote method encountered: public abstract void RMIExample.FooService.bar()
After you defined what the client can do you must define the concrete implementation of the methods on the server slide:
public class FooServiceImpl extends UnicastRemoteObject implements FooService {
public FooServiceImpl() throws RemoteException {
super();
}
public void bar() {
System.out.println("I was remotely invoked!");
}
}
The implementation class must inherit from UnicastRemoteObject and implement the Remote interface you defined earlier and the class must have a constructor that throws RemoteException.
Now that you have fully defined the remote functionality you must bind the implementation object to a URL on the server side:
// 4000 is the port to listen on.
LocateRegistry.createRegistry(4000);
Naming.rebind("//127.0.0.1:4000/foobar", new FooServiceImpl());
Now that you have your server up and running you need a stub instance A.K.A a proxy in the client side:
FooService fooService =
(FooService)Naming.lookup("//127.0.0.1:4000/foobar");
and finally invoke the remote method:
fooService.bar();
the following should be printed on the server side:
I was remotely invoked!
References:
http://en.wikipedia.org/wiki/Java_remote_method_invocation
http://docs.oracle.com/javase/1.5.0/docs/guide/rmi/relnotes.html

grailsApplication not getting injected in a service , Grails 2.1.0

I have service in which i am accessing few configuration properties from grailsApplication
I am injecting it like this
class MyWebService{
def grailsApplication
WebService webService = new WebService()
def getProxy(url, flag){
return webService.getClient(url)
}
def getResponse(){
def proxy = getProxy(grailsApplication.config.grails.wsdlURL, true)
def response = proxy.getItem(ItemType)
return response
}
}
When i call getProxy() method, i see this in tomcat logs
No signature of method: org.example.MyWebService.getProxy() is applicable for argument types: (groovy.util.ConfigObject, java.lang.Boolean) values: [[:], true]
Possible solutions: getProxy(), getProxy(java.lang.String, boolean), setProxy(java.lang.Object)
which means grailsApplication is not getting injected into the service, is there any alternate way to access configuration object ? according to burtbeckwith's post configurationholder has been deprecated, can't think of anything else.
Interestingly the very same service works fine in my local IDE(GGTS 3.1.0), that means locally grailsApplication is getting injected, but when i create a war to deploy to a standalone tomcat, it stops getting injected.
I seem to have figured out the problem, actually grailsApplication is getting injected properly otherwise it would have thrown a null pointer exception, i feel the configuration properties are not getting added. Actually the scenario is like, i have a separate custom configuration file which holds configuration data for different environments, my application listens to the environement type(a variable which is set from tomcat) and based on that merges the corresponding config properties from my custom configuration file. i think those propreties are probably not getting added