⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mm7sender.java

📁 彩信发送源代码,实现mm7接口
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
										res.setStatusDetail(subEle.getTextTrim());
								}
							}
						}
						return res;
					}
				}
			}
		}
		catch (JDOMException jdome) {
			MM7RSErrorRes error = new MM7RSErrorRes();
			// System.err.print(jdome);
			log.error("解析返回消息失败:" + jdome);
			error.setStatusCode(-109);
			error.setStatusText("XML解析错误!原因:" + jdome);
			SevereBuffer.append("[Message_Type=MM7RSErrorRes]");
			SevereBuffer.append("[Comments={-109;" + error.getStatusText() + "}]");
			return error;
		}
		catch (Exception e) {
			// e.printStackTrace();
			log.error("系统错误:" + e);
			MM7RSErrorRes error = new MM7RSErrorRes();
			error.setStatusCode(-100);
			error.setStatusText("系统错误!原因:" + e);
			return error;
		}
	}

	private ByteArrayOutputStream getSendMessage(byte[] bcontent) {
		try {
			/**
			 * 在此加以判断,如果是长连接,则得到以前的连接,否则新建连接
			 */
			if (pool.getKeepAlive().equals("on")) {
				if (log.isDebugEnabled())
					log.debug("KeepAlive设置为ON,TimeOutFlag的值为:" + TimeOutFlag);
				if (this.TimeOutFlag == true) {

					boolean deleteURL = pool.deleteURL(TimeOutWrap);
					if (log.isDebugEnabled())
						log.debug("从链接池中删除链接是否成功:" + deleteURL);
					SevereBuffer.append("TimeOutFlag=true\r\n");
					if (deleteURL) {
						SevereBuffer.append("  TimeOutWrap=" + TimeOutWrap);
						SevereBuffer.append("  pool.deleteURL(TimeOutWrap)");
						// //Add by hudm 2004-06-07
						/*
						 * String MMSCIP = (String)mm7Config.getMMSCIP().get(0);
						 * int index = MMSCIP.indexOf(":"); String ip; int port;
						 * if(index == -1) { ip = MMSCIP; port = 80; } else { ip =
						 * MMSCIP.substring(0,index); port =
						 * Integer.parseInt(MMSCIP.substring(index+1)); } client =
						 * new Socket(ip,port);
						 */
						// //end add by hudm
						connWrap = pool.getConnWrap();
						if (log.isDebugEnabled())
							log.debug("TimeOutFlag为true,从连接池中获取到的连接:" + connWrap);
						this.TimeOutWrap = connWrap;
						if (connWrap != null) {
							SevereBuffer.append("  connWrap!= null");
							client = connWrap.getSocket();
						}
						else {
							log.warn("TimeOutFlag为true,没有从连接池中获取到连接,");
							SevereBuffer.append("   client=null");
							client = null;
						}
					}
					else {
						SevereBuffer.append("deleteURL is false!");
						log.warn("TimeOutFlag为true,从连接池中删除连接失败,返回NULL!!!");
						return null;
					}
				}
				else {
					// SevereBuffer.append("!767!TimeOutFlag==false");
					connWrap = pool.getConnWrap();
					this.TimeOutWrap = connWrap;
					if (log.isDebugEnabled())
						log.debug("TimeOutFlag为False,从连接池中获取到的连接:" + connWrap);
					if (connWrap != null) {
						// SevereBuffer.append(" connWrap != null");
						client = connWrap.getSocket();
						SevereBuffer.append("!!!!client=" + client);
						// ///Add by hudm 2004-06-07
						if (connWrap.getUserfulFlag() == false || client.isConnected() == false) {
							log.warn("connWrap.getUserfulFlag()=" + connWrap.getUserfulFlag()
									+ " client.isConnected()=" + client.isConnected());
							SevereBuffer
									.append("!771!connWrap.getUserfulFlag() == false || client.isConnected() == false");
							pool.deleteURL(connWrap);
							connWrap = pool.getConnWrap();
							log.warn("重新获取到的连接:" + connWrap);
							this.TimeOutWrap = connWrap;
						}
						if (connWrap.getAuthFlag() == true) {
							AuthInfor = connWrap.getDigestInfor();
						}
						// ///end add by hudm
					}
					else
						client = null;
				}
				/*
				 * (connWrap != null) { SevereBuffer.append("!768!connWrap !=
				 * null"); if (connWrap.getUserfulFlag() == false ||
				 * client.isConnected() == false) { //从连接池中删除该条连接
				 * SevereBuffer.append("!771!connWrap.getUserfulFlag() == false ||
				 * client.isConnected() == false"); pool.deleteURL(connWrap);
				 * //////// connWrap = pool.getConnWrap(); this.TimeOutWrap =
				 * connWrap; /////// //return null; } if (connWrap.getAuthFlag() ==
				 * true) { AuthInfor = connWrap.getDigestInfor(); } } else {
				 * SevereBuffer.append("!785!connWrap==null"); return null; }
				 */
			}
			else {
				log.warn("短连接的方式,设置SOCKET!!!");
				String MMSCIP = (String) mm7Config.getMMSCIP().get(0);
				int index = MMSCIP.indexOf(":");
				String ip;
				int port;
				if (index == -1) {
					ip = MMSCIP;
					port = 80;
				}
				else {
					ip = MMSCIP.substring(0, index);
					port = Integer.parseInt(MMSCIP.substring(index + 1));
				}
				client = new Socket(ip, port);
			}
			if (log.isDebugEnabled())
				log.debug("Client连接:" + client);
			if (client != null) {
				SevereBuffer.append("!829!client != null and equals" + client);
				sender = new BufferedOutputStream(client.getOutputStream());
				receiver = new BufferedInputStream(client.getInputStream());
				client.setSoTimeout(mm7Config.getTimeOut());
				client.setKeepAlive(true);

				sb = new StringBuffer();
				sb.append(beforAuth);
				sb.append(AuthInfor);
				sb.append(afterAuth);
				try {
					sendBaos = new ByteArrayOutputStream();
					SevereBuffer.append("!part of send message is:" + sb.toString() + "!\r\n");
					sendBaos.write(sb.toString().getBytes());
					sendBaos.write(bcontent);
					return sendBaos;
				}
				catch (IOException e) {
					// System.out.println("IOException!原因:"+e);
					SevereBuffer.append("IOException=" + e + "\r\n");
					return null;
				}
			}
			else {
				if (log.isDebugEnabled())
					log.debug("和MMSC的连接为空,返回NULL!!!");
				SevereBuffer.append("!853!client == null");
				return null;
			}
		}
		catch (UnknownHostException uhe) {
			// System.out.println("UnknownHostExcepion!原因:"+uhe);
			log.error("UnknownHostExcepion!返回NULL,原因:" + uhe);
			SevereBuffer.append("UnknownHostExcepion=" + uhe + "\r\n");
			return null;
		}
		catch (SocketException se) {
			// System.out.println("SocketException!原因:"+se);
			log.error("SocketException!返回NULL,原因:" + se);
			SevereBuffer.append("SocketException=" + se + "\r\n");
			return null;
		}
		/**
		 * 超时后,被捕获,认为该次发送失败,重新进行发送,当发送超过一定次数后, 即认为整个发送失败。返回失败信息。
		 */
		catch (InterruptedIOException iioe) {
			log.error("Socket Interrupted,返回NULL!原因:" + iioe);
			// modified by liuhuafeng on 2006/7/26
			// 如果出现InterruptedIOException,不再进行重复发送,同事将连接池中的这条连接删除
			// this.TimeOutFlag = true;

			pool.deleteURL(this.TimeOutWrap);
			// for (int j = 0; j < mm7Config.getReSendCount(); j++) {
			// sendBaos = getSendMessage(bcontent);
			// if (sendBaos != null)
			// res = this.SendandReceiveMessage(sendBaos);
			// }
			res.setStatusCode(-101);
			res.setStatusText("超时发送失败!原因:" + iioe);
			SevereBuffer.append("[Comments={-101;超时发送失败!原因:" + iioe + "}]\r\n");
			return null;
		}
		catch (Exception e) {
			// e.printStackTrace();
			log.error("其他异常:返回NULL!原因:" + e);
			res.setStatusCode(-100);
			res.setStatusText("系统错误!原因:" + e);
			SevereBuffer.append("[Comments={-100;系统错误!原因:" + e + "}]\r\n");
			return null;
		}
	}

	private MM7RSRes SendandReceiveMessage(ByteArrayOutputStream sendbaos) {
		try {
			// ///////
			// SevereBuffer.append("sendbaos="+sendbaos.toString());
			// //////
			sender.write(sendbaos.toByteArray());
			sender.flush();
//			if (log.isDebugEnabled())
				log.info("彩信成功发送到MMSC.......");
			if (this.receiveMessge()) {
				res = parseXML();
				return res;
			}
			else {
				MM7RSErrorRes error = new MM7RSErrorRes();
				error.setStatusCode(-102);
				error.setStatusText("接收失败!");
				SevereBuffer.append("[Message_Type=MM7RSErrorRes]");
				SevereBuffer.append("[Comments={-102;" + error.getStatusText() + "}]");
				return error;
			}
		}
		catch (IOException ioe) {
			log.error("SendandReceiveMessage():"+ioe);
			
			MM7RSErrorRes error = new MM7RSErrorRes();
			error.setStatusCode(-104);
			 error.setStatusText("Socket不通!原因:" + ioe);
//			error.setStatusText(" " + ioe);
			SevereBuffer.append("[Message_Type=MM7RSErrorRes]");
			SevereBuffer.append("[Comments={-104;" + error.getStatusText() + "}]");
			
			pool.deleteURL(this.TimeOutWrap);
			
			return error;
			//modified by liuhuafeng on 2006/7/26,如果出现ioexception的话,则直接返回-104的错误信息
			//不再由此进行重发
			//同时在此将连接池中的这个连接删除掉
			
	
			
//			this.TimeOutFlag = true;
//			sendBaos = getSendMessage(this.TimeOutbCount);
//			if (sendBaos != null) {
//				res = SendandReceiveMessage(sendBaos);
//				if (res != null) {
//					this.TimeOutFlag = false;
//					return res;
//				}
//				else {
//					MM7RSErrorRes error = new MM7RSErrorRes();
//					error.setStatusCode(-103);
//					error.setStatusText("没有返回正确的消息");
//					SevereBuffer.append("[Message_Type=MM7RSErrorRes]");
//					SevereBuffer.append("[Comments={-103;" + error.getStatusText() + "}]");
//					return error;
//				}
//				/*
//				 * if (res.getStatusCode() == MMConstants.RequestStatus.SUCCESS) {
//				 * this.TimeOutFlag = false; return res; } else { MM7RSErrorRes
//				 * error = new MM7RSErrorRes(); error.setStatusCode( -110);
//				 * error.setStatusText("原因:" + ioe);
//				 * SevereBuffer.append("[Message_Type=MM7RSErrorRes]");
//				 * SevereBuffer.append("[Comments={-103;" +
//				 * error.getStatusText() + "}]"); return error; }
//				 */
//			}
//			else {
//				MM7RSErrorRes error = new MM7RSErrorRes();
//				error.setStatusCode(-104);
//				// error.setStatusText("Socket不通!原因:" + ioe);
//				error.setStatusText(" " + ioe);
//				SevereBuffer.append("[Message_Type=MM7RSErrorRes]");
//				SevereBuffer.append("[Comments={-104;" + error.getStatusText() + "}]");
//				return error;
//			}
		}
	}

	public boolean receiveMessge() throws IOException {
		try {
			baos.reset();
			boolean bReceive = false;
			byte[] data = new byte[MMConstants.ARRAYLIMIT];
			// int word = -1;
			int readline = -1;
			int totalline = 0;
			boolean flag = false;
			boolean bHead = false;

			// 将此设置为receiver.read(data)的次数,如果readlineOut为1并readline为-1,则直接返回false
			int readlineOut = 0;
			// int envelopecount = 0;
			//added by liuhuafeng on 2006/7/26
			//如果read(data)的时候,不是第一次返回-1,而是之后才返回的-1,那么设定的读的时间为5秒
			long now=System.currentTimeMillis();
			while (1 > 0) {
				if (flag)
					break;
				readline = receiver.read(data);
				// added by liuhuafeng on 2006/7/26
				readlineOut++;

				if (readline != -1) {
					baos.write(data, 0, readline);
//					if (log.isDebugEnabled())
//						log.debug("收到的返回消息:" + baos);
					// System.out.println("baos==="+baos.toString());
					totalline = totalline + readline;
					if (baos.toString().indexOf("\r\n\r\n") < 0)
						continue;
					if (bHead == false) {
						int httpindex = baos.toString().indexOf("HTTP/1.1");
						if (httpindex != -1) {
							String httpCode = baos.toString().substring(httpindex + 8, httpindex + 12).trim();
							int httpsepindex = baos.toString().indexOf("\r\n\r\n");
							if (httpCode.equals("401")) {
								log.warn("MMSC返回验证失败信息,HttpCode:" + httpCode);

								if (baos.toString().indexOf("Digest") != -1) {
									if (log.isDebugEnabled())
										log.debug("进行了Digest加密,对其进行解析");
									if (ResendCount < mm7Config.getReSendCount()) {
										ResendCount = ResendCount + 1;
										pool.setInitNonceCount();
										String clientAuthInfor = "";
										String authInfor = "";
										int authindex = baos.toString().indexOf(MMConstants.AUTHENTICATION);
										if (authindex > 0) {
											int lineend = baos.toString().indexOf("\r", authindex + 1);
											int linebeg = authindex + MMConstants.AUTHENTICATION.length() + 1;
											authInfor = baos.toString().substring(linebeg, lineend);
											clientAuthInfor = setDigestAuth(authInfor);
										}
										int connectionindex = baos.toString().toLowerCase().indexOf(
												MMConstants.CONNECTION);
										int connlength = connectionindex + MMConstants.CONNECTION.length() + 1;
										int connectionend = baos.toString().indexOf("\r\n", connectionindex);
										String ConnectionFlag = "";
										if (connectionindex != -1 && connectionend != -1)
											ConnectionFlag = baos.toString().substring(connlength, connectionend);
										sb = new StringBuffer();
										sb.append(beforAuth);
										sb.append(clientAuthInfor);
										sb.append(afterAuth);
										sb.append(entityBody);
										sender.write(sb.toString().getBytes());
										sender.flush();
										baos = new ByteArrayOutputStream();
										data = new byte[MMConstants.ARRAYLIMIT];
										int resline = -1;
										int totalresline = 0;
										boolean excuteFlag = false;
										int httpseper = -1;
										int contlen3 = -1;
										while (1 > 0) {
											resline = receiver.read(data);
											if (resline == -1)
												break;
											baos.write(data, 0, resline);
											totalresline += resline;
											if (baos.toString().indexOf("\r\n\r\n") < 0)
												continue;
											if (excuteFlag == false) {
												httpindex = baos.toString().indexOf("HTTP/1.1");
												httpCode = baos.toString().substring(httpindex + 8, httpindex + 12)
														.trim();
												int conlen1 = baos.toString().toLowerCase().indexOf(

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -