📄 mm7receiver.java
字号:
SevereBuffer = new StringBuffer();
InfoBuffer = new StringBuffer();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
break;
case 4:
break;
case 6:
try {
FileOutputStream fos = new FileOutputStream(logFileName, true);
if (fos != null) {
if (SevereBuffer != null && SevereBuffer.length() > 0)
fos.write(SevereBuffer.toString().getBytes());
if (InfoBuffer != null && InfoBuffer.length() > 0)
fos.write(InfoBuffer.toString().getBytes());
if (Finerbaos != null && Finerbaos.size() > 0)
fos.write(Finerbaos.toByteArray());
fos.close();
SevereBuffer = new StringBuffer();
InfoBuffer = new StringBuffer();
Finerbaos = new ByteArrayOutputStream();
}
}
catch (IOException ioe) {
ioe.printStackTrace();
}
break;
case 7:
break;
default:
break;
}
}
catch (Exception e) {
}
/*
* int LogLevel = Config.getLogLevel(); String LogPath =
* Config.getLogPath(); int LogNum = Config.getLogNum(); int LogInterval =
* Config.getLogInterval(); int LogSize = Config.getLogSize(); long
* Interval = System.currentTimeMillis() - LogTimeBZ; FileOutputStream
* fos = null; SimpleDateFormat simpledf = new
* SimpleDateFormat("yyyyMMddHHmm"); String temptime =
* simpledf.format(new Date(System.currentTimeMillis())); long timeNow =
* 0; try{ timeNow = simpledf.parse(temptime).getTime();
* }catch(ParseException pe){ pe.printStackTrace(); }
*
* if(logFileName.length() > 0) { File logFile = new File(logFileName);
* int index1 = logFileName.indexOf(MMSCID+"_"); int index11 = index1 +
* MMSCID.length()+1; int index2 = logFileName.indexOf(".",index11+1);
* String strtimeFile = logFileName.substring(index11,index2); long
* timeFile = 0; try{ timeFile = simpledf.parse(strtimeFile).getTime();
* }catch(ParseException pe){ pe.printStackTrace(); }
* if((timeNow-timeFile) > (long)LogInterval * 60 * 1000) { N = 1;
* this.deleteFile(LogPath, LogNum,MMSCID); logFileName = LogPath + "/" +
* MMSCID + "_" + temptime + "." +df.format(N) + ".log"; try{ fos = new
* FileOutputStream(logFileName); }catch(FileNotFoundException fnfe){
* fnfe.printStackTrace(); } } else { if (logFile.length() >= LogSize *
* 1024) { if (N < LogNum) N++; else N = 1; this.deleteFile(LogPath,
* LogNum, MMSCID); logFileName = LogPath + "/" + MMSCID + "_" +
* temptime + "." + df.format(N) + ".log"; try { fos = new
* FileOutputStream(logFileName); } catch (FileNotFoundException fnfe) {
* fnfe.printStackTrace(); } } } } else { N = 1;
* this.deleteFile(LogPath, LogNum,MMSCID); logFileName = LogPath + "/" +
* MMSCID + "_" + temptime + "." +df.format(N) + ".log"; try{ File
* logFile = new File(logFileName); fos = new FileOutputStream(logFile);
* }catch(FileNotFoundException fnfe){ fnfe.printStackTrace(); } } try{
* switch (LogLevel) { case 1: { if(SevereBuffer != null &&
* SevereBuffer.length() > 0) if(fos != null) { try { FileOutputStream
* fos = new FileOutputStream(logFileName, true);
* fos.write(Severebaos.toByteArray()); fos.close(); SevereBuffer = new
* StringBuffer(); }catch(Exception e){} } break; } case 2: break; case
* 3: { if(fos != null) { if (SevereBuffer != null &&
* SevereBuffer.length() > 0)
* fos.write(SevereBuffer.toString().getBytes()); if (InfoBuffer != null &&
* InfoBuffer.length() > 0) fos.write(InfoBuffer.toString().getBytes());
* fos.close(); } break; } case 4: break; case 5: break; case 6: {
* if(fos != null){ if (SevereBuffer != null && SevereBuffer.length() >
* 0) fos.write(SevereBuffer.toString().getBytes()); if (InfoBuffer !=
* null && InfoBuffer.length() > 0)
* fos.write(InfoBuffer.toString().getBytes()); if (Finerbaos != null &&
* Finerbaos.size() > 0) fos.write(Finerbaos.toByteArray());
* fos.close(); } break; } case 7: break; default: break; } } catch
* (IOException ioe) { ioe.printStackTrace(); }
*/
}
private void deleteFile(String logpath, int lognum, String MMSCID) {
File parfile = new File(logpath);
if (parfile.isDirectory()) {
File[] subfile = parfile.listFiles();
List list = new ArrayList();
for (int i = 0; i < subfile.length; i++) {
String name = subfile[i].getName();
if (name.indexOf(MMSCID) >= 0) {
list.add(name);
}
}
if (list.size() >= lognum) {
int deleteLength = list.size() - lognum + 1;
Comparator comp = Collections.reverseOrder();
Collections.sort(list, comp);
for (int i = list.size() - deleteLength; i < list.size(); i++) {
String strfile = (String) list.get(i);
File ff = new File(logpath + "/" + strfile);
ff.delete();
}
}
}
}
private void ServiceConnect(Socket client, int nc) throws IOException {
boolean longLink = false;
longLink = this.getLongLink();
DataInputStream in = null;
SevereBuffer = new StringBuffer();
InfoBuffer = new StringBuffer();
// FinerBuffer = new StringBuffer();
Finerbaos = new ByteArrayOutputStream();
boolean bHead = false;
int index1 = 0, index2 = 0, index3 = 0;
int length = 0;
try {
// client.setSoTimeout(Config.getTimeOut());
// client.setSoTimeout(0);
SevereBuffer.append("recv client=" + client + "\r\n");
in = new DataInputStream(new BufferedInputStream(client.getInputStream()));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int readlen = 0;
int totallen = 0;
while (1 > 0) {
// System.out.println("开始接收");
readlen = in.read(b);
if (readlen < 0)
return;
baos.write(b, 0, readlen);
totallen = totallen + readlen;
if (bHead == false && baos.toString().indexOf("\r\n\r\n") < 0)
continue;
if (bHead == false) {
index1 = baos.toString().toLowerCase().indexOf(MMConstants.CONTENT_LENGTH);
index2 = baos.toString().indexOf("\r", index1);
if (index1 == -1 || index2 == -1) {
baos.reset();
totallen = 0;
continue;
}
length = Integer.parseInt(baos.toString().substring(
(index1 + MMConstants.CONTENT_LENGTH.length() + 1), index2).trim());
index3 = baos.toString().indexOf("\r\n\r\n");
bHead = true;
}
if (totallen < index3 + length + 4)
continue;
bHead = false;
if (Config.getAuthenticationMode() == 1) {
if (basicAuth(client, baos) == false) {
baos.reset();
totallen = 0;
continue;
}
}
else if (Config.getAuthenticationMode() == 2) {
if (digestAuth(client, baos, nc) == false) {
baos.reset();
totallen = 0;
continue;
}
}
// System.out.println("baos="+baos.toString());
SOAPDecoder soapDecoder = new SOAPDecoder();
soapDecoder.setMessage(baos);
try {
soapDecoder.decodeMessage();
// System.out.println("xml处理结束");
SevereBuffer.append("end dealing with xml!");
MM7RSReq req = soapDecoder.getMessage();
if (req == null) {
baos.reset();
totallen = 0;
SevereBuffer.append("[Comments=解析后的消息为空!]");
continue;
}
if (soapDecoder.getMessageName().equals(MMConstants.DELIVERREQ)) {
SevereBuffer.append("DeliverReq");
MM7DeliverReq deliverReq = (MM7DeliverReq) req;
InfoBuffer
.append("\r\n\r\n[" + Recordsdf.format(new Date(System.currentTimeMillis())) + "][3]");
TempBuffer.append("[TransactionID=" + deliverReq.getTransactionID() + "]");
TempBuffer.append("[Message_Type=MM7DeliverReq]");
TempBuffer.append("[Sender_Address=" + deliverReq.getSender() + "]");
TempBuffer.append("[Recipient_Address={");
if (deliverReq.isToExist()) {
TempBuffer.append("To={");
List to = new ArrayList();
to = deliverReq.getTo();
for (int i = 0; i < to.size(); i++)
TempBuffer.append((String) to.get(i) + ",");
TempBuffer.append("}");
}
if (deliverReq.isCcExist()) {
TempBuffer.append("Cc={");
List cc = new ArrayList();
cc = deliverReq.getCc();
for (int i = 0; i < cc.size(); i++)
TempBuffer.append((String) cc.get(i) + ",");
TempBuffer.append("}");
}
if (deliverReq.isBccExist()) {
TempBuffer.append("Bcc={");
List bcc = new ArrayList();
bcc = deliverReq.getBcc();
for (int i = 0; i < bcc.size(); i++)
TempBuffer.append((String) bcc.get(i) + ",");
TempBuffer.append("}");
}
TempBuffer.append("}]");
InfoBuffer.append(TempBuffer);
InfoBuffer.append("\r\n" + soapDecoder.getEnvelope()); // Envelope消息
String timelevel = "\r\n\r\n[" + Recordsdf.format(new Date(System.currentTimeMillis()))
+ "][6]";
Finerbaos.write(timelevel.getBytes());
Finerbaos.write(TempBuffer.toString().getBytes());
Finerbaos.write("\r\n".getBytes());
Finerbaos.write(soapDecoder.getSoapStream().toByteArray());
TempBuffer = new StringBuffer();
MM7DeliverRes deliverRes = (MM7DeliverRes) doDeliver(deliverReq);
InfoBuffer.append("\r\n\r\n" + "[" + Recordsdf.format(new Date(System.currentTimeMillis()))
+ "]" + "[3]");
String tempres = "\r\n\r\n[" + Recordsdf.format(new Date(System.currentTimeMillis())) + "][6]";
Finerbaos.write(tempres.getBytes());
TempBuffer.append("[Message_Type=MM7DeliverRes]");
TempBuffer.append("[Comments={" + deliverRes.getStatusCode() + ";" + deliverRes.getStatusText()
+ "}]\r\n");
InfoBuffer.append(TempBuffer);
Finerbaos.write(TempBuffer.toString().getBytes());
TempBuffer = new StringBuffer();
Finerbaos.write("\r\n".getBytes());
send(client, deliverRes, longLink);
}
else if (soapDecoder.getMessageName().equals(MMConstants.DELIVERYREPORTREQ)) {
// System.out.println("MMConstants.DELIVERYREPORTREQ");
SevereBuffer.append(" DeliveryReportReq");
MM7DeliveryReportReq deliverReportReq = (MM7DeliveryReportReq) req;
InfoBuffer
.append("\r\n\r\n[" + Recordsdf.format(new Date(System.currentTimeMillis())) + "][3]");
TempBuffer.append("[TransactionID=" + deliverReportReq.getTransactionID() + "]");
TempBuffer.append("[Message_Type=MM7DeliveryReportReq]");
TempBuffer.append("[Sender_Address=" + deliverReportReq.getSender() + "]");
InfoBuffer.append(TempBuffer);
InfoBuffer.append("\r\n" + soapDecoder.getEnvelope()); // Envelope消息
String timelevel = "\r\n\r\n[" + Recordsdf.format(new Date(System.currentTimeMillis()))
+ "][6]";
Finerbaos.write(timelevel.getBytes());
Finerbaos.write(TempBuffer.toString().getBytes());
Finerbaos.write("\r\n".getBytes());
Finerbaos.write(soapDecoder.getSoapStream().toByteArray());
TempBuffer = new StringBuffer();
MM7DeliveryReportRes deliverReportRes = (MM7DeliveryReportRes) doDeliveryReport(deliverReportReq);
InfoBuffer.append("\r\n\r\n" + "[" + Recordsdf.format(new Date(System.currentTimeMillis()))
+ "]" + "[3]");
String tempres = "\r\n\r\n[" + Recordsdf.format(new Date(System.currentTimeMillis())) + "][6]";
Finerbaos.write(tempres.getBytes());
TempBuffer.append("[Message_Type=MM7DeliverRes]");
TempBuffer.append("[Comments={" + deliverReportRes.getStatusCode() + ";"
+ deliverReportRes.getStatusText() + "}]\r\n");
InfoBuffer.append(TempBuffer);
Finerbaos.write(TempBuffer.toString().getBytes());
TempBuffer = new StringBuffer();
Finerbaos.write("\r\n".getBytes());
send(client, deliverReportRes, longLink);
}
else if (soapDecoder.getMessageName().equals(MMConstants.READREPLYREQ)) {
SevereBuffer.append(" ReadReplyReq");
MM7ReadReplyReq readReplyReq = (MM7ReadReplyReq) req;
InfoBuffer
.append("\r\n\r\n[" + Recordsdf.format(new Date(System.currentTimeMillis())) + "][3]");
TempBuffer.append("[TransactionID=" + readReplyReq.getTransactionID() + "]");
TempBuffer.append("[Message_Type=MM7ReadReplyReq]");
TempBuffer.append("[Sender_Address=" + readReplyReq.getSender() + "]");
InfoBuffer.append(TempBuffer);
InfoBuffer.append("\r\n" + soapDecoder.getEnvelope()); // Envelope消息
String timelevel = "\r\n\r\n[" + Recordsdf.format(new Date(System.currentTimeMillis()))
+ "][6]";
Finerbaos.write(timelevel.getBytes());
Finerbaos.write(TempBuffer.toString().getBytes());
Finerbaos.write("\r\n".getBytes());
Finerbaos.write(soapDecoder.getSoapStream().toByteArray());
TempBuffer = new StringBuffer();
MM7ReadReplyRes readReplyRes = (MM7ReadReplyRes) doReadReply(readReplyReq);
InfoBuffer.append("\r\n\r\n" + "[" + Recordsdf.format(new Date(System.currentTimeMillis()))
+ "]" + "[3]");
String tempres = "\r\n\r\n[" + Recordsdf.format(new Date(System.currentTimeMillis())) + "][6]";
Finerbaos.write(tempres.getBytes());
TempBuffer.append("[Message_Type=MM7DeliverRes]");
TempBuffer.append("[Comments={" + readReplyRes.getStatusCode() + ";"
+ readReplyRes.getStatusText() + "}]\r\n");
InfoBuffer.append(TempBuffer);
Finerbaos.write(TempBuffer.toString().getBytes());
TempBuffer = new StringBuffer();
Finerbaos.write("\r\n".getBytes());
send(client, readReplyRes, longLink);
}
InfoBuffer.append(strEnvelope);
Finerbaos.write(strEnvelope.getBytes());
String time = "[" + Recordsdf.format(new Date(System.currentTimeMillis())) + "]";
SevereBuffer.insert(0, "\r\n\r\n" + time + "[1]"); //
MMSCID = Config.getMmscId();
if (Config.getLogLevel() > 0) {
SevereBuffer.append("manager count=" + (receiveNumber++));
WriteLog(MMSCID);
}
}
catch (SOAPDecodeException e) {
System.err.println("decode exception:" + e);
SevereBuffer.append("[SOAPDecodeException=" + e + "]");
}
// System.out.println("处理数目="+(receiveNumber++));
if (totallen > index3 + length + 4) {
baos.write(b, (MMConstants.ARRAYLIMIT - (totallen - index3 - length - 4)), totallen - index3
- length - 4);
baos.reset();
totallen = totallen - index3 - length - 4;
baos.write(b, 0, totallen);
}
else {
baos.reset();
totallen = 0;
}
bHead = false;
if (longLink == false)
break;
}
}
catch (IOException ioe) {
SevereBuffer.append("[Error in SimpleWebServer: " + ioe + "]");
}
finally {
// 清除
if (in != null)
in.close();
if (client != null)
client.close();
}
}
public void send(Socket socket, MM7VASPRes mm7VASPRes, boolean longLink) {
StringBuffer beforAuth = new StringBuffer();
StringBuffer afterAuth = new StringBuffer();
StringBuffer sb = new StringBuffer();
StringBuffer entityBody = new StringBuffer();
BufferedOutputStream sender = null;
try {
SevereBuffer.append(" send ack socket is " + socket + "\r\n");
sender = new BufferedOutputStream(socket.getOutputStream());
beforAuth.append("HTTP/1.1 200 OK\r\n");
if (longLink == true)
beforAuth.append("Connection: Keep-Alive\r\n");
else
beforAuth.append("Connection: close\r\n");
// beforAuth.append("Content-Type:
// text/xml;charset=\""+Config.getCharSet()+"\""+"\r\n");
beforAuth.append("Content-Type: text/xml;charset=" + Config.getCharSet() + "\r\n");
// 设置鉴权
byte[] bcontent = getContent(mm7VASPRes);
// 设置消息体长度
afterAuth.append("Content-Length: " + bcontent.length + "\r\n");
// /////////
// afterAuth.append("Connection: close\r\n");
// ////////
// afterAuth.append("Mime-Version:1.0"+"\r\n");
// afterAuth.append("SOAPAction: \"\""+"\r\n");
afterAuth.append("\r\n");
sb.append(beforAuth);
sb.append(afterAuth);
entityBody.append(new String(bcontent));
sb.append(entityBody);
sender.write(sb.toString().getBytes());
sender.flush();
// System.out.println("发送完毕");
SevereBuffer.append("end sending ack!");
return;
}
catch (InterruptedIOException iioe) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -