-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Support for Template Engines #44
Comments
Not quite sure what you have in mind ... can you illustrate a simple use case |
If you want to redirect a user to another external URL you have two options for doing this. Option one is using HTTP redirect status codes which are 3xx codes. The limitation of this option is that you can't redirect to a URL with a different body (payload) and different content type. Option two is using template engines to render an HTML page with Javascript and then redirect the user using Javascript with the customized request body and content type. |
I think what you are after is static file serving. To achieve this you simply need to set a dedicated "file" writer to a REST endpoint ... For instance the following endpoint: @Path("docs")
public class ServeStaticFileRest {
@GET
@Path("/{path:.*}")
@ResponseWriter(FileResponseWriter.class)
public String serveDocFile(@PathParam("path") String path) {
return "some_folder/" + path;
}
} Will return a "path" as a String. This path is then used by the FileResponseWriter to find and return the contents of the desired file from So for instance if you put in the |
OK. What if we want dynamic file contents like passing variables to file writer to render the file? |
Then you will need to create your own writer / custom implementation ... Just for illustration ... (NOT WORKING CODE): @Path("docs")
public class ServeStaticFileRest {
@GET
@Path("/{path:.*}")
@ResponseWriter(MyFileResponseWriter.class)
public MyFileDesire serveDocFile(@PathParam("path") String path, @QueryParam("size") int size) {
return new MyFileDesire(path, size)
}
} The writer would then look something like this: public class MyFileResponseWriter implements HttpResponseWriter<MyFileDesire> {
@Context
RoutingContext context;
@Override
public void write(MyFileDesire desire, HttpServerRequest request, HttpServerResponse response) throws FileNotFoundException {
if (!fileExists(context, desire.path)) {
throw new FileNotFoundException(path);
}
// create custom output ... of your liking ...
MyFile file = getFromDesire(desire)
// write to output
response.putHeader("Content-Length", String.valueOf(file.size()));
response.write(Buffer.buffer(file.toByteArray()));
response.end();
}
protected boolean fileExists(RoutingContext context, String file) {
FileSystem fs = context.vertx().fileSystem();
return fs.existsBlocking(file);
}
} |
In some cases, we need to use template engines even in Rest services. For example, if we want to redirect the user to a third-party server with
x-www-form-urlencoded
body payload we need to use Javascript for doing this.The text was updated successfully, but these errors were encountered: