Java EE 8 Design Patterns and Best Practices
上QQ阅读APP看书,第一时间看更新

Implementing commands

Here, we have the abstract class AbstractCommand, which includes the abstract execute method. All implementations of this command extend AbstractCommand, which is an abstract class:

package com.rhuan.action.Command;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public abstract class AbstractCommand {

public abstract void execute(HttpServletRequest
request, HttpServletResponse response)
throws ServletException, java.io.IOException ;
}

In the following block of code, we have the PdfCommand class. This is a subclass of AbstractCommand that implements the logic to download a PDF file:

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

public class PdfCommand extends AbstractCommand {


@Override
public void execute(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {

String fileName = request.getParameter("fileName");

// for example application/pdf, text/plain, text/html,
image/jpg
response.setContentType("application/pdf");

// Make sure to show the download dialog
response.setHeader("Content-disposition","attachment;
filename=myapp_download.pdf"
);

// Assume file name is retrieved from database
// For example D:\\file\\test.pdf

File file = new File(fileName);

// This should send the file to browser
OutputStream out = response.getOutputStream();
FileInputStream in = new FileInputStream(file);

byte[] buffer = new byte[4096];
int length;
while ((length = in.read(buffer)) > 0){
out.write(buffer, 0, length);
}
in.close();
out.flush();

}
}

In the preceding code block, we have the execute() method, which processes the logic to download a PDF file. At this point, all the processes and main validations of requests were executed, and the execute() method needs only to execute the download process.

Here, we have the JpgCommand class, which is a subclass of AbstractCommand that implements the logic to download a JPG file:

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

public class JpgCommand extends AbstractCommand {

@Override
public void execute(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {

//Gets the file name sent by paramenter.
String fileName = request.getParameter("fileName");

//Configures the content type.
response.setContentType("image/jpg");

// Configure the dialog to download.
response.setHeader("Content-disposition","attachment;
filename=myapp_download.pdf"
);

//Read file and send to client.
File file = new File(fileName);
OutputStream out = response.getOutputStream();
FileInputStream in = new FileInputStream(file);

byte[] buffer = new byte[4096];
int length;
while ((length = in.read(buffer)) > 0){
out.write(buffer, 0, length);
}
in.close();
out.flush();

}
}

In the preceding code block, we have the execute() method, which processes the logic to download a JPG file. At this point, all request and main validation processes have already been done.