package com.cares.mqconnector.controller;
import com.cares.configManager.common.constants.MqMessageConstant;
import com.cares.configManager.core.message.MqMessage;
import com.cares.configManager.core.message.MqMessageBody;
import com.cares.configManager.core.message.MqMessageHeader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.oxm.Marshaller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.stream.StreamResult;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Date;
@Controller
@EnableConfigurationProperties({ServerProperties.class})
public class ErrorHandleController implements ErrorController {
private static MqMessageHeader header =new MqMessageHeader();;
private static MqMessageBody<Object> body = new MqMessageBody<Object>();
private static MqMessage<Object> msg = new MqMessage<Object>();
private static MessageProperties messageProperties;
static {
messageProperties = new MessageProperties();
messageProperties.setContentType(MediaType.APPLICATION_XML_VALUE);
header.setSendTime(new Date());
body.setSeqNum("0");
header.setSender(MqMessageConstant.Participate.DATACENTER.getParticipate());
msg.setBody(body);
msg.setHeader(header);
}
private Logger logger = LoggerFactory.getLogger(ErrorHandleController.class);
@Autowired
Marshaller marshaller;
@Override
public String getErrorPath() {
return null;
}
@RequestMapping(value = "error",produces = MediaType.APPLICATION_XML_VALUE)
@ResponseBody
public String errorHtml404(HttpServletRequest request,
HttpServletResponse response) {
HttpStatus httpStatus = getStatus(request);
if (HttpStatus.NOT_FOUND.equals(httpStatus)) {
body.setStatus(MqMessageConstant.MqMessageStatus.REJECT.getStatus());
} else {
body.setStatus(MqMessageConstant.MqMessageStatus.UNAVAILABLE.getStatus());
}
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
marshaller.marshal(msg, new StreamResult(byteArrayOutputStream));
} catch (IOException e) {
e.printStackTrace();
return "" ;
}
return new String(byteArrayOutputStream.toByteArray());
}
private HttpStatus getStatus(HttpServletRequest request) {
Integer statusCode = (Integer) request
.getAttribute("javax.servlet.error.status_code");
if (statusCode == null) {
return HttpStatus.INTERNAL_SERVER_ERROR;
}
try {
return HttpStatus.valueOf(statusCode);
}
catch (Exception ex) {
return HttpStatus.INTERNAL_SERVER_ERROR;
}
}
}
本博客所有文章除特别声明外,均采用 CC BY-SA 3.0协议 。转载请注明出处!