The FIleUploadResource class

In the following code block, we have the JFileUploadResource class, which uses JAX-RS and is a REST service:

import javax.enterprise.event.Event;
import javax.enterprise.util.AnnotationLiteral;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import java.io.File;
import java.util.Objects;

@Path("upload")
public class FileUploadResource {

@Inject
Event<FileEvent> fileEvent;

@Consumes("application/pdf")
@POST
public Response uploadPdf(File file){

FileEvent fileEvent = new FileEvent(file, "pdf");

Event<FileEvent> pdfEvent = this.fileEvent.select(new
AnnotationLiteral<Pdf>() {});

pdfEvent.fireAsync(fileEvent)
.whenCompleteAsync((event, err)->{

if( Objects.isNull( err ) )
System.out.println("PDF saved");
else
err.printStackTrace();

});

return Response.ok().build();

}

@Consumes("image/jpeg")
@POST
public Response uploadJpg(File file){

FileEvent fileEvent = new FileEvent(file, "jpg");

Event<FileEvent> jpgEvent = this.fileEvent.select( new
AnnotationLiteral<Jpg>() {} );

jpgEvent.fireAsync(fileEvent)
.whenCompleteAsync((event, err)->{

if( Objects.isNull( err ) )
System.out.println( "JPG saved" );
else
err.printStackTrace();

});

return Response.ok().build();

}

@Consumes("application/zip")
@POST
public Response uploadZip( File file){

FileEvent fileEvent = new FileEvent( file, "zip" );

Event<FileEvent> zipEvent = this.fileEvent.select(new
AnnotationLiteral<Zip>() {});

zipEvent.fireAsync(fileEvent)
.whenCompleteAsync( (event, err)->{

if( Objects.isNull( err ) )
System.out.println( "PDF saved" );
else
err.printStackTrace();

});

return Response.ok().build();

}
}

The preceding code contains the uploadPdf(File file), uploadJpg(File file), and uploadZip(File file) methods that are called when a user wants to upload a file with PDF, JPG, or ZIP extensions, respectively. Furthermore, this class has the fileEvent attribute of the Event<FileEvent> typeEvent<FileEvent> is the class responsible for launching an event driven by a qualifier. In the following code, we have a code snippet that selects a correct Event, using an annotation as a qualifier:

Event<FileEvent> zipEvent = this.fileEvent.select(new AnnotationLiteral<Zip>() {});

Another way to establish the correct event to launch is to use the qualifier at the point where the object is injected using @Inject, but this way, the event becomes static and all events launched by the Event object are of the same type. Using the select (Annotation... var) method, we can launch a dynamic event as well as other event types. The following is an example of Event with a static event type:

@Inject
@Pdf //Qualifier
Event<FileEvent> pdfEvent;

In the preceding example, pdfEvent will always launch an event to an observer that processes an event marked by the @Pdf qualifier.

To launch an asynchronous event, we need to call the fireAsync(U var) method, which returns CompletionStage. In the following code block, we have a snippet that calls this method and prepares a callback function to execute when the process is complete:

 zipEvent.fireAsync(fileEvent)
.whenCompleteAsync( (event, err)->{

if( Objects.isNull( err ) )
System.out.println( "PDF saved" );
else
err.printStackTrace();

});
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.221.133