📄 nokiasender.java
字号:
/**
* MMSLIB - A Java Implementation of the MMS Protocol
* Copyright (C) 2004 Simon Vogl
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package at.jku.soft.mms.nokia;
import java.util.*;
import net.sourceforge.jwap.wsp.*;
import java.net.*;
import java.io.*;
import net.sourceforge.jwap.util.*;
import java.lang.*;
import com.nokia.mms.*;
public class NokiaSender implements IWSPUpperLayer{
// the URL of the WAP-Gateway and its port.
private InetAddress wapgw;
private int wapgwport;
// the URI of the server to post to
private String posturi;
// the bytes to be posted
private byte[] bytes;
private String contentType;
// holds the WAP session we open to send the message (from connect till disconnect)
private CWSPSession s;
// holds the Method transaction for posting (from m_send_req till m_send_conf)
private CWSPMethodManager m;
/**
* Constructs a WAP sender object.
*
* @param wapgw the wap gateway to use
* @param wapgwport the port of the wap gateway
*/
public NokiaSender(InetAddress wapgw, int wapgwport, String posturi,
byte[] dataToSend, String contentType){
this.bytes = dataToSend;
this.posturi = posturi;
this.wapgw = wapgw;
this.wapgwport = wapgwport;
this.contentType = contentType;
if ( bytes == null) {
encode();
}
init();
}
/**
* This method connects to the wap gateway and opens a WSP session.
*/
private void init()
{
try
{
s = new CWSPSession(wapgw, wapgwport, this);
System.out.println("connecting to WAP gateway " + wapgw);
s.s_connect();
}
catch (Exception e)
{
e.printStackTrace();
}
}
private byte[] readFile(String file)
{
byte b[] = null;
try {
File f = new File(file);
long len = f.length();
b = new byte[(int)len];
FileInputStream fi = new FileInputStream(f);
fi.read(b);
} catch (Exception e) {
e.printStackTrace();
}
return b;
}
private void encode()
{
// code to retrieve ore create the Multimedia Message
MMMessage mm=new MMMessage();
mm.setVersion(IMMConstants.MMS_VERSION_10);
mm.setMessageType(IMMConstants.MESSAGE_TYPE_M_SEND_REQ);
mm.setTransactionId("tid_01");
mm.setDate(new Date(System.currentTimeMillis()));
mm.setFrom("+4369914219503/TYPE=PLMN");
mm.addToAddress("+4369914219500/TYPE=PLMN");
mm.addCcAddress("+436502323555/TYPE=PLMN");
//mm.addToAddress("+4369911111010/TYPE=PLMN");
mm.setSubject("test mms");
mm.setMessageClass(IMMConstants.MESSAGE_CLASS_PERSONAL);
mm.setPriority(IMMConstants.PRIORITY_HIGH);
mm.setContentType(IMMConstants.CT_APPLICATION_MULTIPART_MIXED);
// In case of multipart related message and a smil presentation available
// mm.setContentType(IMMConstants.CT_APPLICATION_MULTIPART_RELATED);
// mm.setMultipartRelatedType(IMMConstants.CT_APPLICATION_SMIL);
// mm.setPresentationId("<A0>"); // where <A0> is the id of the content containing the SMIL presentation
MMContent part1=new MMContent();
// read the file with readFile() that is a function that reads a file and
// returns an array of bytes
byte [] buf1 = readFile("text.txt");
part1.setContent(buf1,0,buf1.length);
part1.setContentId("text.txt");
part1.setType(IMMConstants.CT_TEXT_PLAIN);
mm.addContent(part1);
MMContent part2=new MMContent();
byte [] buf2 = readFile("img1.jpg");
part2.setContent(buf2,0,buf2.length);
part2.setContentId("img1.jpg");
part2.setType(IMMConstants.CT_IMAGE_JPEG);
mm.addContent(part2);
// instantiate an encoder object
MMEncoder encoder=new MMEncoder();
// set the message to be encoded
encoder.setMessage(mm);
// encode the message
try {
encoder.encodeMessage();
} catch (MMEncoderException e) {
System.err.println("An error occurred encoding the message.");
e.printStackTrace();
System.exit(1);
}
bytes=encoder.getMessage();
System.out.println("encoded");
}
/**
* Implementation of IWSPUpperLayer.
* Is called by the WAP stack after being connected.
* We then send the Message in a POST.
*/
public void s_connect_cnf()
{
try
{
System.out.println("connected to WAP gateway " + wapgw);
System.out.println("sending data (WSP POST)");
m = s.s_post(bytes,
contentType,
posturi);
}
catch (Exception e )
{
e.printStackTrace();
}
}
/**
* Implementation of IWSPUpperLayer.
* this method is called by the wap stack after a response of a method is received.
* @param payload the payload from the response
* @param contentType the content type of the response
* @param moreData will there come more data belonging to this result?
*/
public void s_methodResult_ind(byte[] payload,
String contentType,
boolean moreData)
{
// decode the response here...
String nl = System.getProperty("line.separator");
System.out.println("Response More Data: " + moreData );
System.out.println("Response Content Type: " + contentType );
saveToFile(contentType + "_" + System.currentTimeMillis() +".pdu", payload);
if (!moreData) {
System.out.println("disconnecting from WAP gateway " + wapgw);
m.s_methodResult(null);
s.s_disconnect();
MMSPDU pdu = new MMSPDU();
pdu.setPayload(payload);
}
}
/**
* Implementation of IWSPUpperLayer. NOT USED.
* @param reason
*/
public void s_suspend_ind(short reason){}
/**
* Implementation of IWSPUpperLayer. NOT USED.
*/
public void s_resume_cnf(){}
/**
* Implementation of IWSPUpperLayer.
* Called by the WAP stack if we are disconnected.
* @param reason the reason, why we are disconnected
*/
public void s_disconnect_ind(short reason)
{
System.out.println("disconnected from WAP gateway " + wapgw);
}
/**
* Implementation of IWSPUpperLayer. NOT USED.
* if we receive a redirect from the server.
* @param redirectInfo
*/
public void s_disconnect_ind(Vector redirectInfo){
System.out.println("disconnecting from WAP gateway " + wapgw);
this.m.s_methodResult(null);
s.s_disconnect();
}
public void s_disconnect_ind(InetAddress[] redirectInfo){
System.out.println("---redirected to " + redirectInfo.toString());
}
private void saveToFile(String filename, byte[]b) {
try{
FileOutputStream fo;
fo = new FileOutputStream(filename);
fo.write(b);
fo.close();
} catch (Exception e){
// Host address not ok
e.printStackTrace();
System.exit(1);
}
}
/**
* It automatically connects to the WAP gateway.
* After the connect.conf it sends the WAP POST.
* After all it disconnects from the gateway.
*
* @param args none (ignored)
*/
public static void main(String[] args)
{
try {
InetAddress gateway = InetAddress.getByName("194.24.131.17");
NokiaSender poster =
new NokiaSender(
gateway,
9201,
"http://mmsc.one.at/mms/wapenc",
null,
"application/vnd.wap.mms-message");
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -