0

How would one handle consuming an attachment from a client POST/PUT request on the server side and store that file in a local folder, all using Restlet ?

My thoughts are as follows:

Setup Server as follows:

new MailServerComponenet.start();

public MailServer(){
    getServers().add(Protocol.HTTP, 8111);
    getDefaultHost().attachDefault(new MailServer());
    server.getContext().getParameters().set("tracing", "true");
}

@Put
public void store(Form form){
    // *And here is where I am not sure*
}

Thanks for any insight and help in advance.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Neutron_boy
  • 919
  • 1
  • 6
  • 7

1 Answers1

0

Here are the following steps you should follow to implement your Restlet application:

  • Create a component

    Component component = new Component();
    
  • Create an application and attach it on the component

    component.getServers().add(Protocol.HTTP, 8182);
    
    Application application = new MyApplication();
    component.getDefaultHost().attachDefault(application);
    
    component.start();
    
  • Configure the application within the method createInboundRoot (create a router and attach server resource on it)

    public class MyApplication extends Application {
        @Override
        public Restlet createInboundRoot() {
            Router router = new Router();
            router.attach("/test", MyServerResource.class);
            return router;
        }
    }
    
  • Implement the server resources

    public class MyServerResource extends ServerResource {
        @Post
        public Representation handlePost(Representation repr) {
            (...)
        }
    }
    

Now the global frame is implemented, I would wonder how you would send the content to Restlet. Is it simple binary content within the request payload or multi-part content?

  • Binary content

    @Post
    public Representation handlePost(FileRepresentation fileRepr) {
        fileRepr.write(new FileOutputStream(
                   new File("/tmp/myfile.txt"))); 
        return null;
    }
    
  • Multipart content. You can have a look at this answer in this case: File Upload with Description in Restlet

Hope it helps you, Thierry

Community
  • 1
  • 1
Thierry Templier
  • 198,364
  • 44
  • 396
  • 360