2012-06-12 3 views
0

Я реализую WS-агент, который будет управлять запросами на мыло к ответам от PCRF. Соединение осуществляется через порт https и используется Java Key Store (инструмент для хранения ключей) для генерации a key-store file from a trusted certificate. Соединение кажется ОК (я не уверен, что аутентификация прошла успешно или нет), но возвращенная информация (ответ) является короткой нечитаемой строкой.Проблема с запросом-ответом веб-служб Java

Та же процедура через приложение SoapUI работает нормально.

Единственная разница между обеими процедурами заключается в том, что к 1-му я использую хранилище ключей, а вторым я пользуюсь доверенным сертификатом.

Как я могу решить это? Я не понимаю, где что-то не так.

Если это поможет, я могу поделиться источником Java, используемым для отправителя SOAP-сообщений, и ответами, полученными от PSRF, в SoapUI и в реализованный WS.

package SOAPSender; 

import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.net.Socket; 

public class DebugMain { 

    public static void main(String[] args) { 
     //PCRFProvisioningAgent temp = new PCRFProvisioningAgent(); 

      loadKeyAndTrustStore(); 

      StringBuffer outputStream = new StringBuffer(); 
      BufferedWriter wr = null; 
      BufferedReader rd = null; 
      Socket sock = null; 
      String outputBuffer = null; 

      try { 

       // Create socket 
       sock = new Socket("10.96.227.219", 8080); 

       // Create header 
       wr = new BufferedWriter(new OutputStreamWriter(
         sock.getOutputStream(), "UTF-8")); 

       String xmlData = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:rm=\"rm:soap\">"+ 
          "<soapenv:Header/>"+ 
          "<soapenv:Body>"+ 
           "<rm:getSubscriber>"+ 
           "<inPara>"+ 
            "<subscriber>"+ 
             "<attribute>"+ 
              "<key>USRIDENTIFIER</key>"+ 
              "<value>284090000000004</value>"+ 
             "</attribute>"+ 
            "</subscriber>"+ 
           "</inPara>"+ 
           "</rm:getSubscriber>"+ 
          "</soapenv:Body>"+ 
         "</soapenv:Envelope>";    

       wr.write("POST https://10.96.227.219:8080/axis/services/ScfPccSoapServiceEndpointPort HTTP/1.1\r\n"); 
       wr.write("User-Agent: https://10.96.227.219:8080/axis/services/ScfPccSoapServiceEndpointPort\r\n"); 
       wr.write("Content-Length: " + xmlData.length() + "\r\n"); 
       wr.write("Content-Type: text/xml;charset=UTF-8\r\n"); 
//    wr.write("SOAPAction: \"rm:soap/ScfPccSoapServiceEndpoint/getSubscriberRequest\"\r\n"); 
       wr.write("SOAPAction: \"\"\r\n"); 
       wr.write("\r\n"); 

       // Send data 
       wr.write(xmlData); 
       wr.flush(); 

       // Read response 
       // exception handler - when connection is reset instead of close 
       // after sending a packet from source 
       char cbuf[] = new char[4096]; 
       int i = 0; 
       // buffer is sized to max 4096 packet size 

       try { 
        rd = new BufferedReader(new InputStreamReader(
          sock.getInputStream(), "UTF-8")); 
        while ((i = rd.read(cbuf)) != -1) { 
         outputStream.append(cbuf, 0, i); 
         int contStartIndex = outputStream.toString().indexOf(
           "Content-Length: ") 
           + "Content-Length: ".length(); 
         int contEndIndex = outputStream.toString().indexOf("\n", 
           contStartIndex) - 1; 
         if (outputStream.toString().indexOf("Content-Length: ") != -1) { 
          int contLength = Integer.valueOf(
            outputStream.toString().substring(
              contStartIndex, contEndIndex)) 
            .intValue(); 
          int headerLength = outputStream 
            .toString() 
            .substring(
              0, 
              outputStream.toString().indexOf(
                "\n\r\n")).length() + 3; 
          // if the message body is complete but there is not an 
          // ending character 
          // while will break 
          // warning - with national characters! content length is 
          // count of bytes not chars 
          if (i - headerLength >= contLength) 
           break; 
         } 
        } 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
       System.out.println("--------\n" + String.valueOf(cbuf) 
         + "\n--------------"); 
       outputBuffer = outputStream.toString(); 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } finally { 
       try { 
        if (wr != null) 
         wr.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       try { 
        if (rd != null) 
         rd.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       try { 
        if (sock != null) 
         sock.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
    } 

    private static void loadKeyAndTrustStore() { 
     try { 
      // System.out.println(Constants.ROOT_PATH + Constants.STORE_FILE); 
      // AdminLogger.error("Certificate file: " + Constants.ROOT_PATH 
      // + Constants.STORE_FILE); 

      // System load ssl the file of certificate 
      // Load Key store 

      System.out.println(System.getProperty("javax.net.ssl.keyStoreType")); 
      System.setProperty("javax.net.ssl.keyStoreType", "****"); 
      System.out.println(System.getProperty("javax.net.ssl.keyStoreType")); 

      System.out.println(System.getProperty("javax.net.ssl.keyStore")); 
      System.setProperty("javax.net.ssl.keyStore", 
        "****"); 
      System.out.println(System.getProperty("javax.net.ssl.keyStore")); 

      System.out.println(System.getProperty("javax.net.ssl.keyStorePassword")); 
      System.setProperty("javax.net.ssl.keyStorePassword", "****"); 
      System.out.println(System.getProperty("javax.net.ssl.keyStorePassword")); 


      // TrustStore 

      System.out.println(System.getProperty("javax.net.ssl.trustStore")); 
      System.setProperty("javax.net.ssl.trustStore", 
        "****"); 
      System.out.println(System.getProperty("javax.net.ssl.trustStore")); 

      System.out.println(System.getProperty("javax.net.ssl.trustStorePassword")); 
      System.setProperty("javax.net.ssl.trustStorePassword", "****"); 
      System.out.println(System.getProperty("javax.net.ssl.trustStorePassword")); 

     } catch (Exception ex) { 
      // AdminLogger.error(ex, "StartupServlet.loadKeyAndTrustStore"); 
     } 
    } 

} 

Это ответ:

... и вот ответ от SoapUI приложения:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <soapenv:Body> 
     <getSubscriberResponse xmlns="rm:soap"> 
     <result xmlns=""> 
      <resultCode>12302</resultCode> 
      <paras> 
       <key>errorDescription</key> 
       <value>The subscriber does not exist.</value> 
      </paras> 
     </result> 
     </getSubscriberResponse> 
    </soapenv:Body> 
</soapenv:Envelope> 

ответ

1

Sockets не идеальный способ вызова веб-службы от Java. Используйте Java API, например JAX-WS. Получите WSDL из веб-службы PSRF и создайте клиентские заглушки, используя команду ws-import.

В целях безопасности вам необходимо использовать API безопасности JAX-WS для вызова веб-сервиса, защищенного с помощью WS-Security.

Смотрите эту тему - jax-ws-consuming-web-service-with-ws-security-and-ws-addressing

Спасибо, Sreehari.

+0

Я не упоминал, что система с отправкой этих SOAP-сообщений на PCRF настроена с помощью java-версии 1.4.2! – nenito

0

Как отмечалось в @sreehari, Sockets является менее идеальным для вызова веб-сервисов - вам в основном придется реализовать всю необходимую информацию о протоколе приложения (например, HTTP/S) самостоятельно. Само по себе это грандиозное начинание.

Если вы хотите остаться «низкоуровневым» и сами создавать запросы, я предлагаю вам «обновить» до использования HttpsUrlConnection.

Смежные вопросы