Implementing the FileUploadResource class

FileUploadResource is a resource designed to allow clients to upload files to a server through an asynchronous request using REST. This class uses the JAX-RS to create a REST resource and, depending on the file extension, this resource delegates the task of saving a file to its respective EJB. In the following code block, we have the FileUploadResource class: 

import javax.annotation.Resource;
import javax.enterprise.concurrent.ManagedExecutorService;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;

@Path("upload")
public class FileUploadResource {

@Resource
private ManagedExecutorService executor;

@Consumes("application/pdf")
@POST
public CompletionStage<String> uploadPdf(File file) {

BeanManager beanManager = getBeanManager();

CompletableFuture<String> completionStage = new
CompletableFuture<>();
executor.execute(() -> {
FileBean fileBean = new FileBean(file, "pdf");

Bean<PdfHandler> bean = (Bean)
beanManager.getBeans(PdfHandler.class).iterator().next();
CreationalContext cCtx =
beanManager.createCreationalContext(bean);
PdfHandler pdfHandler = (PdfHandler)
beanManager.getReference(bean, PdfHandler.class, cCtx);

try {

completionStage.complete(pdfHandler.handler( fileBean ));
} catch (IOException e) {
e.printStackTrace();
completionStage.completeExceptionally(e);
}

});

return completionStage;
}

private BeanManager getBeanManager(){
try {
// manual JNDI lookupCDI for the CDI bean manager (JSR 299)
return (BeanManager) new InitialContext().lookup(
"java:comp/BeanManager");

} catch (NamingException ex) {
throw new IllegalStateException(
"cannot perform JNDI lookup for CDI BeanManager");
}
}


@Consumes("image/jpeg")
@POST
public CompletionStage<String> uploadJpg(File file) throws
IOException {

BeanManager beanManager = getBeanManager();

CompletableFuture<String> completionStage = new
CompletableFuture<>();
executor.execute(() -> {
FileBean fileBean = new FileBean(file, "jpg");

Bean<JpgHandler> bean = (Bean)
beanManager.getBeans(JpgHandler.class).iterator().next();
CreationalContext cCtx =
beanManager.createCreationalContext(bean);
JpgHandler jpgHandler = (JpgHandler)
beanManager.getReference(bean, JpgHandler.class, cCtx);

try {

completionStage.complete(jpgHandler.handler( fileBean ));
} catch (IOException e) {
e.printStackTrace();
completionStage.completeExceptionally(e);
}

});

return completionStage;
}

@Consumes("application/zip")
@POST
public CompletionStage<String> uploadZip( File file) throws
IOException {

BeanManager beanManager = getBeanManager();

CompletableFuture<String> completionStage = new
CompletableFuture<>();
executor.execute(() -> {
FileBean fileBean = new FileBean(file, "zip");

Bean<ZipHandler> bean = (Bean)
beanManager.getBeans(ZipHandler.class).iterator().next();
CreationalContext cCtx =
beanManager.createCreationalContext(bean);
ZipHandler zipHandler = (ZipHandler)
beanManager.getReference(bean, ZipHandler.class, cCtx);

try {

completionStage.complete(zipHandler.handler( fileBean ));
} catch (IOException e) {
e.printStackTrace();
completionStage.completeExceptionally(e);
}

});

return completionStage;

}
}

In the preceding code block, we have the FileUploadResource class, which has a class injected named executor. The executor attribute has the ManagedExecutorService type, which is injected by @Resource of CDI. This attribute is used to create a new thread to execute a separated task. In the following code block, we have a code snippet of FileUploadResource, which has an executor attribute injection:

 @Resource
private ManagedExecutorService executor;

In addition, this class consists of three methods that receive a request, prepare the FileBean, and send it to an EJB to process a task that saves the files. In the following code block, we have the uploadPdf(File file) method, which is responsible for processing all the requests to upload PDF files:

    @Consumes("application/pdf")
@POST
public CompletionStage<String> uploadPdf(File file) {

BeanManager beanManager = getBeanManager();

CompletableFuture<String> completionStage = new
CompletableFuture<>();
executor.execute(() -> {
FileBean fileBean = new FileBean(file, "pdf");

//get the EJB by CDI
Bean<PdfHandler> bean = (Bean)
beanManager.getBeans(PdfHandler.class).iterator().next();
CreationalContext cCtx =
beanManager.createCreationalContext(bean);
PdfHandler pdfHandler = (PdfHandler)
beanManager.getReference(bean, PdfHandler.class, cCtx);

try {

completionStage.complete(pdfHandler.handler( fileBean ));
} catch (IOException e) {
e.printStackTrace();
completionStage.completeExceptionally(e);
}

});

return completionStage;

}

In the preceding code block, we have a FileUploadResource code snippet, which contains an example of the service for uploading PDFs provided by resources. This service works with asynchronous processing. To enable the method to work with an asynchronous method, we should either use AsyncResponse (@Suspended final AsyncResponse ar) as a parameter of the service method, or this method would need to return a CompletionState<T> to the client. In our example, we returned CompletionState to the client. Further, we need to create a separate thread in order to execute a nonblocking task. In the following code block, we have a code snippet of the uploadPdf method, which creates a separate task to call the task that saves the PDF file:

executor.execute(() -> {
FileBean fileBean = new FileBean(file, "pdf");

Bean<PdfHandler> bean = (Bean)
beanManager.getBeans(PdfHandler.class).iterator().next();
CreationalContext cCtx =
beanManager.createCreationalContext(bean);
PdfHandler pdfHandler = (PdfHandler)
beanManager.getReference(bean, PdfHandler.class, cCtx);

try {

completionStage.complete(pdfHandler.handler( fileBean ));
} catch (IOException e) {
e.printStackTrace();
completionStage.completeExceptionally(e);
}

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

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