2016-10-11 3 views
0

로그인 나는 넷빈즈에서 개발 WebService에 있고 같이 :SOAP 메시지를 확보하고 자바

import _504.iec62325.wss._1._0.MsgFaultMsg; 
import ch.iec.tc57._2011.schema.message.ErrorType; 
import ch.iec.tc57._2011.schema.message.FaultMessageType; 
import ch.iec.tc57._2011.schema.message.HeaderType; 
import ch.iec.tc57._2011.schema.message.ReplyType; 
import ch.iec.tc57._2011.schema.message.ResponseMessageType; 
import javax.jws.WebService; 
import javax.xml.ws.BindingType; 
import ch.iec.tc57._2011.schema.message.RequestMessageType; 
... 

@WebService(serviceName = "ServiceEME", portName = "Service_EME_Port", 
     endpointInterface = "_504.iec62325.wss._1._0.PortTFEDIType", 
     targetNamespace = "urn:iec62325.504:wss:1:0", 
     wsdlLocation = "WEB-INF/wsdl/ServiceEME/servicioCS.wsdl") 
@BindingType(value = "http://java.sun.com/xml/ns/jaxws/2003/05/soap/bindings/HTTP/") 

public class ServiceEME 
{ 
    @Resource 

    private static final String CONTEXT = "PRODUCTION"; 
    private final String DATA_TYPE = "datatype"; 
    private final String SERVER_TIMESTAMP = "servertimestamp"; 
    private final String VERB_GET = "get"; 
    private final String NOUN_QUERYDATA = "querydata"; 
    private static final String NOUN_LIST = "messagelist"; 
    private static final String NOUN_ANY = "any"; 

    private static enum TipoMensaje { 
     QUERY_DATA, 
     MESSAGE_LIST, 
     MESSAGE_GET; 
    } 

    public ResponseMessageType request (RequestMessageType parameter) 
             throws MsgFaultMsg 
    { 
     Document msgAsDocument = null; 
     RSAPrivateKey privateKey = null; 
     X509Certificate cert = null; 
     try 
     { 
      switch(getMessageType(parameter)) 
      { 
       case QUERY_DATA : 
        return QueryData.procesar(parameter); 
       case MESSAGE_LIST : 
        return MessagesList.procesar(parameter); 
       case MESSAGE_GET : 
        return GetMessage.procesar(parameter); 
       default: 
        throw componerError("Error", "Error"); 
      } 
     } 
     catch (InterpretaInputException ex) 
     { 
      throw componerError(ex.getCode(), ex.getMessage()); 
     } 
    } 
... 

public static void signDocument(final Document msgAsDocument, final 
    RSAPrivateKey privateKey, final X509Certificate cert) 
               throws InterpretaInputException 
    { 
     try 
     { 
      XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM"); 
      List<Transform> trfLst = new ArrayList<>(); 
      trfLst.add(fac.newTransform(Transform.ENVELOPED, 
               (TransformParameterSpec) null)); 
      trfLst.add(fac.newCanonicalizationMethod(CanonicalizationMethod. 
            INCLUSIVE, (C14NMethodParameterSpec) null)); 
      Reference ref = fac.newReference("", fac.newDigestMethod 
            (DigestMethod.SHA1,null), trfLst, null, null); 
      SignedInfo si = fac.newSignedInfo(fac.newCanonicalizationMethod 
       (CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec) null), 
       fac.newSignatureMethod(SignatureMethod.RSA_SHA1, null), 
       Collections.singletonList(ref)); 
      Node headerNode = null; 
      NodeList nl = msgAsDocument.getElementsByTagNameNS 
          ("http://iec.ch/TC57/2011/schema/message", "Header"); 

      if (nl.getLength() == 1) 
      { 
       headerNode = nl.item(0); 
      } 
      else 
      {//Header error 
       throw new InterpretaInputException(
        Constants.ERROR_QRY_004_CODE, Constants.ERROR_QRY_004_DETAIL); 
      } 

      DOMSignContext dsc = new DOMSignContext(privateKey, headerNode); 
      KeyInfoFactory keyInfoFactory = fac.getKeyInfoFactory(); 
      List<Object> x509Content = new ArrayList<>(); 

      x509Content.add(keyInfoFactory.newX509IssuerSerial(cert. 
           getIssuerDN().getName(), cert.getSerialNumber())); 
      x509Content.add(cert.getSubjectX500Principal().getName()); 
      x509Content.add(cert); 
      X509Data xd = keyInfoFactory.newX509Data(x509Content); 

      KeyInfo keyInfo = keyInfoFactory.newKeyInfo 
               (Collections.singletonList(xd)); 
      XMLSignature signature = fac.newXMLSignature(si, keyInfo); 

      signature.sign(dsc); 
     } 
     catch (Exception e) 
     { //Simplified exception handling for brevity 
      throw new InterpretaInputException(
        Constants.ERROR_HAND_009_CODE, Constants.ERROR_HAND_009_DETAIL); 
     } 
    } 
} 

문제없이 서비스 응답을하지만 지금은 서버 인증서를 기반으로 서명을 추가해야합니다. 이 서명은 코드에서 볼 수 있듯이 signDocument 함수를 통해 생성됩니다.

내 문제는 사용자 지정 응답 "ResponseMessageType"이 있고이 응답을 문서 형식으로 변환하여 서명 태그를 생성하고 메시지에 추가하는 방법을 모른다는 것입니다. 내가 msgAsDocument 내 ResponseMessageType를 변환하는 방법을 잘 모릅니다 때문에 이 세 가지 항목

Document msgAsDocument = null; 
RSAPrivateKey privateKey = null; 
X509Certificate cert = null; 

는 emtpy된다.

다른 사이트를 읽고 어쩌면 SOAP 메시지를 가로 채기 위해 처리기를 사용해야하지만 내 대답을 가로 채고 메시지를 보내기 전에 그것을 소개하는 방법을 알아낼 수 있습니다.

미리 감사드립니다.

답변

0

처리기를 추가하는 문제가 해결되었습니다.

public class SignHandler implements SOAPHandler<SOAPMessageContext> 
.... 
@Override 
public boolean handleMessage(SOAPMessageContext context) 
{ 
    //System.out.println("Server : handleMessage()......"); 
    Boolean isResponse = (Boolean) context.get(MessageContext. 
               MESSAGE_OUTBOUND_PROPERTY); 
    if(isResponse) 
    { 
     try 
     { 
      SOAPMessage soapMsg = context.getMessage(); 
      Source source = soapMsg.getSOAPPart().getContent(); 
      Node root = null; 
... 

나를위한 새로운 개념이 너무 많습니다.

감사합니다.

관련 문제