import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
-
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
-
-
import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
import org.onap.so.logger.ErrorCode;
import org.onap.so.logger.MessageEnum;
-
import org.onap.so.serviceinstancebeans.RequestError;
import org.onap.so.serviceinstancebeans.ServiceException;
-
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
private static Logger logger = LoggerFactory.getLogger(ApiExceptionMapper.class);
-
+
private final JAXBContext context;
private final Marshaller marshaller;
private HttpHeaders headers;
public ApiExceptionMapper() {
- try {
- context = JAXBContext.newInstance(RequestError.class);
- marshaller = context.createMarshaller();
- } catch (JAXBException e) {
- logger.debug("could not create JAXB marshaller");
- throw new IllegalStateException(e);
- }
+ try {
+ context = JAXBContext.newInstance(RequestError.class);
+ marshaller = context.createMarshaller();
+ } catch (JAXBException e) {
+ logger.debug("could not create JAXB marshaller");
+ throw new IllegalStateException(e);
+ }
}
+
@Override
public Response toResponse(ApiException exception) {
}
-
+
List<MediaType> typeList = Optional.ofNullable(headers.getAcceptableMediaTypes()).orElse(new ArrayList<>());
List<String> typeListString = typeList.stream().map(item -> item.toString()).collect(Collectors.toList());
MediaType type;
if (typeListString.stream().anyMatch(item -> item.contains(MediaType.APPLICATION_XML))) {
- type = MediaType.APPLICATION_XML_TYPE;
+ type = MediaType.APPLICATION_XML_TYPE;
} else if (typeListString.stream().anyMatch(item -> typeListString.contains(MediaType.APPLICATION_JSON))) {
- type = MediaType.APPLICATION_JSON_TYPE;
+ type = MediaType.APPLICATION_JSON_TYPE;
} else {
- type = MediaType.APPLICATION_JSON_TYPE;
+ type = MediaType.APPLICATION_JSON_TYPE;
}
- return buildServiceErrorResponse(errorText,messageId,variables, type);
+ return buildServiceErrorResponse(errorText, messageId, variables, type);
}
- protected String buildServiceErrorResponse(String errorText, String messageId, List<String> variables, MediaType type){
+ protected String buildServiceErrorResponse(String errorText, String messageId, List<String> variables,
+ MediaType type) {
RequestError re = new RequestError();
ServiceException se = new ServiceException();
se.setMessageId(messageId);
String requestErrorStr;
ObjectMapper mapper = createObjectMapper();
-
+
mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
try {
- if (MediaType.APPLICATION_JSON_TYPE.equals(type)) {
- requestErrorStr = mapper.writeValueAsString(re);
+ if (MediaType.APPLICATION_JSON_TYPE.equals(type)) {
+ requestErrorStr = mapper.writeValueAsString(re);
} else {
- StringWriter sw = new StringWriter();
- this.getMarshaller().marshal(re, sw);
- requestErrorStr = sw.toString();
+ StringWriter sw = new StringWriter();
+ this.getMarshaller().marshal(re, sw);
+ requestErrorStr = sw.toString();
}
} catch (JsonProcessingException | JAXBException e) {
- String errorMsg = "Exception in buildServiceErrorResponse writing exceptionType to string " + e.getMessage();
+ String errorMsg =
+ "Exception in buildServiceErrorResponse writing exceptionType to string " + e.getMessage();
logger.error("{} {} {} {}", MessageEnum.GENERAL_EXCEPTION.toString(), "BuildServiceErrorResponse",
- ErrorCode.DataError.getValue(), errorMsg, e);
+ ErrorCode.DataError.getValue(), errorMsg, e);
return errorMsg;
}
}
protected void writeErrorLog(Exception e, String errorText, ErrorLoggerInfo errorLogInfo) {
- if( e!= null)
+ if (e != null)
logger.error("Exception occurred", e);
- if(errorLogInfo != null)
- logger.error(errorLogInfo.getLoggerMessageType().toString(), errorLogInfo.getErrorSource(), errorLogInfo.getTargetEntity(), errorLogInfo.getTargetServiceName(), errorLogInfo.getErrorCode(), errorText);
-
+ if (errorLogInfo != null)
+ logger.error(errorLogInfo.getLoggerMessageType().toString(), errorLogInfo.getErrorSource(),
+ errorLogInfo.getTargetEntity(), errorLogInfo.getTargetServiceName(), errorLogInfo.getErrorCode(),
+ errorText);
+
}
- public ObjectMapper createObjectMapper(){
+ public ObjectMapper createObjectMapper() {
return new ObjectMapper();
}
-
+
public Marshaller getMarshaller() {
- return marshaller;
+ return marshaller;
}
-
+
}