| Line 1... |
Line 1... |
| 1 |
package com.spice.profitmandi.common.web.util;
|
1 |
package com.spice.profitmandi.common.web.util;
|
| 2 |
|
2 |
|
| 3 |
import java.io.File;
|
- |
|
| 4 |
import java.nio.file.Files;
|
- |
|
| 5 |
import java.util.Map;
|
- |
|
| 6 |
|
- |
|
| 7 |
import javax.servlet.http.HttpServletRequest;
|
- |
|
| 8 |
import javax.servlet.http.HttpServletResponse;
|
- |
|
| 9 |
|
- |
|
| 10 |
import com.fasterxml.jackson.core.JsonProcessingException;
|
3 |
import com.fasterxml.jackson.core.JsonProcessingException;
|
| - |
|
4 |
import com.fasterxml.jackson.databind.ObjectMapper;
|
| - |
|
5 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| - |
|
6 |
import com.spice.profitmandi.common.model.ProfitMandiResponse;
|
| - |
|
7 |
import com.spice.profitmandi.common.model.Response;
|
| - |
|
8 |
import com.spice.profitmandi.common.model.ResponseStatus;
|
| 11 |
import org.springframework.beans.factory.annotation.Autowired;
|
9 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 12 |
import org.springframework.http.HttpStatus;
|
10 |
import org.springframework.http.HttpStatus;
|
| 13 |
import org.springframework.http.MediaType;
|
11 |
import org.springframework.http.MediaType;
|
| 14 |
import org.springframework.http.ResponseEntity;
|
12 |
import org.springframework.http.ResponseEntity;
|
| 15 |
import org.springframework.stereotype.Component;
|
13 |
import org.springframework.stereotype.Component;
|
| 16 |
|
14 |
|
| 17 |
import com.fasterxml.jackson.databind.ObjectMapper;
|
15 |
import javax.servlet.http.HttpServletRequest;
|
| 18 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
16 |
import javax.servlet.http.HttpServletResponse;
|
| 19 |
import com.spice.profitmandi.common.model.ProfitMandiResponse;
|
17 |
import java.io.File;
|
| 20 |
import com.spice.profitmandi.common.model.Response;
|
18 |
import java.nio.file.Files;
|
| 21 |
import com.spice.profitmandi.common.model.ResponseStatus;
|
19 |
import java.util.Map;
|
| 22 |
|
20 |
|
| 23 |
@Component
|
21 |
@Component
|
| 24 |
public class ResponseSender<T> {
|
22 |
public class ResponseSender<T> {
|
| 25 |
|
23 |
|
| 26 |
|
24 |
|
| Line 121... |
Line 119... |
| 121 |
}
|
119 |
}
|
| 122 |
|
120 |
|
| 123 |
public void writeNotFound(HttpServletRequest request, HttpServletResponse response, Response failedResponse) throws Exception{
|
121 |
public void writeNotFound(HttpServletRequest request, HttpServletResponse response, Response failedResponse) throws Exception{
|
| 124 |
this.writeGenericRequest(request, response, failedResponse, HttpStatus.NOT_FOUND);
|
122 |
this.writeGenericRequest(request, response, failedResponse, HttpStatus.NOT_FOUND);
|
| 125 |
}
|
123 |
}
|
| - |
|
124 |
|
| - |
|
125 |
private void addCorsHeaders(HttpServletRequest request, HttpServletResponse response) {
|
| - |
|
126 |
String origin = request.getHeader("Origin");
|
| - |
|
127 |
if (origin != null && !origin.isEmpty()) {
|
| - |
|
128 |
response.setHeader("Access-Control-Allow-Origin", origin);
|
| - |
|
129 |
response.setHeader("Access-Control-Allow-Credentials", "true");
|
| - |
|
130 |
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT, PATCH");
|
| - |
|
131 |
response.setHeader("Access-Control-Allow-Headers",
|
| - |
|
132 |
"Content-Type, Auth-Token, Authorization, X-Requested-With, Idempotencykey");
|
| - |
|
133 |
response.setHeader("Access-Control-Expose-Headers", "Auth-Token, Content-Type, Authorization");
|
| - |
|
134 |
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
|
| - |
|
135 |
response.setHeader("Pragma", "no-cache");
|
| - |
|
136 |
response.setHeader("Expires", "0");
|
| - |
|
137 |
}
|
| - |
|
138 |
}
|
| - |
|
139 |
|
| 126 |
private void writeGenericRequest(HttpServletRequest request, HttpServletResponse response, Response failedResponse, HttpStatus httpStatus) throws Exception{
|
140 |
private void writeGenericRequest(HttpServletRequest request, HttpServletResponse response,
|
| - |
|
141 |
Response failedResponse, HttpStatus httpStatus) throws Exception {
|
| - |
|
142 |
this.addCorsHeaders(request, response);
|
| - |
|
143 |
|
| 127 |
response.setStatus(httpStatus.value());
|
144 |
response.setStatus(httpStatus.value());
|
| 128 |
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
|
145 |
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
|
| 129 |
response.setCharacterEncoding("UTF-8");
|
146 |
response.setCharacterEncoding("UTF-8");
|
| - |
|
147 |
final ProfitMandiResponse<?> profitMandiResponse = new ProfitMandiResponse<>(
|
| 130 |
final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(httpStatus.toString(), httpStatus, ResponseStatus.FAILURE, failedResponse);
|
148 |
httpStatus.toString(), httpStatus, ResponseStatus.FAILURE, failedResponse);
|
| 131 |
final Object obj = objectMapper.readValue(objectMapper.writeValueAsString(profitMandiResponse), Object.class);
|
149 |
final Object obj = objectMapper.readValue(objectMapper.writeValueAsString(profitMandiResponse), Object.class);
|
| 132 |
final String jsonString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
|
150 |
final String jsonString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
|
| 133 |
response.getWriter().write(jsonString.toString());
|
151 |
response.getWriter().write(jsonString.toString());
|
| 134 |
response.getWriter().flush();
|
152 |
response.getWriter().flush();
|
| 135 |
}
|
153 |
}
|
| 136 |
|
154 |
|
| 137 |
}
|
155 |
}
|