package eu.osci.messagetypes;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.junit.Before;
import org.junit.Test;
import org.xml.sax.SAXException;

import eu.osci.helper.EndpointLocator;
import eu.osci.messageparts.OSCIFragmentCreator;


/**
 * Copyright © 2010 Freie Hansestadt Bremen. <br>
 * Created by bremen online services Entwicklungs- und Betriebsgesellschaft mbH & Co. KG. <br>
 * This library (Starter Kit) can be used by anybody according to the Bremer Lizenz für freie
 * Softwarebibliotheken. The license terms can be viewed at www.osci.de or can be ordered at the
 * OSCI-Leitstelle (Die Senatorin für Finanzen, Freie Hansestadt Bremen, Postfach 10 15 40, 28015 Bremen).
 * 
 * @author R. Lindemann
 */
public class TestReceptionReceipt extends MessageParent
{

  ReceptionReceipt receptionReceipt;

  @Before
  public void init() throws MalformedURLException, SAXException, IOException
  {
    EndpointLocator endpointLocator = new EndpointLocator(new URL(URL_WSDL_USER));
    endpointLocator.usePort("LetterStyle");
    receptionReceipt = new ReceptionReceipt(endpointLocator);
  }

  @Test
  public void testSendReceipt() throws Exception
  {
    OSCIRequest req = (OSCIRequest)sendToMsgBox(true);
    OSCIMsgBoxFetchRequest reqFetch = new OSCIMsgBoxFetchRequest(new URL(URL_WSDL_USER), SERVICE, FETCH_PORT);
    reqFetch.setSelectionByMessageId(new String[]{req.getMessageId().getValue()},
                                     OSCIFragmentCreator.createEndpointRef(URL_WSDL_USER));
    OSCIMsgBoxFetchResponse rsp = reqFetch.sendRequest();
    // ReceptionReceiptType reciType = ReceptionReceipt.createReceptionReceiptObject(rsp.getReceiveMessage());
    // FetchedNotificationType fetchedNotificationType = new
    // FetchedNotificationType();
    // try
    // {
    // prepareSendMessage(bindingProvider);
    //
    // // TODO Dynamisch ein Zertifikat beziehen
    // // X509Certificate cert =
    // //
    // CryptoTools.loadCertificate(ReceptionReceipt.class.getResourceAsStream("/test_alice_cypher.cer"));
    // ByteArrayOutputStream body = new ByteArrayOutputStream();
    //
    // JAXBElement<ReceptionReceiptType> messageBody = new
    // ObjectFactory().createReceptionReceipt(receReceipt);
    // JaxbContextHolder.getOSCIJaxBContext().createMarshaller().marshal(messageBody,
    // body);
    // ByteArrayOutputStream encOut = null;
    // try
    // {
    // encOut =
    // XMLEncryptionHelper.createEncryptionWithApacheOutputStream(endpointEncryptionCert,
    // new ByteArrayInputStream(
    // body.toByteArray()));
    // }
    // catch (Exception ex)
    // {
    // throw new OSCIServerException("",
    // "Could not get the encryption certificate.", ex);
    // }
    // // ############### second try
    // // SOAPMessage msg = MessageFactory.newInstance().createMessage();
    // // ((JAXBEncryptedData)encData).writeTo(msg);
    // // ByteArrayOutputStream secondOut = new ByteArrayOutputStream();
    // // msg.writeTo(secondOut);
    // // LOG.debug("XENC2 :" + secondOut);
    //
    // // ByteArrayOutputStream encBody = new ByteArrayOutputStream();
    // // ((JAXBEncryptedData)encData).writeTo(encBody);
    // // LOG.debug("XENC: " + encBody);
    // Message sendMsg =
    // com.sun.xml.ws.api.message.Messages.createUsingPayload(new
    // StreamSource(
    // new ByteArrayInputStream(
    // encOut.toByteArray())),
    // SOAPVersion.SOAP_12);
    // LOG.fine("XENC: " + encOut);
    //
    // // SOAPMessage msg = MessageFactory.newInstance().createMessage();
    // // msg.getSOAPBody().addNamespaceDeclaration("xenc",
    // ((JAXBEncryptedData)encData).getNamespaceURI());
    // // sendMsg.readAsSOAPMessage()
    // // .getSOAPBody()
    // // .addNamespaceDeclaration("xenc",
    // ((JAXBEncryptedData)encData).getNamespaceURI());
    //
    //
    // LOG.fine("Now sending the FetchNotification for messageId: " +
    // fetchMessageId);
    // Message rspMsg = jaxbDispatch.invoke(sendMsg);
    // return new OSCIResponse(rspMsg);
    // }
    // catch (DatatypeConfigurationException ex)
    // {
    // throw new OSCIServerException("", "Could not create the fetch time.",
    // ex);
    // }
    // catch (JAXBException ex)
    // {
    // throw new OSCIServerException("",
    // "Could create FetchNotification request.", ex);
    // }
    // catch (CertificateException ex)
    // {
    // throw new OSCIServerException("",
    // "Could not get the encryption certificate.", ex);
    // }
  }
}
