官术网_书友最值得收藏!

Implementing DownloadApplicationController

Here, we have the implementation of DownloadApplicationController, which is responsible for deciding the correct command to send the request. The process for deciding the correct command can be carried out in several ways, with reflections and annotations, using switch cases and maps, among others. In our example, we use a map to help us:

import com.rhuan.action.Command.AbstractCommand;
import com.rhuan.action.Command.PdfCommand;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class DownloadApplicationController {

private static Logger logger = LogManager.getLogger(DownloadApplicationController.class);

private final String PAGE_ERROR = "/pageError.jsp";

private HttpServletRequest request;

private HttpServletResponse response;

private Map<String, Class> map;

private String key;

public DownloadApplicationController(HttpServletRequest
request, HttpServletResponse
response){

//On our example, only PDF and JPG is acepted to download.
this.map = new HashMap<String, Class>();
this.map.put("PDF", PdfCommand.class);
this.map.put("JPG", PdfCommand.class);

this.request = request;
this.response = response;

}

public void process(){

//Processes the URI and creates the key using URI.
this.processUri();

//Validates if the request is valid.
if (!validate()) {
try {
response.sendError(400);
} catch (IOException e1) {
logger.error(e1.getMessage());
}

return;
}

//Get the correspondent command.
Class commandClass = map.get(key);

boolean error = false;

try {

AbstractCommand command = (AbstractCommand)
commandClass.newInstance();

//Executes the command.
command.execute(request,response);

} catch (InstantiationException e) {
logger.error(e.getMessage());
error = true;

} catch (IllegalAccessException e) {
logger.error(e.getMessage());
error = true;

} catch (ServletException e) {
logger.error(e.getMessage());
error = true;
} catch (IOException e) {
logger.error(e.getMessage());
error = true;
}


//If an error ocorred, response 500.
if(error){
try {
response.sendError(500);

} catch (IOException e1) {
logger.error(e1.getMessage());
return;
}
}


}

private void processUri(){

String uri = request.getRequestURI();
if(uri.startsWith("/")) uri = uri.replaceFirst("/", "");
String[] uriSplitted = uri.split("/");

if(uriSplitted.length > 2)
key = uriSplitted[2].toUpperCase();
}



private boolean validate(){

String uri = request.getRequestURI();
if(uri.startsWith("/")) uri = uri.replaceFirst("/", "");
String[] uriSplitted = uri.split("/");

return uriSplitted.length == 3 && map.containsKey(key);
}

}

In the preceding code, we created two methods to help us to process and validate the URI, the processUri method, and the validated method. After processing the URI, validating the request and generating the key, the code finds the correct command using the key generated. In the following code snippet, we have the line of code that gets the command class by keycreates a new instance of the command, and executes the following command:

//Get the correspondent command.
Class commandClass = map.get(key);

A new instance of the command is then created using the newInstance() method :

//Instantiate the command    
AbstractCommand command = (AbstractCommand) commandClass.newInstance();

The command is then executed, passing request and response as parameters:

//Executes the command.
command.execute(request,response);
主站蜘蛛池模板: 白水县| 台安县| 惠安县| 天台县| 武山县| 祁连县| 宜丰县| 福贡县| 江阴市| 邹平县| 米林县| 伊春市| 定南县| 大埔县| 岗巴县| 伊宁县| 阿图什市| 三明市| 锦州市| 辽阳市| 鞍山市| 平山县| 色达县| 吴川市| 湘乡市| 灌云县| 杭锦后旗| 涿鹿县| 凤冈县| 进贤县| 娱乐| 来凤县| 新建县| 中西区| 嘉禾县| 丽水市| 洛川县| 盐源县| 马公市| 鹤峰县| 宜兰市|