Skip to content
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

Closed
YounesRahimi opened this issue Jul 2, 2019 · 5 comments
Closed

Add Support for Template Engines #44

YounesRahimi opened this issue Jul 2, 2019 · 5 comments

Comments

@YounesRahimi
Copy link
Contributor

YounesRahimi commented Jul 2, 2019

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.

@drejc
Copy link
Member

drejc commented Jul 2, 2019

Not quite sure what you have in mind ... can you illustrate a simple use case

@YounesRahimi
Copy link
Contributor Author

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.

@drejc
Copy link
Member

drejc commented Jul 3, 2019

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 ...
Rest.vertx already provides a default implementation out of the box, but you can create your own.

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 /resources folder.

So for instance if you put in the /resources/some_folder/myfile.txt the call to:
GET /docs/myfile.txt will serve /resource/some_folder/myfile.txt if present ... or return 404 not found.

@YounesRahimi
Copy link
Contributor Author

OK. What if we want dynamic file contents like passing variables to file writer to render the file?

@drejc
Copy link
Member

drejc commented Jul 3, 2019

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);
	}
}

@drejc drejc closed this as completed Jul 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants