Code Monkey home page Code Monkey logo

Comments (5)

drejc avatar drejc commented on May 20, 2024

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

from rest.vertx.

YounesRahimi avatar YounesRahimi commented on May 20, 2024

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.

from rest.vertx.

drejc avatar drejc commented on May 20, 2024

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.

from rest.vertx.

YounesRahimi avatar YounesRahimi commented on May 20, 2024

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

from rest.vertx.

drejc avatar drejc commented on May 20, 2024

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

from rest.vertx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.