simplemaillistener.java
来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 559 行 · 第 1/2 页
JAVA
559 行
log.info(msgs.length + " Message(s) Found");
for (int i = 0; i < msgs.length; i++) {
MimeMessage msg = (MimeMessage) msgs[i];
try {
MessageContext mc = createMessageContextToMailWorker(msg);
msg.setFlag(Flags.Flag.DELETED, true);
if(mc==null){
continue;
}
MailWorker worker = new MailWorker(configurationContext,mc);
this.configurationContext.getThreadPool().execute(worker);
} catch (Exception e) {
log.error("Error in SimpleMailListener - processing mail", e);
} finally {
// delete mail in any case
}
}
}
receiver.disconnect();
}
} catch (Exception e) {
log.error("Error in SimpleMailListener", e);
} finally {
try {
Thread.sleep(listenerWaitInterval);
} catch (InterruptedException e) {
log.warn("Error Encountered " + e);
}
}
}
}
private MessageContext createMessageContextToMailWorker(MimeMessage msg) throws Exception {
Object content = msg.getContent();
if(!(content instanceof Multipart)){
return null;
}
MessageContext msgContext = null;
TransportInDescription transportIn =
configurationContext.getAxisConfiguration()
.getTransportIn(org.apache.axis2.Constants.TRANSPORT_MAIL);
TransportOutDescription transportOut =
configurationContext.getAxisConfiguration()
.getTransportOut(org.apache.axis2.Constants.TRANSPORT_MAIL);
if ((transportIn != null) && (transportOut != null)) {
// create Message Context
msgContext = configurationContext.createMessageContext();
msgContext.setTransportIn(transportIn);
msgContext.setTransportOut(transportOut);
msgContext.setServerSide(true);
msgContext.setProperty(org.apache.axis2.transport.mail.Constants.CONTENT_TYPE,
msg.getContentType());
msgContext.setIncomingTransportName(org.apache.axis2.Constants.TRANSPORT_MAIL);
MailBasedOutTransportInfo transportInfo = new MailBasedOutTransportInfo();
Address[] mimefroms = msg.getFrom();
if (mimefroms != null && mimefroms.length > 0) {
EndpointReference fromEPR = new EndpointReference(
org.apache.axis2.transport.mail.Constants.MAILTO + ":" +
msg.getFrom()[0].toString());
transportInfo.setFrom(fromEPR);
}
// Save Message-Id to set as In-Reply-To on reply
String smtpMessageId = msg.getMessageID();
if (smtpMessageId != null) {
transportInfo.setInReplyTo(smtpMessageId);
}
String inReplyTo =
getMailHeader(msg, org.apache.axis2.transport.mail.Constants.IN_REPLY_TO);
if (inReplyTo != null) {
transportInfo.setInReplyTo(inReplyTo);
}
msgContext.setProperty(org.apache.axis2.Constants.OUT_TRANSPORT_INFO, transportInfo);
buildSOAPEnvelope(msg, msgContext);
if(!fillMessageContextFromAvaiableData(msgContext,inReplyTo)){
return null;
}
}
return msgContext;
}
private boolean fillMessageContextFromAvaiableData(MessageContext msgContext ,
String messageID) throws AxisFault{
Hashtable mappingTable = (Hashtable) configurationContext.
getProperty(org.apache.axis2.transport.mail.Constants.MAPPING_TABLE);
if(mappingTable!=null&&messageID!=null){
String messageConetextId= (String) mappingTable.get(messageID);
if(messageConetextId!=null){
OperationContext opContext = configurationContext.getOperationContext(messageConetextId);
if(opContext!=null && !opContext.isComplete()){
AxisOperation axisOp = opContext.getAxisOperation();
//TODO need to handle fault case as well ,
//TODO need to check whether the message contains fault , if so we need to get the fault message
AxisMessage inMessage = axisOp.getMessage(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
msgContext.setOperationContext(opContext);
msgContext.setAxisMessage(inMessage);
opContext.addMessageContext(msgContext);
msgContext.setServiceContext(opContext.getServiceContext());
}
}
}
Hashtable callBackTable = (Hashtable) configurationContext.getProperty(
org.apache.axis2.transport.mail.Constants.CALLBACK_TABLE);
if(messageID!=null&&callBackTable!=null){
SynchronousMailListener listener = (SynchronousMailListener) callBackTable.get(messageID);
if(listener!=null){
listener.setInMessageContext(msgContext);
return false;
}
}
return true;
}
private void buildSOAPEnvelope(MimeMessage msg, MessageContext msgContext)
throws AxisFault {
//TODO we assume for the time being that there is only one attachement and this attachement contains the soap evelope
try {
Multipart mp = (Multipart) msg.getContent();
if (mp != null) {
for (int i = 0, n = mp.getCount(); i < n; i++) {
Part part = mp.getBodyPart(i);
String disposition = part.getDisposition();
if (disposition != null && disposition.equalsIgnoreCase(Part.ATTACHMENT)) {
String soapAction;
/* Set the Charactorset Encoding */
String contentType = part.getContentType();
String charSetEncoding = BuilderUtil.getCharSetEncoding(contentType);
if (charSetEncoding != null) {
msgContext.setProperty(
org.apache.axis2.Constants.Configuration.CHARACTER_SET_ENCODING,
charSetEncoding);
} else {
msgContext.setProperty(
org.apache.axis2.Constants.Configuration.CHARACTER_SET_ENCODING,
MessageContext.DEFAULT_CHAR_SET_ENCODING);
}
/* SOAP Action */
soapAction = getMailHeaderFromPart(part,
org.apache.axis2.transport.mail.Constants.HEADER_SOAP_ACTION);
msgContext.setSoapAction(soapAction);
String contentDescription =
getMailHeaderFromPart(part, "Content-Description");
/* As an input stream - using the getInputStream() method.
Any mail-specific encodings are decoded before this stream is returned.*/
if (contentDescription != null) {
msgContext.setTo(new EndpointReference(contentDescription));
}
if (contentType.indexOf(SOAP12Constants.SOAP_12_CONTENT_TYPE) > -1) {
TransportUtils
.processContentTypeForAction(contentType, msgContext);
} else {
// According to the mail sepec, mail transport should support only
// application/soap+xml;
String message = "According to the mail sepec, mail transport " +
"should support only application/soap+xml";
log.error(message);
throw new AxisFault(message);
}
String cte = getMailHeaderFromPart(part, "Content-Transfer-Encoding");
if (!(cte != null && cte.equalsIgnoreCase("base64"))) {
String message = "Processing of Content-Transfer-Encoding faild.";
log.error(message);
throw new AxisFault(message);
}
InputStream inputStream = part.getInputStream();
SOAPEnvelope envelope = TransportUtils
.createSOAPMessage(msgContext, inputStream, contentType);
msgContext.setEnvelope(envelope);
}
}
}
} catch (IOException e) {
throw new AxisFault(e.getMessage(),e);
}
catch (MessagingException e) {
throw new AxisFault(e.getMessage(),e);
} catch (XMLStreamException e) {
throw new AxisFault(e.getMessage(),e);
}
}
private String getMailHeader(MimeMessage msg, String headerName) throws AxisFault {
try {
String values[] = msg.getHeader(headerName);
if (values != null) {
return parseHeaderForQuotes(values[0]);
} else {
return null;
}
} catch (MessagingException e) {
throw new AxisFault(e.getMessage(),e);
}
}
private String parseHeaderForQuotes(String value) {
if (value != null) {
if (value.length() > 1 && value.startsWith("\"") && value.endsWith("\"")) {
value = value.substring(1, value.length() - 1);
}
}
return value;
}
private String getMailHeaderFromPart(Part part, String headerName) throws AxisFault {
try {
String values[] = part.getHeader(headerName);
if (values != null) {
return parseHeaderForQuotes(values[0]);
} else {
return null;
}
} catch (MessagingException e) {
throw new AxisFault(e.getMessage(),e);
}
}
/**
* Start this listener
*/
public void start() throws AxisFault {
this.configurationContext.getThreadPool().execute(this);
}
/**
* Stop this server.
* <p/>
*/
public void stop() {
running = false;
log.info("Stopping the mail listner");
}
public EndpointReference getEPRForService(String serviceName, String ip) throws AxisFault {
return getEPRsForService(serviceName, ip)[0];
}
public EndpointReference[] getEPRsForService(String serviceName, String ip) throws AxisFault {
return new EndpointReference[]{
new EndpointReference(Constants.TRANSPORT_MAIL + ":" + replyTo + "?" +
org.apache.axis2.transport.mail.Constants.X_SERVICE_PATH + "="
+ configurationContext.getServiceContextPath() + "/" +
serviceName),
new EndpointReference(Constants.TRANSPORT_MAIL + ":" + replyTo + "?" +
configurationContext.getServiceContextPath() + "/" +
serviceName)
};
}
public SessionContext getSessionContext(MessageContext messageContext) {
return null;
}
public void destroy() {
this.configurationContext = null;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?