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

📄 gsmservicehttpgateway.java

📁 华为编程开发规范与案例, 华为编程开发规范与案例,华为编程开发规范与案例
💻 JAVA
字号:
// SMSLib for Java v3
// A Java API library for sending and receiving SMS via a GSM modem
// or other supported gateways.
// Web Site: http://www.smslib.org
//
// SMSLib is distributed under the terms of the Apache License version 2.0
//
// Copyright (C) 2002-2007, Thanasis Delenikas, Athens/GREECE
// Portions Copyright:
// Davide Bettoni, Clusone/ITALY, dbettoni@users.sourceforge.net
// Tomek Cejner, Polland, heretique@users.sourceforge.net
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.smslib.gateway;

import java.net.URL;
import java.net.URLEncoder;
import java.util.Date;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.smslib.InboundMessage.MessageClass;
import org.apache.log4j.*;
import org.smslib.*;

/**
 * 
 * @author heretique
 */
/*
 * Gateway class for sending messages through http://www.gsmservice.pl HTTP
 * gateway. It may be useful only for Polish users.
 */
public class GSMServiceHTTPGateway extends AbstractGateway
{

	private String username, password;

	private Object sync;

	public GSMServiceHTTPGateway(String id, String username, String password, Logger logger)
	{
		super(id, logger);
		this.username = username;
		this.password = password;
		// sending only
		attributes = AbstractGateway.Attributes.SEND | AbstractGateway.Attributes.CUSTOMFROM;
		sync = new Object();
	}

	public void readMessages(LinkedList<InboundMessage> msgList, MessageClass msgClass) throws Exception
	{
		throw new UnsupportedOperationException("Not supported yet.");
	}

	public InboundMessage readMessage(String memLoc, int memIndex) throws Exception
	{
		throw new UnsupportedOperationException("Not supported yet.");
	}

	public void sendMessage(OutboundMessage msg) throws Exception
	{
		LinkedList<String> response;
		URL url;

		String file = String.format("/api/send.php?login=%s&pass=%s&showid=1&rodzaj=1&podpis=%s&numer=%s&tekst=%s", username, password, msg.getFrom(), msg.getRecipient(), URLEncoder.encode(msg.getText(), "ISO-8859-1"));

		if (msg.getFrom() != null)
		{
			file.concat("&podpis=" + URLEncoder.encode(msg.getFrom(), "ISO-8859-1"));
		}

		url = new URL("http", "bramka.gsmservice.pl", file);

		synchronized (sync)
		{
			response = HttpDriver.HttpGet(url);
		}

		String first = response.getFirst();
		// Valid response is one line, and should match following regex:
		// Status:(\d+)(\|(\d+))?
		//
		// Where matches:
		// [1] = status code
		// [3] = message id (may be null if url parameter "showid" is set to 0 or
		// omited)

		Matcher m = Pattern.compile("Status:(\\d+)(\\|(\\d+))?").matcher(first);
		if (m.matches())
		{
			msg.setRefNo(m.group(3));
			msg.setDispatchDate(new Date());
			int stat = Integer.parseInt(m.group(1));
			switch (stat) {
				case 3:		// message sent
				case 10:	// message in queue
				case 13:	// message delivered
					msg.setMessageStatus(OutboundMessage.MessageStatus.SENT);
					break;
				default:
					msg.setMessageStatus(OutboundMessage.MessageStatus.FAILED);
					break;
			}
			
		} else {
			throw new InvalidResponseException("Unknown response from provider: "+first);
		}
	}

	public void deleteMessage(InboundMessage msg) throws Exception
	{
		throw new UnsupportedOperationException("Not supported yet.");
	}

	public float queryBalance() throws Exception
	{
		throw new UnsupportedOperationException("Unsupported Gateway operation!");
	}

	public boolean queryCoverage(OutboundMessage msg) throws Exception
	{
		throw new UnsupportedOperationException("Unsupported Gateway operation!");
	}

	protected void sendMessage(LinkedList<OutboundMessage> msgList) throws Exception
	{
		for (int i = 0; i < msgList.size(); i++)
			sendMessage(msgList.get(i));
	}
}

⌨️ 快捷键说明

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