WebService se vrti na jboss 4.0.4 AS.
Veoma je jednostavan, nudi klijentima mogucnost dostave odredjenih alerta na mail i to realizuje preko SMTP.
Sve funkcionise kako je zamisljeno, jedino mi u logu nedostaje informacija sa koje IP adrese je pozvan servis.
Verujem da je ovo trivijalno, ako moze neki hint (ili resenje ;-))
Btw. javlja mi da je linija koja sadrzi "responseLine = is.readLine()) != null " deprecated, sa cim da zamenim readLine()?
Kod sledi:
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.ws.*;
import org.apache.log4j.*;
import java.io.*;
import java.net.*;
@Stateless()
@WebService()
public class msgSender {
static Category Log = Category.getInstance(msgSender.class.getName());
@WebMethod
public String send(@WebParam(name = "kome") String kome,
@WebParam(name = "tekst") String tekst,
@WebParam(name = "od") String od)
{
String response = null;
int rez = msgcComm(kome,tekst,od);
if (rez == 1)
{
Log.info("Status:"+rez+"(OK); From:"+od+"; To:"+kome+"; Msg:{"+tekst+"}");
}
else
{
Log.info("Status:"+rez+"(MSGNOTSENT); From:"+od+"; To:"+kome+"; Msg:{"+tekst+"}");
}
return rez;
}
protected int msgcComm (String id, String txt, String from)
{
String host = "server.domen.com";
Socket smtpSocket = null;
DataOutputStream os = null;
DataInputStream is = null;
try {
smtpSocket = new Socket(host, 25);
os = new DataOutputStream(smtpSocket.getOutputStream());
is = new DataInputStream(smtpSocket.getInputStream());
} catch (UnknownHostException e1) {
return -10;
} catch (IOException e2) {
return -20;
}
if (os != null && is != null) {
try {
os.writeBytes("HELO\r\n");
os.writeBytes("MAIL From:"+from+"\r\n");
os.writeBytes("RCPT To:"+id+"@server.domen.com\r\n");
os.writeBytes("DATA\r\n");
os.writeBytes(txt+"\r\n");
os.writeBytes("\r\n.\r\n");
os.writeBytes("QUIT");
String responseLine;
while ((responseLine = is.readLine()) != null) {
if (responseLine.indexOf("accepted") != -1)
{
os.close();
is.close();
smtpSocket.close();
return 1;
}
if (responseLine.indexOf("Error") != -1)
{
os.close();
is.close();
smtpSocket.close();
return -1;
}
}
} catch (UnknownHostException e3) {
return -30;
} catch (IOException e4) {
return -40;
}
}
return -100;
}
}