Jersey resources mapping - spring

Is it possible do the following by using jersey?
<mvc:resources mapping="/css/**" location="/public/css/" />
<mvc:resources mapping="/js/**" location="/public/js/" />
<mvc:resources mapping="/views/**" location="/public/views/" />
So, i need to handle /css/some.css like /public/css/some.css
Thanks!

Jersey itself doesn't serve static resources, presumably you're integrating with Spring that should work already as long as you registered the dispatch servlet and you don't have a JAX-RS resource at the top level like #Path("/"), subset them to #Path("/services") for example.

Related

How to use dynamic values in URI endpoints in apache camel XML configuration

I am using apache camel to create routes between endpoints where through one URI (API Gateway) running on Tomcat on one port, I am mapping to another URI running on Tomcat on different domain and port.
<bean id="hostnameVerifier" class="org.apache.http.conn.ssl.AllowAllHostnameVerifier" />
...
<camel:sslContextParameters id="ssl">
<camel:keyManagers keyPassword="password">
<camel:keyStore ... />
</camel:keyManagers>
<camel:trustManagers>
<camel:keyStore ... />
</camel:trustManagers>
</camel:sslContextParameters>
....
<rest path="/MyService" consumes="application/json" produces="application/json">
<post uri="/login">
<description>Authenticate User</description>
<route streamCache="true">
<to
uri="https4://domain-b:9000/Auth/user/login?bridgeEndpoint=true&sslContextParametersRef=ssl&x509HostnameVerifier=hostnameVerifier" />
</route>
</post>
...
</rest>
Now as far as I am hardcoding the domain-b in my to endpoints, things are working fine. Problem comes when I have to dynamically fill that value from an input from some configuration file.
This is how I am trying to achieve the same -
<bean id="properties" class="org.apache.camel.component.properties.PropertiesComponent">
<property name="location" value="classpath:${LOCATION_PATH}propsfile.properties"/>
</bean>
The name of the properties key is "domain", now in my end points defintion I am writing the same as -
<to
uri="https4://${properties.domain}:9000/Auth/user/login?bridgeEndpoint=true&sslContextParametersRef=ssl&x509HostnameVerifier=hostnameVerifier" />
Basically after loading the properties in a bean named properties, I am trying to replace the domain-b with ${properties.domain} or #{properties.domain}, but does not seem to be working.
If anyone can suggest, in XML based config only, how can I read the URL domain from the properties file, that will be really awesome.
-AJ
You have to use property place holder to achieve dynamic uri the way you want.
For example:
<camelContext ...>
<propertyPlaceholder id="properties" location="YOUR_PROPERTY_FILE_LOCATION"/>
</camelContext>
And then try with
<to uri="https4://{{properties.domain}}:9000/.......>
Note: When you are configuring your property file using spring bean "PropertiesComponent", you have to use camel Property component inside your camel route to achieve dynamic value loading.
Since Camel 2.16
We can use
.from("file://path")
.toD("file://folder/${file:onlyname}")
We can use file

Moving mvc interceptor from module-context to web-application context

Can we move mvc interceptors from individual context file to web-application context.xml
Thus removing interceptor from individual module context?
You can put them in the web-context as follow:
<mvc:interceptors>
<bean class="com.package.TheInterceptor" />
<bean class="com.package.AnotherInterceptor" />
</mvc:interceptors>

Static static resources going through DispatcherServlet

In my Spring webapp I have annotated my dispatcher servlet like this:
<tx:annotation-driven />
<mvc:annotation-driven />
<context:annotation-config />
<context:component-scan base-package="com.myapp.package.controller" />
<mvc:resources location="/css/" mapping="/css/**" />
<mvc:resources location="/script/" mapping="/script/**" />
<mvc:resources location="/img/" mapping="/img/**" />
<mvc:resources location="/fonts/" mapping="/fonts/**" />
<!-- Uso de Tiles -->
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles-defs.xml</value>
</list>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" />
</bean>
Now by activating spring's debug mode on log4j.properties, I've checked that every resource (even static resources) goes through DispatcherServlet, which I don't even know if it's expected behavior. The fact is, by debugging the processRequest method I've checked that this method is somehow causing high memory consumption on serving each webpage, my guess being that every static resource is being held in memory by this method.
This is the log output for a static resource such as jquery script library:
14:14:23,569 DEBUG RequestMappingHandlerMapping:220 - Looking up handler method for path /script/jquery/jquery-1.9.1.min.js
14:14:23,575 DEBUG RequestMappingHandlerMapping:230 - Did not find handler method for [/script/jquery/jquery-1.9.1.min.js]
14:14:23,576 DEBUG SimpleUrlHandlerMapping:169 - Matching patterns for request [/script/jquery/jquery-1.9.1.min.js] are [/script/**]
14:14:23,576 DEBUG SimpleUrlHandlerMapping:194 - URI Template variables for request [/script/jquery/jquery-1.9.1.min.js] are {}
14:14:23,576 DEBUG SimpleUrlHandlerMapping:124 - Mapping [/script/jquery/jquery-1.9.1.min.js] to HandlerExecutionChain with handler [org.springframework.web.servlet.resource.ResourceHttpRequestHandler#101f75b5] and 1 interceptor
14:14:23,576 DEBUG DispatcherServlet:912 - Last-Modified value for [/myapp/script/jquery/jquery-1.9.1.min.js] is: -1
14:14:23,576 DEBUG ResourceHttpRequestHandler:173 - Trying relative path [jquery/jquery-1.9.1.min.js] against base location: ServletContext resource [/script/]
14:14:23,577 DEBUG ResourceHttpRequestHandler:178 - Found matching resource: ServletContext resource [/script/jquery/jquery-1.9.1.min.js]
14:14:23,577 DEBUG ResourceHttpRequestHandler:132 - Determined media type 'application/javascript' for ServletContext resource [/script/jquery/jquery-1.9.1.min.js]
As far as I can tell it's properly identifying it as a static resource.
Is my configuration right? Shouldn't static resources be served directly instead of going throuhg Dispatcher Servlet? Could it be that every resource is being kept in memory on serving it to the response?
<mvc:resources location="/css/" mapping="/css/**" />
means that the dispatcher servlet will, indeed, serve static resources located in /css for any URL starting with /css/. This is what allows serving static resources from the classpath, for example, instead of serving them from static files in the webapp.
If you don't want the Spring servlet to serve static resources, the do as the documentation indicates:
<mvc:default-servlet-handler/>
If you've done configured something with Spring, Spring will handle it.
When you provide a config with this element
<mvc:resources location="/css/" mapping="/css/**" />
Spring registers a SimpleUrlHandlerMapping bean with a mapping between the specified mapping and a ResourceHttpRequestHandler for the location.
The DispatcherServlet registers all HandlerMapping beans it finds in the ApplicationContext. One of these will be the SimpleUrlHandlerMapping above. If a request arrives which the SimpleUrlHandlerMapping can handle, the DispatcherServlet will use it. The SimpleUrlHandlerMapping will then delegate to the appropriate ResourceHttpRequestHandler which will serve the static resource.
Could it be that every resource is being kept in memory on serving it
to the response?
No, the ResourceHttpRequestHandler does not cache the content of the resource.

Spring MVC Static Resource Mapping

I have the following servlet mapping present -
<!-- Mapping Static Resources -->
<mvc:resources mapping="/css/**" location="/resources/css/" />
<mvc:resources mapping="/js/**" location="/resources/js/" />
<mvc:resources mapping="/images/**" location="/resources/images/" />
My image link in the html is "/images/folder/imageName.jpg" - These images get me a 404 whereas if the change the link to "/images/imageName.jpg" and move the image to directly under the images folder it gets me the image.
Do I need to modify my servlet mapping in any way to take into account the hierarchical structure?
You need to modify links to the images. When you write
<mvc:resources mapping="/images/**" location="/resources/images/" />
Then your HTTP requests to /resources/images are translated to webapp/images folder on the server. So in the html you should have something like this:
<img src="<spring:url value='/resources/images/logo.png'/>"

Spring 3 mvc:resources causing mvc:interceptors to run multiple times

in Spring 3 MVC dispather-servlet.xml with the configuration below, it seems like everytime a .js file is called the interceptor is kicked off.
<mvc:interceptors>
<bean class="com.something.SomeInterceptor" />
</mvc:interceptors>
<mvc:resources mapping="/js/**" location="/js/" />
<mvc:resources mapping="/jsp/**" location="/jsp/" />
My view/jsp calls four .js and the interceptor runs four times...
What is the proper way to set up the xml file so that this does not happen?
thanks
It's actually the browser that is requesting the JS files, so 4 HTTP requests are being made to your application. You'll need to use the "mapping" element of mvc:interceptor to select a subset of paths that the interceptor will be applied to. For example:
<mvc:interceptors>
<mvc:interceptor>
<mapping path="/secure/*"/>
<bean class="org.example.SecurityInterceptor" />
</mvc:interceptor>
</mvc:interceptors

Resources