debugjavaMinor
Servlet responses to upload/download requests
Viewed 0 times
uploadresponsesservletrequestsdownload
Problem
My task is to write a program to store files in object store. One of the subtasks requires to send json or xml format response to the file uploader/downloader as requested by client.
The following rules must be followed:
I've managed to solve the task but I'm not satisfied with the class dependencies and nested
My code snippet:
```
public Result upload(HttpServletRequest request) {
UploadRequestParams uploadParams = UploadRequestParams.getObjectFrom(request);
... // create info object
Result res = handleUploadRequest(uploadParams);
Result result = jsonXml(info, res);
return result;
}
private Result jsonXml(RequestInfo info, Result result) {
Serializer ser;
if ("xml".equals(format)) {
ser = new XmlSerializer();
} else {
ser = new JsonSerializer();
}
Result res = ser.serialize(info, result);
return res;
}
@Override
public Result serialize(RequestInfo info, Result result) { // json implementation
GsonBuild
The following rules must be followed:
entry
+-------------------------+
v v
upload download
+-----------+ +--------------+
v v v v
succ err succ err
+-----+ +-----+ +-----+
v v v v v v
json1 xml1 json2 xml2 josn2 xml2- if user uploads file with success, return json/xml type 1
- if user uploads file with error, return json/xml type 2
- if user downloads file with success, don't change return object
- if user downloads file with error, return json/xml type 2
I've managed to solve the task but I'm not satisfied with the class dependencies and nested
if/else-s I have. I think mixture of abstract factory and strategy are suitable for the issue. Can you give me a hint on how I can deal with the problem?My code snippet:
```
public Result upload(HttpServletRequest request) {
UploadRequestParams uploadParams = UploadRequestParams.getObjectFrom(request);
... // create info object
Result res = handleUploadRequest(uploadParams);
Result result = jsonXml(info, res);
return result;
}
private Result jsonXml(RequestInfo info, Result result) {
Serializer ser;
if ("xml".equals(format)) {
ser = new XmlSerializer();
} else {
ser = new JsonSerializer();
}
Result res = ser.serialize(info, result);
return res;
}
@Override
public Result serialize(RequestInfo info, Result result) { // json implementation
GsonBuild
Solution
if (info.isUpload()) {
if (result.isValid()) {
wrap = new InfoWrapperImpl1(info, result);
} else {
wrap = new InfoWrapperImpl2(info, result);
}
} else {
wrap = new InfoWrapperImpl1(info, result);
}Can be written as:
if (!info.isUpload() || result.isValid()) {
wrap = new InfoWrapperImpl1(info, result);
} else {
wrap = new InfoWrapperImpl2(info, result);
}Also
InfoWrapperImpl1 and InfoWrapperImpl2 are not very good names, as they are not expressive and look quite similar to each other.Code Snippets
if (info.isUpload()) {
if (result.isValid()) {
wrap = new InfoWrapperImpl1(info, result);
} else {
wrap = new InfoWrapperImpl2(info, result);
}
} else {
wrap = new InfoWrapperImpl1(info, result);
}if (!info.isUpload() || result.isValid()) {
wrap = new InfoWrapperImpl1(info, result);
} else {
wrap = new InfoWrapperImpl2(info, result);
}Context
StackExchange Code Review Q#82569, answer score: 3
Revisions (0)
No revisions yet.