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

📄 mm7sender.java

📁 彩信网关程序,SP程序,包含移动彩信代码和电信彩信代码!供学习之用
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
							Element ele = (Element) Message.getChildren()
									.get(i);
							if (ele.getName().equals(MMConstants.STATUS)) {
								for (int j = 0; j < ele.getChildren().size(); j++) {
									Element subEle = (Element) ele
											.getChildren().get(j);
									if (subEle.getName().equals(
											MMConstants.STATUSCODE))
										res
												.setStatusCode(Integer
														.parseInt(subEle
																.getTextTrim()));
									else if (subEle.getName().equals(
											MMConstants.STATUSTEXT))
										res.setStatusText(subEle.getTextTrim());
									else if (subEle.getName().equals(
											MMConstants.STATUSDETAIL))
										res.setStatusDetail(subEle
												.getTextTrim());
								}
							}
						}
						return res;
					}
				}
			}
		} catch (JDOMException jdome) {
			MM7RSErrorRes error = new MM7RSErrorRes();
			System.err.print(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();
			MM7RSErrorRes error = new MM7RSErrorRes();
			error.setStatusCode(-100);
			error.setStatusText("系统错误!原因:" + e);
			return error;
		}
	}

	private ByteArrayOutputStream getSendMessage(byte[] bcontent) {
		try {
			/**
			 * 在此加以判断,如果是长连接,则得到以前的连接,否则新建连接
			 */
			if (pool.getKeepAlive().equals("on")) {
				if (this.TimeOutFlag == true) {
					SevereBuffer.append("TimeOutFlag=true\r\n");
					if (pool.deleteURL(TimeOutWrap)) {
						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();
						this.TimeOutWrap = connWrap;
						if (connWrap != null) {
							SevereBuffer.append("  connWrap != null");
							client = connWrap.getSocket();
						} else {
							SevereBuffer.append("   client=null");
							client = null;
						}
					} else {
						SevereBuffer.append("deleteURL is false!");
						return null;
					}
				} else {
					// SevereBuffer.append("!767!TimeOutFlag==false");
					connWrap = pool.getConnWrap();
					this.TimeOutWrap = 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) {
							SevereBuffer
									.append("!771!connWrap.getUserfulFlag() == false || client.isConnected() == false");
							pool.deleteURL(connWrap);
							connWrap = pool.getConnWrap();
							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 {
				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 (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 {
				SevereBuffer.append("!853!client == null");
				return null;
			}
		} catch (UnknownHostException uhe) {
			// System.out.println("UnknownHostExcepion!原因:"+uhe);
			SevereBuffer.append("UnknownHostExcepion=" + uhe + "\r\n");
			return null;
		} catch (SocketException se) {
			// System.out.println("SocketException!原因:"+se);
			SevereBuffer.append("SocketException=" + se + "\r\n");
			return null;
		}
		/**
		 * 超时后,被捕获,认为该次发送失败,重新进行发送,当发送超过一定次数后, 即认为整个发送失败。返回失败信息。
		 */
		catch (InterruptedIOException iioe) {
			this.TimeOutFlag = true;
			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();
			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 (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) {
			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;
			int readlineOut = 1;
			int envelopecount = 0;
			while (1 > 0) {
				if (flag)
					break;
				readline = receiver.read(data);
				if (readline != -1) {
					baos.write(data, 0, readline);
					// 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")) {
								if (baos.toString().indexOf("Digest") != -1) {
									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(
																MMConstants.CONTENT_LENGTH);
												int conlen2 = baos.toString()
														.indexOf("\r", conlen1);
												contlen3 = Integer
														.parseInt(baos
																.toString()
																.substring(
																		(conlen1
																				+ MMConstants.CONTENT_LENGTH
																						.length() + 1),
																		conlen2)
																.trim());
												httpseper = baos.toString()
														.indexOf("\r\n\r\n");
												if (httpCode.equals("200")) {
													// 还要再加判断客户端是否是长连接
													ResendCount = 0;
													excuteFlag = true;

⌨️ 快捷键说明

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