Getting Path (context root) to the Application in Restlet - restlet

I am needing to get the application root within a Restlet resource class (it extends ServerResource). My end goal is trying to return a full explicit path to another Resource.
I am currently using getRequest().getResourceRef().getPath() and this almost gets me what I need. This does not return the full URL (like http://example.com/app), it returns to me /resourceName. So two problems I'm having with that, one is it is missing the schema (the http or https part) and server name, the other is it does not return where the application has been mounted to.
So given a person resource at 'http://dev.example.com/app_name/person', I would like to find a way to get back 'http://dev.example.com/app_name'.
I am using Restlet 2.0 RC3 and deploying it to GAE.

It looks like getRequest().getRootRef().toString() gives me what I want. I tried using a combination of method calls of getRequest().getRootRef() (like getPath or getRelativePart) but either they gave me something I didn't want or null.

Just get the base url from service context, then share it with the resources and add resource path if needed.
MyServlet.init():
String contextPath = getServletContext().getContextPath();
getApplication().getContext().getAttributes().put("contextPath", contextPath);
MyResource:
String contextPath = getContext().getAttributes().get("contextPath");

request.getRootRef() or request.getHostRef()?

The servlet's context is accessible from the restlet's application:
org.restlet.Application app = org.restlet.Application.getCurrent();
javax.servlet.ServletContext ctx = ((javax.servlet.ServletContext) app.getContext().getAttributes().get("org.restlet.ext.servlet.ServletContext"));
String path = ctx.getResource("").toString();

Related

Flowable - concat dynamic variable and a string in http task (url)

I need to convert the base url according to the production and other environments.
I am using script task before a http task to perform this logic.
baseUrl = http://localhost:8080
baseUrl, is the output of the script task. Now I need to add this base url as a prefix in http task url
Url = ${baseUrl}/application/find (something like this).
I am getting the following issue
Unknown Property used in the expression ${baseUrl}/application/find
Script
var env = execution.getVariable("env")
if(env == "prod") {
var baseUrl = "http://localhost:8080";
execution.setVariable("baseUrl", baseUrl);
}
Please assist.
This typically means that it is unable to find a property in the expression (as the message says). The only expression you are using is baseUrl which means that the issue is around the baseUrl. The concatenation as you have done it is correct and doesn't need to have an adaption.
You should check if the variable really exists, this you can do by introducing a wait state before your HTTP task and check afterwards if the variable is created. Rather than using outputs, you can also use the Java API in your script task to create the variable:
execution.setVariable("baseUrl", "http://localhost:8080");
Assuming you are using Spring Boot, for your specific use-case it would be also an option to use the application.properties to specify your base-url and then refer to the baseUrl with the following expression:
${environment.getProperty("baseUrl")}/application/find
This will allow you to change the baseUrl independent of your process definition.

Spring Cloud Server serving multiple property files for the same application

Lets say I have applicationA that has 3 property files:
-> applicationA
- datasource.properties
- security.properties
- jms.properties
How do I move all properties to a spring cloud config server and keep them separate?
As of today I have configured the config server that will only read ONE property file as this seems to be the standard way. This file the config server picks up seems to be resolved by using the spring.application.name. In my case it will only read ONE file with this name:
-> applicationA.properties
How can I add the other files to be resolved by the config server?
Not possible in the way how you requested. Spring Cloud Config Server uses NativeEnvironmentRepository which is:
Simple implementation of {#link EnvironmentRepository} that uses a SpringApplication and configuration files located through the normal protocols. The resulting Environment is composed of property sources located using the application name as the config file stem (spring.config.name) and the environment name as a Spring profile.
See: https://github.com/spring-cloud/spring-cloud-config/blob/master/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepository.java
So basically every time when client request properties from Config Server it creates ConfigurableApplicationContext using SpringApplicationBuilder. And it is launched with next configuration property:
String config = application;
if (!config.startsWith("application")) {
config = "application," + config;
}
list.add("--spring.config.name=" + config);
So possible names for property files will be only application.properties(or .yml) and config client application name that is requesting configuration - in your case applicationA.properties.
But you can "cheat".
In config server configuration you can add such property
spring:
cloud:
config:
server:
git:
search-paths: '{application}, {application}/your-subdirectory'
In this case Config Server will search for same property file names but in few directories and you can use subdirectories to keep your properties separate.
So with configuration above you will be able to load configuration from:
applicationA/application.properies
applicationA/your-subdirectory/application.properies
This can be done.
You need to create your own EnvironmentRepository, which loads your property files.
org.springframework.cloud.config.server.support.AbstractScmAccessor#getSearchLocations
searches for the property files to load :
for (String prof : profiles) {
for (String app : apps) {
String value = location;
if (app != null) {
value = value.replace("{application}", app);
}
if (prof != null) {
value = value.replace("{profile}", prof);
}
if (label != null) {
value = value.replace("{label}", label);
}
if (!value.endsWith("/")) {
value = value + "/";
}
output.addAll(matchingDirectories(dir, value));
}
}
There you could add custom code, that reads the required property files.
The above code matches exactly the behaviour described in the spring docs.
The NativeEnvironmentRepository does NOT access GIT/SCM in any way, so you should use
JGitEnvironmentRepository as base for your own implementation.
As #nmyk pointed out, NativeEnvironmentRepository boots a mini app in order to collect the properties by providing it with - sort of speak - "hardcoded" {appname}.* and application.* supported property file names. (#Stefan Isele - prefabware.com JGitEnvironmentRepository ends up using NativeEnvironmentRepository as well, for that matter).
I have issued a pull request for spring-cloud-config-server 1.4.x, that supports defining additional file names, through a spring.cloud.config.server.searchNames environment property, in the same sense one can do for a single springboot app, as defined in the Externalized Configuration.Application Property Files section of the documentation, using the spring.config.name enviroment property. I hope they review it soon, since it seems many have asked about this feature in stack overflow, and surely many many more search for it and read the currently advised solutions.
It worths mentioning that many ppl advise "abusing" the profile feature to achieve this, which is a bad practice, in my humble opinion, as I describe in this answer

How can I get window.location.href in Elm?

I have an index.html which contains my Elm app. The Elm app uses various GETs to an API served by the same server as the one that serves the index.html.
Rather than hardcode the URLs in my Elm code for the GETs, e.g.:
url =
"http://localhost:8080/api/tasks"
is there a function which returns the value of window.location.href?
I'd like to do something like:
url =
getHref() ++ "/api/tasks"
In this way, if I move my server to somewhere else I will not need to update all the urls in my Elm code.
Whilst the above answers your question, I think there is a more straightforward solution to the problem:
If the application code is being served from the same server (URL) as the API you want to access you don't need to specify the server - just the root relative path for your api i.e. you can make requests to /api/tasks from your elm code and the browser will sort out the rest for you.
This is how I addressed the problem in my deployed code.
There is elm-history package with the location function for this, but it's deprecated and doesn't exist for 0.18 version.
Then you might want to use elm-navigation package and explicitly store the current location in your model.
Please have a look at this example. A program with navigation can be created via:
Navigation.program UrlChange
{ init = init
, view = view
, update = update
, subscriptions = (\_ -> Sub.none)
}
UrlChange here is a type of message, which triggers on every url change, so you can process it and set the current location:
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
UrlChange location ->
( { model | location = location }
, Cmd.none
)
And then purely get the location.href wherever the model is accessible.
In the provided application, this place is view: viewLocation model.location
In your application, it's, for example, something like this:
url model =
model.location.href ++ "/api/tasks"
Use URL Builder to link to anoter page on your site
There's no need to specify the base url:
import Url.Builder exposing (absolute)
url = absolute [ "api", "tasks" ] []
-- results in "http://localhost:8080/api/tasks"
-- if working in your developer environment
-- the URL will automatically change in production
-- to the correct URL assuming you don't have any crazy set ups
And you definitely will not need to worry if your server URL changes. So you can easily switch from a development to production/staging environments without any additional configuration.

ERR_UNSAFE_REDIRECT

I'm trying to get my server to redirect to another page in my 'public folder'. When I use:
response.redirect(path.join(__dirname, '../public/user_home.html'))
I get and error net::ERR_UNSAFE_REDIRECT
On the client side I have:
$.get( "/user_home", function( data ) {console.log(data)};
I can't find anything about this error. Am I going about this incorrectly?
Your public folder is already available if you have static middleware configured in your app.
app.use(express.static('public'))
You can use:
res.redirect("/user_home.html");
response.redirect(path.join(__dirname, '../public/user_home.html'),safe=true)

how to get the mule application name

Is there some way of getting the base mule application name of a deployed mule application?
The only two options i have found so far is:
muleContext.getConfiguration().getId() //which gives me some not so humanly-readable id of some sort.
and
muleEvent.getFlowConstruct().getName() //gives me that flow name from where this was called.
Each application is in their own application directory when deployed, is it not possible to get a hold of this or some other similar distinguished name from within the muleContext?
kind regards
The simplest way to retrieve the application name is by injecting it into your component using the ${app.name} spring placeholder
This worked for me:
String appName = ((org.mule.module.launcher.MuleApplicationClassLoader)this.getClass().getClassLoader()).getAppName();
For Mule 4.x there are predefined variables and one of those is app.name to retrieve the application name in runtime. This is the link to the Mulesoft documentation to check all the predefined variables:
https://docs.mulesoft.com/mule-runtime/4.2/dataweave-variables-context
For MuleSoft 4.2.0 you can load application name with static Java function:
ClassLoader cl = Class.forName("full-name-holding-this").getClassLoader();
Method getArtifactDescriptor = cl.getClass().getMethod("getArtifactDescriptor");
Object artifactDescriptor = getArtifactDescriptor.invoke(cl);
Method getName = artifactDescriptor.getClass().getMethod("getName");
Object appName = getName.invoke(artifactDescriptor);
return appName.toString();