DropWizard Serving Assets help needed - java

Help! I've been trying for hours, googling anything I could think of. I have a problem, that I would like to show my static content instead of my application on my site.
I modified a simple hello-world application:
public static void main(String[] args) throws Exception {
Class.forName("org.sqlite.JDBC");
new HelloWorldApplication().run(args);
}
#Override
public String getName() {
return "hello-world";
}
#Override
public void initialize(Bootstrap<HelloWorldConfiguration> bootstrap) {
bootstrap.addBundle(new AssetsBundle("/assets/*", "/"));
}
#Override
public void run(HelloWorldConfiguration configuration, Environment environment) {
final HelloWorldResource resource = new HelloWorldResource(
configuration.getTemplate(),
configuration.getDefaultName()
);
final AddResource addResource = new AddResource();
final DeleteResource deleteResource = new DeleteResource();
final TemplateHealthCheck healthCheck = new TemplateHealthCheck(configuration.getTemplate());
environment.healthChecks().register("template", healthCheck);
environment.jersey().register(resource);
environment.jersey().register(addResource);
environment.jersey().register(deleteResource);
}
Here's my hello-world.yml:
server:
type: simple
applicationContextPath: /application/hello-world
template: Hello, %s!
defaultName: Stranger
I applied everything, what the DropWizard docs (http://dropwizard.readthedocs.org/en/latest/manual/core.html#serving-assets) said. But I just cannot manage to reach the index.html

I have not seen an actual example that proves that the documented way actually works.
And when looking in the Dropwizard source, I conclude that this is in fact not possible: the Jetty application context is set by the configuration parameter applicationContextPath in SimpleServerFactory:103:
environment.getApplicationContext().setContextPath(applicationContextPath);
And after that, the AssetBundles are registered into this applicationContext upon run() (AssetBundle:109):
environment.servlets().addServlet(assetsName, createServlet()).addMapping(uriPath + '*');
So, assetbundles are always served within the applicationContextPath that is set in the application's YAML file, so serving them outside this applicationContextPath is not possible (despite the docs saying so)
A better way to get this working, is to configure the application to use the / path:
applicationContextPath: /
And then, in your application's code, in the bootstrap() and run() methods, explicitly override the path for Jersey resources and add AssetBundles to your liking:
bootstrap.addBundle(new AssetsBundle("/static", "/"));
environment.jersey().setUrlPattern("/application/*");

I got it working by using the default constructor for the AssetsBundle() class.
With the default constructor your resources will gets looked up in a directory on the java classpath e.g.
/src/main/resources/assets/
and your have to name your applicationContextPath only /application
Point your browser to the folling location for static content
localhost:8080/application/assets/index.htm

For Dropwizard 0.8.0 and newer this is achieved from this configuration:
applicationContextPath: /
rootPath: /application
Where applicationContextPath is Jetty's Context path, and rootPath is Jersey's.

As Geert mentioned, the asset bundle needs to be served from within the applicationContextPath. However, if you add the AssetsBundle in the bootstrap method, and set the contextPath from the run method, the AssetServlet gets added after the contextPath is set.
My fix is to avoid using the AssetsBundle and add the AssetsServlet directly in the run method (after contextPath is set):
environment.getApplicationContext().setContextPath("/");
environment.servlets().addServlet("assets", new AssetServlet("/assets", "/", "index.html", StandardCharsets.UTF_8)).addMapping("/*");

Related

How to configure hot reload in Jhipster?

I am using Jhipster(Angular + Springboot) Application for my existing project.
I managed to create a controller(app.resource) manually apart from the ones already generated by jhiptser(using .jh file) for achieving a file download functionality.
So, when we start the server we usually initiate two servers i.e gradlew and npm start. The second runs on port 9000 which eventually supports hot reload functionality.(front-end development)
So the problem is, I am able to access those endpoints from the server running on standard 8000 port. However, from the port which is a proxy(9000), the method is returning 404.
I tried to clean build the application several times.
NOTE: The #RequestMapping value on the new controller is different then those present already.
Does this have to do something with spring security?
Thanks in advance.
Here is the previous controller:
#RestController
#RequestMapping("/api")
public class FGAppDiagramResource {
#GetMapping(value = "/fg-app-diagram-downloadFile")
public void getImage(String fileName,String folderName, HttpServletResponse
response){
// Some Code
}
}
Here is my New controller:
#RestController
#RequestMapping("/fileDownload")
public class DownloadFileController {
private final Logger log =
LoggerFactory.getLogger(DownloadFileController.class);
public DownloadFileController() {
super();
}
#Autowired
private ApplicationProperties applicationProperties;
#GetMapping(value = "/fg-app-diagram-downloadFile/{fileName}/{folderName}")
public void getImage(#PathVariable String fileName,#PathVariable String folderName, HttpServletResponse response) {
// Some Code
}
}
Your new controller does not use /api so you must add your endpoint URL /fileDownload to proxy configuration of webpack dev server in webpack/webpack.dev.js
proxy: [{
context: [
/* jhipster-needle-add-entity-to-webpack - JHipster will add entity api paths here */
'/api',
'/fileDownload',
You may want to use /api/fileDownload to avoid changing proxy configuration and also because /api is useful for many other aspects like security and also using HTML5 URL routing strategy in Angular to get rid of # in client routes (see https://github.com/jhipster/generator-jhipster/pull/9098).
/api and /management are namespaces to avoid route conflicts, so it is usually wise to use them for your new endpoints.

Spring Boot resource handler patterns for single page applications

I have the following in my Spring Boot configuration:
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.setOrder(Ordered.HIGHEST_PRECEDENCE)
.addResourceHandler("**/*.*")
.addResourceLocations("file:static/");
}
Since I'm creating a single page app I want this resource handler to deliver static files if the following is true:
URL does not start with /api
This resource handler contains a file that matches the path from the URL
If there are no matching file, the resource handler should not respond with 404 but let the call through to the controllers of the project.
In one of them I place my fallback to index.html annotation (again unless the URL starts with /api):
#GetMapping({"/**", "/"})
This last part works fine but the problem is that the resource handler returns a 404 if there are no matching files instead of letting the call through (I'm used to the middleware patterns of Node.js and Go) to the controllers. How can I achieve this? Can I create my own resource handler that acts more middleware like?
In Go with Echo for example you just need to enable HTML5 with a skipper function for /api:
func main() {
server := echo.New()
server.Use(middleware.Static("static"))
server.Use(middleware.StaticWithConfig(middleware.StaticConfig{
Root: "static",
HTML5: true,
Skipper: noHTML5IfAPICallSkipper,
}))
server.Logger.Fatal(server.Start(":1323"))
}
func noHTML5IfAPICallSkipper(context echo.Context) bool {
if strings.HasPrefix(context.Path(), "/api/") {
return true
}
return false
}
How to enable this single page app routing patterns in Spring Boot?
Oh, and by the way, it's crucial that the static files are served from disk rather than from a classpath or anything else that is packaged inside a jar. This service will run with Docker and it has to be possible to mount the files from outside the container.
Try with this:
public class Application extends WebMvcConfigurerAdapter { // or implements WebMvcConfigurer (Spring Boot 2)
#Value("${static.path}")
private String staticPath;
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**/*.*").addResourceLocations("file:"+staticPath);
}
#Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("/index.html");
}
...
}
You must to add to application.properties file the line:
`static.path=path/to/static/content`
If you want to use Docker, this path need to reference to internal docker container directory, an example: /opt/app/statics and then, map the external path to this:
You must to add to application.properties file the line: static.path=/opt/app/statics
And execute docker like this:
docker run ... -v /absolute/static/path/at/host:/opt/app/statics image:version

Wildfly Swarm RESTeasy hides webapp/index.html

I am working on a project based on Wildfly Swarm. The problem I currently have is that RESTeasy hides my index.html (and other html files) which are placed below /webapp since RESTeasy is listening on root level.
My Main Application:
#ApplicationPath("/")
public class XYZnlineApplication extends Application {
}
One of my resources:
#Path("protected/api/admin")
public class AdminResource {
#GET
#Path("public/api/offer/reduced")
#Produces("application/json")
public List<XYZ> getXYZ() {
...
}
#GET
#Path("protected/api/offer/full")
#Produces("application/json")
public List<XYZ> getAllXYZ() {
...
}
}
The thing is. If I start my wildfly swarm app and access one of the restendpoint above, everything works fine (e.g. http://localhost:8080/app/public/api/offer/reduced)
But if I d'like to access one of my html (e.g. login.html) files which are directly below /webapp, I get a 404 although the file is bundled correctly (e.g. on trying to access http://localhost:8080/app/login.html). So in my opinion what happens is that RESTeasy hides this html file cause it listens on root (/).
Since the first part of my url is the context (which is injected by a proxy) I can't set anything else than root (/) as ApplicationPath in my XYZApplication.
Do you have any idea on how I could solve this issue?
Thanks a lot in advance for your help.
You'll need to change the ApplicationPath to be something like "/services" or "/svc" or whatever works for you. Ultimately you need to partition the URL namespace between static resources and services. You don't need to worry about the context when specifying the ApplicationPath.
Major Edit:
Your comment really explains what's going on. I'm not sure what type of security you're using exactly but ultimately you likely need to have a filter of some sort in front of your services. I would have something like:
#Provider
#Priority(Priorities.AUTHENTICATION)
#PreMatching
public class AuthFilter implements ContainerRequestFilter {
#Context
private HttpServletRequest httpServletRequest;
#Context
private HttpServletResponse httpServletResponse;
#Override
public void filter(ContainerRequestContext containerRequestContext) throws IOException {
if( containerRequestContext.getUriInfo().getPath().contains("/public/") )
return; // user is in public area - doesn't matter if they are authenticated
// guess at how to check if user is authenticated
if( httpServletRequest.getSession().get("user_is_ok") )
return;
httpServletResponse.sendRedirect("/login");
// or maybe
httpServletResponse.sendError(SC_UNAUTHORIZED);
}
}
Again, this is a bit of a guess but this is a pretty common way of handling your challenge.

Spring MVC 1 to 1 mapping of a URL to .HTML without exposing path

I am using Spring 4.1.5 with Boot 1.2 on a webservice that does not serve up any JSPs. I don't want to add a JSP servlet but I want it to serve up a single canary page that shows in a prettier html type format the information that would be provided at the /manage/health endpoint.
I have a file in webapp/canary/canary.html I want to serve this up from the url: www.mywebservice.com:9343/canary, exactly like that, NOT canary.html
I tried doing this:
#Configuration
public class CanaryConfiguration extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/canary")
.addResourceLocations("/canary/canary.html");
}
}
That doesn't work however.
It is expecting the handler to provide a file name. So in otherwords the location should be something like: /canary/
and the handler would something like: /canary/**
With that, the URL www.mywebservice.com:9343/canary/canary.html would work like a charm.
HOWEVER, I want the URL to resolve www.mywebservice.com:9343/canary to webapp/canary/canary.html without me having to type the html.
This is really easy in a jsp servlet because you can set the suffix ect...
I looked at ResourceResolver but it didn't make sense to me how I would link that into my current configuration.
It looks like what I want:
Provides mechanisms for resolving an incoming request to an actual Resource and for obtaining the public URL path that clients should use when requesting the resource.
See: ResourceResolver Documentation
Any help would be very beneficial.
Also I am very aware that I can put html in the resources/static and several other places that are automatically configured. That always requires the .html to be typed, which is not what I want in this case so that won't work. Thanks!
You can use view controllers to do it. Here is a sample of it. Hope this helps.
public class AppConfig extends WebMvcConfigurerAdapter {
#Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/canary").setViewName("/canary/canary.html");
}
}
Note: if you are using tomcat, you might have to configure jsp servlet to server html files.
Related post here.
For information sake, the selected answer is the same as the following:
#Controller
public class CanaryController {
#RequestMapping(value="/canary", method=RequestMethod.GET)
public String getCanary() {
return "/canary/canary.html";
}
}
The above code will work as long as canary(or whatever file/folder) is in your webapp folder.
When I tried this I was trying to set the suffix to .html in my YAML (.yml) file and it wasn't working to I thought that it needed to return to a servlet if it is not a RestController. I was mistaken.

Grizzly Server with static content and REST resource

I have a Jersey REST 2.5.1 service which is served through a Grizzly server. Everything works fine so far. I want to add some static content, which is also served through Grizzly and provided from my JAR file. Therefore I use CLStaticHttpHandler. When I access static resources, such as my index.html explicitly (e.g. http://localhost:8080/index.html), everything works fine. However, when I try to access the root http://localhost:8080, I get a 404. The code looks like this:
ObjectMapper mapper = new ObjectMapper();
// some configuration stuff here
JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider();
provider.setMapper(mapper);
ResourceConfig resourceConfig = new ResourceConfig()
.packages("my.restapi.package")
.register(provider);
HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), resourceConfig);
HttpHandler httpHandler = new CLStaticHttpHandler(HttpServer.class.getClassLoader(), "/static/");
httpServer.getServerConfiguration().addHttpHandler(httpHandler, "/");
As far as I can tell from debugging, org.glassfish.grizzly.http.server.CLStaticHttpHandler.handle(String, Request, Response) never gets called. Any hints, how I can make the index.html accessible as default page?
After some wasted hours, I feel a bit stupid now, but the simple solution was, to specify a path in BASE_URI (http://localhost:8080/api/ instead of http://localhost:8080/). Now, when accessing /, I get the index.html and the REST methods are under /api.

Categories

Resources