SparkJava before filter with WebSocket - java

I'm using Spark Java to create a simple API.
Furthermore I've been using spark-authentication for basic authentication.
webSocket(ROUTE_WS, GarageWebsocket.class);
get(ROUTE_GARAGE_DOOR_STATE, GarageDoorController::getGarageDoorState);
put(ROUTE_GARAGE_DOOR_COMMAND_OPEN, GarageDoorController::openGarageDoor);
put(ROUTE_GARAGE_DOOR_COMMAND_CLOSE, GarageDoorController::closeGarageDoor);
put(ROUTE_GARAGE_DOOR_COMMAND_TOGGLE, GarageDoorController::toggleGarageDoor);
put(ROUTE_GARAGE_DOOR_COMMAND_STOP, GarageDoorController::stopGarageDoor);
before(new BasicAuthenticationFilter("/garage/*", new AuthenticationDetails(USERNAME, PASSWORD)));
The problem seems to be, that the before filter also applies to the websocket route.
Which is /ws.
The PUT routes starts with /garage/....
Any ideas?

Related

How to start CloudFoundry app using ReactorCloudFoundryClient?

I used StartApplicationRequest to create a sample request to start the application as given below:
StartApplicationRequest request = StartApplicationRequest.builder()
.applicationId("test-app-name")
.build();
Then, I used the ReactorCloudFoundryClient to start the application as shown below:
cloudFoundryClient.applicationsV3().start(request);
But my test application test-app-name is not getting started. I'm using latest Java CF client version (v4.5.0 RELEASE), but not seeing a way around to start the application.
Quite surprisingly, the outdated version seems to be working with the below code:
cfstatus = cfClient.startApplication("test-app-name"); //start app
cfstatus = cfClient.stopApplication("test-app-name"); //stop app
cfstatus = cfClient.restartApplication("test-app-name"); //stop app
I want to do the same with latest CF client library, but I don't see any useful reference. I referred to test cases written at CloudFoundry official Github repo. I derived to the below code after checking out a lot of docs:
StartApplicationRequest request = StartApplicationRequest.builder()
.applicationId("test-app-name")
.build();
cloudFoundryClient.applicationsV3().start(request);
Note that cloudFoundryClient is ReactorCloudFoundryClient instance as the latest library doesn't support the client class used with outdated code. I would like to do all operations (start/stop/restart) with latest library. The above code isn't working.
A couple things here...
Using the reactor based client, your call to cloudFoundryClient.applicationsV3().start(request) returns a Mono<StartApplicationResponse>. That's not the actual response, it's the possibility of one. You need to do something to get the response. See here for more details.
If you would like similar behavior to the original cf-java-client, you can call .block() on the Mono<StartApplicationResponse> and it will wait and turn into a response.
Ex:
client.applicationsV3()
.start(StartApplicationRequest.builder()
.applicationId("test-app-name")
.build())
.block()
The second thing is that it's .applicationId not applicationName. You need to pass in an application guid, not the name. As it is, you're going to get a 404 saying the application doesn't exist. You can use the client to fetch the guid, or you can use CloudFoundryOperations instead (see #3).
The CloudFoundryOperations interface is a higher-level API. It's easier to use, in general, and supports things like starting an app based on the name instead of the guid.
Ex:
ops.applications()
.start(StartApplicationRequest.builder()
.name("test-app-name").build())
.block();

How to conversion of Mysql data http calls use Spring cloud data flow?

I need to implement an ETL-like function to Migrate Mysql Data to another system via http calls. A high degree of real-time data is required in the process
I tried to combine spring-cloud-starter-stream-source-jdbc and spring-cloud-starter-stream-processor-httpclient. Instead, I got spring-cloud-starter-stream-source-jdbc without main class error.
jdbc --spring.datasource.driver-class-name=org.mariadb.jdbc.Driver --spring.datasource.username='******' --spring.dataso… | http …
At first, I reconstructed it with reference to the https://github.com/spring-cloud/spring-cloud-stream-samples/tree/master/source-samples/jdbc-source . I added main class and rabbit binder. The stream is running as scheduled. I didn't think it was supposed to be this complicated, and then I switched to jdbc-source-rabbit, which is exactly what I expected

Custom OSRM server Mapbox Navigation SDK

Is it possible using custom OSRM server (Docker) for routing in navigation SDK? If i have custom streets in postgrey db, how can i calculate route on this streets?
Something as
NavigationRoute.builder(this)
.baseUrl("my server url")
does make request to my server but with additional params in query which i dont want :
/route/v1/driving/directions/v5/mapbox/driving-traffic/
I need just
/route/v1/driving/
Is it possible or exist some lib which converts osrm format to mapbox format?
I've found that it's reasonably trivial to use OSRM as a backing server for the Graphhopper Navigation API (which was forked from Mapbox I believe). I haven't tried using it directly with the Mapbox SDKs, but it might be worth a shot. Basically all I had to do was start up a forwarding server that would grab the coordinates and route parameters and pass them to OSRM, then add a request UUID on the way back to stop the SDK from complaining. I implemented the server in Ruby using Sinatra, and the code is below:
require 'net/http'
require 'sinatra'
require 'sinatra/json'
get '/directions/v5/:user/driving/:coordinates' do
uri = URI("http://router.project-osrm.org/route/v1/driving/#{params['coordinates']}")
uri.query = URI.encode_www_form({
alternatives: params['alternatives'],
continue_straight: params['continue_straight'],
geometries: params['geometries'],
overview: params['overview'],
steps: params['steps']
})
res = JSON.parse(Net::HTTP.get_response(uri).body)
res["uuid"] = SecureRandom.uuid
json(res)
end

How to implement a catch all route with java spark for an SPA

The app I'm trying to get working uses Vue.js and Java Spark. I am trying to get the SPA to work with HTML5 history mode. Right now it serves the index page and the app takes over routing from there. Because of html5 routing, if I try to go straight to a url such as "/about", i'll get an error. I tried to add a catch all route using:
get("/*", (rq, rs) -> new ModelAndView(map, "index.hbs"), new HandlebarsTemplateEngine());
But this overrides the previously defined static file routing in:
staticFileLocation("/public");
How can I implement a catch all route for all other pages without overriding the static file routes? I'd rather not have to redefine every route on the server to the same page. I've accomplished this in node.js with express, It has to be possible with Spark.
Spark.get("*", (request, response) ->
{
response.type("application/json");
return "not supported";
});
Should do the trick.

Remote call to a PlayFramework object

I am looking for a way to send serialized objects (or simply strings) to a PlayFramework model or controller object through a remote JVM.
I am trying to create a web application based on pushed events, so for the moment, it is possible to get new events through a long polling ajax call, when some events are added to the model.
Now, I would like to add events into my model from a remote JVM, through RMI, a socket, or anything which could work. I have searched in the PlayFramework documentation but haven't found any API or piece of code on how to do this.
You can use WebSockets, I wrote a blog post about it here:
http://geeks.aretotally.in/log4play-log4j-ui-mashed-up-with-play-framework-knockout-js-and-websockets
My example only pushes from server to client but you can use WebSockets for two-way communication through JSON:
http://www.playframework.org/documentation/1.2.1/asynchronous#UsingWebSockets
You can also use Akka Remote Actors (http://akka.io/docs/akka/1.1.3/scala/remote-actors.html).
Isn't pushing data into the server the easy part?
The natural way would be to invoke some controller action (read: HTTP POST) accepting a JsonData object (if data is structured) or plain parameters if data is unstructured.
Off the top of my head, in play speak it would look like this:
WSRequest request = new WSUrlFetch().newRequest("http://<url of your 'vm'>");
// request.setParameter("param", value);
// ...
request.post();
You do not need WebSockets for this.

Categories