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

📄 httpserver.java

📁 发送短信 接收短信 多种接口com/net/modem 开发库
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
				tpl = "<p style='text-align: center;'>Version: _VERSION<br />Uptime: since _START, _D days, _H hours, _M minutes</p>";
				line = tpl.replaceAll("_VERSION", Library.getLibraryVersion());
				line = line.replaceAll("_START", new Date(getSMSServer().getService().getStartMillis()).toString());
				msMillisDiff = System.currentTimeMillis() - getSMSServer().getService().getStartMillis();
				days = (int) (msMillisDiff / (24 * 60 * 60 * 1000));
				msMillisDiff = msMillisDiff - (days * (24 * 60 * 60 * 1000));
				hours = (int) (msMillisDiff / (60 * 60 * 1000));
				msMillisDiff = msMillisDiff - (hours * (60 * 60 * 1000));
				minutes = (int) (msMillisDiff / (60 * 1000));
				line = line.replaceAll("_D", "" + days);
				line = line.replaceAll("_H", "" + hours);
				line = line.replaceAll("_M", "" + minutes);
				body.println(line);
				tpl = "<p style='font-size: 1.3em; font-weight: bold; text-align: center;'>Queued Messages: _N</p>";
				line = tpl.replaceAll("_N", "" + getSMSServer().getService().getQueueManager().getQueueLoad());
				body.println(line);
			}
			body.println("<p style='font-size: 1.3em; font-weight: bold; text-align: center;'>Gateway Status</p>");
			body.println("<center>");
			body.println("<table id='box-table-a' style='width: 750px;'>");
			body.println("<tr><th></th><th></th><th></th><th></th><th scope='col' colspan='2' style='text-align: center;'>Total Traffic</th><th></th><th></th><th></th></tr>");
			body.println("<tr><th>Name</th><th>Type</th><th style='text-align: center;'>IN?</th><th style='text-align: center;'>OUT?</th><th style='text-align: right;'>Inbound</th><th style='text-align: right;'>Outbound</th><th style='text-align: right;'>Queued</th><th style='text-align: right;'>Restarts</th><th style='text-align: center;'>Status</th></tr>");
			{
				String line;
				for (AGateway gateway : getSMSServer().getService().getGateways())
				{
					line = "<tr><td>_NAME</td><td>_CLASS</td><td style='text-align: center;'>_ISIN</td><td style='text-align: center;'>_ISOUT</td><td style='text-align: right;'>_INBOUND</td><td style='text-align: right;'>_OUTBOUND</td><td style='text-align: right;'>_QUEUED</td><td style='text-align: right;'>_RESTART</td><td style='text-align: center;'>_STATUS</td></tr>";
					line = line.replaceAll("_NAME", gateway.getGatewayId());
					line = line.replaceAll("_CLASS", gateway.getClass().getName());
					line = line.replaceAll("_ISIN", (gateway.isInbound() ? "Yes" : "No"));
					line = line.replaceAll("_ISOUT", (gateway.isOutbound() ? "Yes" : "No"));
					line = line.replaceAll("_INBOUND", "" + gateway.getInboundMessageCount());
					line = line.replaceAll("_OUTBOUND", "" + gateway.getOutboundMessageCount());
					line = line.replaceAll("_QUEUED", "" + getSMSServer().getService().getQueueManager().getQueueLoad(gateway.getGatewayId()));
					line = line.replaceAll("_RESTART", "" + gateway.getRestartCount());
					switch (gateway.getStatus())
					{
						case STOPPING:
							line = line.replaceAll("_STATUS", "" + "Stopping...");
							break;
						case STOPPED:
							line = line.replaceAll("_STATUS", "" + "Stopped");
							break;
						case FAILURE:
							line = line.replaceAll("_STATUS", "" + "Failure");
							break;
						case RESTART:
							line = line.replaceAll("_STATUS", "" + "Restarting");
							break;
						case STARTING:
							line = line.replaceAll("_STATUS", "" + "Starting...");
							break;
						case STARTED:
							line = line.replaceAll("_STATUS", "" + "Started");
							break;
					}
					body.println(line);
				}
			}
			body.println("</table>");
			body.println("</center>");
			body.println("<br />");
			body.println("<p style='font-size: 1.3em; font-weight: bold; text-align: center;'>Interface Status</p>");
			body.println("<center>");
			body.println("<table id='box-table-a' style='width: 500px;'>");
			//body.println("<tr><th></th><th></td><th></th><th></th><th scope='col' colspan='2' style='text-align: center;'>Total Traffic</th></tr>");
			body.println("<tr><th>Name</th><th>Type</th><th style='text-align: center;'>IN?</th><th style='text-align: center;'>OUT?</th><th style='text-align: center;'>Queue</th></tr>");
			{
				String line;
				for (Interface<? extends Object> inf : getSMSServer().getInfList())
				{
					line = "<tr><td>_NAME</td><td>_CLASS</td><td style='text-align: center;'>_ISIN</td><td style='text-align: center;'>_ISOUT</td><td style='text-align: center;'>_QUEUE</td></tr>";
					line = line.replaceAll("_NAME", inf.getId());
					line = line.replaceAll("_CLASS", inf.getClass().getName());
					line = line.replaceAll("_ISIN", (inf.isInbound() ? "Yes" : "No"));
					line = line.replaceAll("_ISOUT", (inf.isOutbound() ? "Yes" : "No"));
					line = line.replaceAll("_QUEUE", (inf.getPendingMessagesToSend() == -1 ? "N/A" : "" + inf.getPendingMessagesToSend()));
					body.println(line);
				}
			}
			body.println("</table>");
			body.println("</center>");
			body.println("</body>");
			body.println("</html>");
			((Request) getRequest()).setHandled(true);
		}
	}

	public class Read extends AHttpCommand
	{
		public Read(SMSServer myServer, String myTarget, HttpServletRequest myRequest, HttpServletResponse myResponse, int myDispatch)
		{
			super(myServer, myTarget, myRequest, myResponse, myDispatch);
		}

		@Override
		public void process() throws IOException
		{
			String gateway;
			String password;
			
			PrintWriter body = getResponse().getWriter();
			password = getRequest().getParameter("password");
			if (password != null && password.equalsIgnoreCase(getProperty("password.read", "")))
			{
				List<InboundMessage> msgList = new ArrayList<InboundMessage>();
				getResponse().setContentType("text/xml");
				getResponse().setStatus(HttpServletResponse.SC_OK);
				gateway = getRequest().getParameter("gateway");
				try
				{
					if ((gateway != null) && (gateway.length() != 0)) getSMSServer().getService().readMessages(msgList, MessageClasses.ALL, gateway);
					else getSMSServer().getService().readMessages(msgList, MessageClasses.ALL);
					body.println("<?xml version='1.0' encoding='UTF-8'?>");
					body.println("<messages>");
					body.printf("<error>0</error>", 0);
					for (InboundMessage msg : msgList)
					{
						body.println("<message>");
						if (msg.getType() == MessageTypes.INBOUND)
						{
							body.printf("<message_type>%s</message_type>", "Inbound");
							body.printf("<message_date>%s</message_date>\n", getDateAsISO8601(msg.getDate()));
							body.printf("<originator>%s</originator>\n", msg.getOriginator());
							body.printf("<text>%s</text>\n", msg.getText());
							body.printf("<encoding>%s</encoding>\n", msg.getEncoding());
							body.printf("<gateway>%s</gateway>\n", msg.getGatewayId());
						}
						body.println("</message>");
					}
					body.println("</messages>");
				}
				catch (Exception e)
				{
					body.println("<messages>");
					body.printf("<error>%d</error>", ERR_INTERNAL_ERROR);
					body.println("</messages>");
				}
			}
			else
			{
				getResponse().setContentType("text/xml");
				getResponse().setStatus(HttpServletResponse.SC_OK);
				body.println("<?xml version='1.0' encoding='UTF-8'?>");
				body.println("<messages>");
				body.printf("<error>%d</error>", ERR_WRONG_PASSWORD);
				body.printf("<error_description>%s</error_description>", "Invalid password.");
				body.println("</messages>");
			}
			((Request) getRequest()).setHandled(true);
		}
	}

	public class Send extends AHttpCommand
	{
		public Send(SMSServer myServer, String myTarget, HttpServletRequest myRequest, HttpServletResponse myResponse, int myDispatch)
		{
			super(myServer, myTarget, myRequest, myResponse, myDispatch);
		}

		@Override
		public void process() throws IOException
		{
			boolean foundErrors = false;
			String recipient, text, parm;
			String password;
			
			PrintWriter body = getResponse().getWriter();
			password = getRequest().getParameter("password");
			if (password != null && password.equalsIgnoreCase(getProperty("password.send", "")))
			{
				getResponse().setContentType("text/xml");
				getResponse().setStatus(HttpServletResponse.SC_OK);
				recipient = getRequest().getParameter("recipient");
				text = getRequest().getParameter("text");
				if (((recipient == null) || (recipient.length() == 0)) || ((text == null) || (text.length() == 0)))
				{
					foundErrors = true;
					pushResponse(body, ERR_MISSING_PARMS, "Missing Parameters.");
				}
				else
				{
					OutboundMessage msg = new OutboundMessage(recipient, text);
					parm = getRequest().getParameter("encoding");
					if ((parm != null) && parm.length() != 0)
					{
						if (parm.equalsIgnoreCase("7")) msg.setEncoding(MessageEncodings.ENC7BIT);
						else if (parm.equalsIgnoreCase("8")) msg.setEncoding(MessageEncodings.ENC8BIT);
						else if (parm.equalsIgnoreCase("U")) msg.setEncoding(MessageEncodings.ENCUCS2);
						else
						{
							foundErrors = true;
							pushResponse(body, ERR_INVALID_PARMS, "Invalid encoding requested.");
						}
					}
					parm = getRequest().getParameter("priority");
					if ((parm != null) && (parm.length() != 0))
					{
						try
						{
							msg.setPriority(Integer.parseInt(parm));
						}
						catch (Exception e)
						{
							foundErrors = true;
							pushResponse(body, ERR_INVALID_PARMS, "Invalid priority requested.");
						}
					}
					parm = getRequest().getParameter("from");
					if ((parm != null) && (parm.length() != 0)) msg.setFrom(parm);
					parm = getRequest().getParameter("gateway");
					if ((parm != null) && (parm.length() != 0)) msg.setGatewayId(parm);
					if (!foundErrors)
					{
						try
						{
							getSMSServer().getService().sendMessage(msg);
							pushResponse(body, msg);
						}
						catch (Exception e)
						{
							pushResponse(body, ERR_INTERNAL_ERROR, e.getMessage());
						}
					}
				}
			}
			else
			{
				pushResponse(body, ERR_WRONG_PASSWORD, "Invalid password.");
			}
			((Request) getRequest()).setHandled(true);
		}

		private void pushResponse(PrintWriter body, OutboundMessage msg)
		{
			body.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
			body.println("<send>");
			if (msg.getMessageStatus() == MessageStatuses.SENT)
			{
				body.printf("<error>%d</error>", 0);
			}
			else
			{
				body.printf("<error>%d</error>", ERR_SEND_ERROR);
				body.printf("<error_description>%s</error_description>", "Message not sent.");
			}
			body.printf("<message_status>%s</message_status>", msg.getMessageStatus());
			if (msg.getMessageStatus() == MessageStatuses.FAILED) body.printf("<failure_cause>%s</failure_cause>", msg.getFailureCause());
			body.printf("<ref_no>%s</ref_no>", msg.getRefNo());
			body.printf("<gateway>%s</gateway>", msg.getGatewayId());
			body.println("</send>");
		}

		private void pushResponse(PrintWriter body, int errNo, String errMessage)
		{
			body.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
			body.println("<send>");
			body.printf("<error>%d</error>", errNo);
			body.printf("<error_description>%s</error_description>", errMessage);
			body.println("</send>");
		}
	}

	protected String getDateAsISO8601(Date date)
	{
		if (date == null) date = new Date();
		String result = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").format(date);
		StringBuilder sb = new StringBuilder(result.length() + 1);
		sb.append(result.substring(0, result.length() - 2));
		sb.append(":");
		sb.append(result.substring(result.length() - 2));
		return sb.toString();
	}
}

⌨️ 快捷键说明

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