Package pl.zgora.uz.wmie.fe.service.impl

Source Code of pl.zgora.uz.wmie.fe.service.impl.SmsService

package pl.zgora.uz.wmie.fe.service.impl;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.SocketAddress;
import java.net.URL;
import java.net.URLConnection;
import java.util.Properties;

/**
* Klasa sluzaca do wysylania sms
* @author mireksz
*
*@version 0.1
*/
public class SmsService implements pl.zgora.uz.wmie.fe.service.SmsService{
  private final static String ORANGE_GATEWAY = "http://sms.centertel.pl";
  private final static String PLUS_GATEWAY = "http://www.text.plusgsm.pl/sms/sendsms.php";
  private final static String ERA_GATEWAY = "http://www.era.pl";
  private final static String UNIWERSAL_GATEWAY = "http://sms.ikp.pl/?";
 

 
 
 
 
 
 
  public int sendSms(String phoneNumber, String topic, String message, String from) throws Exception{
    Object detectionResult = detectGateway(phoneNumber);
    System.out.println("********Sending SMS*********");
    int result = 0;
    if(detectionResult != null){
      if("plus".equals(detectionResult.toString())){
        result = sendSmsToPlus(phoneNumber, topic, message, from, 0);
      } else if("orange".equals(detectionResult.toString())){
        throw new Exception("not supperoder yet");
      } else if("era".equals(detectionResult.toString())){
        throw new Exception("not supperoder yet");
      }
    }
   
    if(result == 1){
      throw new Exception("SMS was not send");
    }
    return result;
  }
 

 
  private int sendSmsToPlus(String phoneNumber, String topic, String message, String from, int proxyNum) throws Exception{
    URL url = new URL (PLUS_GATEWAY);
    int errorCode = 0;
   
    URLConnection uConnection = getConnection(url, proxyNum);
    if(uConnection == null){
      return 1;
    }
    uConnection.setDoOutput(true);
   
//    String tekst = URLEncoder.encode("mirosąęł", "ISO-8859-2");
   
    DataOutputStream printout = new DataOutputStream (uConnection.getOutputStream());
      String content =
      "tprefix=" + phoneNumber.substring(0,3)+
      "&numer=" + phoneNumber.substring(3)+
      "&odkogo=" + from +
      "&tekst="+message;

      printout.writeBytes (content);
      printout.flush ();
      printout.close ();
     
      try {
      DataInputStream input = new DataInputStream(uConnection
          .getInputStream());

      InputStreamReader isr = new InputStreamReader(input);
      BufferedReader br = new BufferedReader(isr);

      String firstLine = br.readLine();

      br.close();
      input.close();
      String result = firstLine.substring(firstLine.indexOf("<title>"),
          firstLine.indexOf("</title>"));
      if (result.indexOf("nie") != -1) {
        errorCode = sendSmsToPlus(phoneNumber, topic, message, from,
            proxyNum + 1);
      }
    } catch (FileNotFoundException e) {
      errorCode = sendSmsToPlus(phoneNumber, topic, message, from,
          proxyNum + 1);
    } catch (java.io.IOException e) {
      errorCode = sendSmsToPlus(phoneNumber, topic, message, from,
          proxyNum + 1);
    }
    return errorCode;
  }
  //TODO pobierac serwery proxy z innej strony
  private URLConnection getConnection(URL url, int proxyNum) throws Exception{
    SocketAddress addr = null;
    Proxy proxy = null;
   
    switch (proxyNum) {
    case 0:
      System.out.println("Get default gateway");
      return url.openConnection();
    case 1:  
      System.out.println("Get 91.204.161.125 gateway");
      addr = new InetSocketAddress("91.204.161.125", 80);
      proxy = new Proxy(Proxy.Type.HTTP, addr);
      return url.openConnection(proxy)
    case 2:  
      System.out.println("Get 203.160.1.75 gateway");
      addr = new InetSocketAddress("203.160.1.75", 80);
      proxy = new Proxy(Proxy.Type.HTTP, addr);
      return url.openConnection(proxy)
    case 3:  
      System.out.println("Get 212.95.49.87 gateway");
      addr = new InetSocketAddress("212.95.49.87", 80);
      proxy = new Proxy(Proxy.Type.HTTP, addr);
      return url.openConnection(proxy);
    case 4:  
      System.out.println("Get 193.173.119.83 gateway");
      addr = new InetSocketAddress("193.173.119.83", 8080);
      proxy = new Proxy(Proxy.Type.HTTP, addr);
      return url.openConnection(proxy);
    case 5:  
      System.out.println("Get 159.148.82.3 gateway");
      addr = new InetSocketAddress("159.148.82.3", 3128);
      proxy = new Proxy(Proxy.Type.HTTP, addr);
      return url.openConnection(proxy);
    case 6:
      System.out.println("Get 194.44.170.81 gateway");
      addr = new InetSocketAddress("194.44.170.81", 3128);
      proxy = new Proxy(Proxy.Type.HTTP, addr);
      return url.openConnection(proxy);
    default:
      System.out.println("No another gateway");
      return null;
    }
   
  }
 
  private Object detectGateway(String phoneNumber) throws Exception{
    Properties properties = new Properties();
    properties.load(getClass().getClassLoader().getResourceAsStream("prefix.properties"));
    String prefix = phoneNumber.substring(0,3);
    return properties.get(prefix);
   
  }
}
TOP

Related Classes of pl.zgora.uz.wmie.fe.service.impl.SmsService

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.