// Path: client-transport/src/main/java/com/nortal/jroad/client/service/XRoadDatabaseService.java
// public abstract class XRoadDatabaseService extends BaseXRoadDatabaseService {
// 
//   @Resource
//   protected XRoadConsumer xRoadConsumer;
//   @Resource
//   protected XRoadServiceConfigurationProvider xRoadServiceConfigurationProvider;
// 
//   @Override
//   protected XRoadConsumer getXRoadConsumer() {
//     return xRoadConsumer;
//   }
// 
//   @Override
//   protected XRoadServiceConfigurationProvider getXRoadServiceConfigurationProvider() {
//     return xRoadServiceConfigurationProvider;
//   }
// }
// 
// Path: common/src/main/java/com/nortal/jroad/model/XRoadAttachment.java
// public class XRoadAttachment implements InputStreamSource {
//   private String cid;
//   private DataHandler dataHandler;
// 
//   public XRoadAttachment(String cid, String contentType, byte[] data) {
//     this.cid = cid;
//     this.dataHandler = new DataHandler(new ByteArrayDataSource(contentType, data));
//   }
// 
//   public XRoadAttachment(String cid, DataHandler dataHandler) {
//     this.cid = cid;
//     this.dataHandler = dataHandler;
//   }
// 
//   public InputStream getInputStream() throws IOException {
//     return dataHandler.getInputStream();
//   }
// 
//   @Override
//   public String toString() {
//     return "cid:" + cid;
//   }
// 
//   public String getCid() {
//     return cid;
//   }
// 
//   public byte[] getData() throws IOException {
//     return FileCopyUtils.copyToByteArray(dataHandler.getInputStream());
//   }
// 
//   public String getContentType() {
//     return dataHandler.getContentType();
//   }
// 
//   public DataHandler getDataHandler() {
//     return dataHandler;
//   }
// 
// }
// 
// Path: common/src/main/java/com/nortal/jroad/util/AttachmentUtil.java
// public class AttachmentUtil {
//   private static long salt = 0;
// 
//   public static String getUniqueCid() {
//     salt++;
//     return UUID.randomUUID().toString() + String.valueOf(salt);
//   }
// 
// }


import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.activation.DataHandler;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.transform.TransformerException;
import org.springframework.stereotype.Service;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.soap.saaj.SaajSoapMessage;
import com.nortal.jroad.client.exception.XRoadServiceConsumptionException;
import com.nortal.jroad.client.service.XRoadDatabaseService;
import com.nortal.jroad.client.service.callback.CustomCallback;
import com.nortal.jroad.client.tor.types.ee.x_road.emtav5.producer.DownloadMimeDocument;
import com.nortal.jroad.client.tor.types.ee.x_road.emtav5.producer.DownloadMimeDocument.DownloadMime;
import com.nortal.jroad.client.tor.types.ee.x_road.emtav5.producer.DownloadMimeResponseDocument.DownloadMimeResponse;
import com.nortal.jroad.client.tor.types.ee.x_road.emtav5.producer.DownloadMimeType;
import com.nortal.jroad.client.tor.types.ee.x_road.emtav5.producer.TORIKDocument;
import com.nortal.jroad.client.tor.types.ee.x_road.emtav5.producer.TORIKDocument.TORIK;
import com.nortal.jroad.client.tor.types.ee.x_road.emtav5.producer.TORIKResponseDocument;
import com.nortal.jroad.client.tor.types.ee.x_road.emtav5.producer.TORIKResponseDocument.TORIKResponse;
import com.nortal.jroad.client.tor.types.ee.x_road.emtav5.producer.TorikRequestType;
import com.nortal.jroad.client.tor.types.ee.x_road.emtav5.producer.TorikRequestType.ParinguLiik;
import com.nortal.jroad.client.tor.types.ee.x_road.emtav5.producer.UploadMimeDocument;
import com.nortal.jroad.client.tor.types.ee.x_road.emtav5.producer.UploadMimeDocument.UploadMime;
import com.nortal.jroad.client.tor.types.ee.x_road.emtav5.producer.UploadMimeResponseDocument.UploadMimeResponse;
import com.nortal.jroad.client.tor.types.ee.x_road.emtav5.producer.UploadMimeType;
import com.nortal.jroad.client.tor.types.ee.x_road.emtav5.producer.UploadMimeType.Props;
import com.nortal.jroad.client.tor.types.ee.x_road.emtav5.producer.UploadMimeType.Props.Prop;
import com.nortal.jroad.model.XRoadAttachment;
import com.nortal.jroad.model.XRoadMessage;
import com.nortal.jroad.model.XmlBeansXRoadMessage;
import com.nortal.jroad.util.AttachmentUtil;

    DownloadMimeType request = downloadMimeDocument.addNewRequest();
    request.setTarget(target);

    XRoadMessage<DownloadMimeResponse> response =
        send(new XmlBeansXRoadMessage<DownloadMimeDocument.DownloadMime>(downloadMimeDocument),
             METHOD_DOWNLOAD_MIME,
             V1,
             new TorCallback(),
             null);
    return response;
  }

  @Override
  public UploadMimeResponse uploadMime(String target, String operation, String id, DataHandler fail)
      throws XRoadServiceConsumptionException {
    UploadMime uploadMimeDocument = UploadMimeDocument.UploadMime.Factory.newInstance();

    UploadMimeType request = uploadMimeDocument.addNewRequest();
    request.setTarget(target);
    request.setOperation(operation);
    Props props = request.addNewProps();
    Prop prop = props.addNewProp();
    prop.setKey(UPLOAD_ID);
    prop.setStringValue(id);

    XmlBeansXRoadMessage<UploadMimeDocument.UploadMime> XRoadMessage =
        new XmlBeansXRoadMessage<UploadMimeDocument.UploadMime>(uploadMimeDocument);
    List<XRoadAttachment> attachments = XRoadMessage.getAttachments();

// Path: client-transport/src/main/java/com/nortal/jroad/client/service/XRoadDatabaseService.java
// public abstract class XRoadDatabaseService extends BaseXRoadDatabaseService {
// 
//   @Resource
//   protected XRoadConsumer xRoadConsumer;
//   @Resource
//   protected XRoadServiceConfigurationProvider xRoadServiceConfigurationProvider;
// 
//   @Override
//   protected XRoadConsumer getXRoadConsumer() {
//     return xRoadConsumer;
//   }
// 
//   @Override
//   protected XRoadServiceConfigurationProvider getXRoadServiceConfigurationProvider() {
//     return xRoadServiceConfigurationProvider;
//   }
// }
// 
// Path: common/src/main/java/com/nortal/jroad/model/XRoadAttachment.java
// public class XRoadAttachment implements InputStreamSource {
//   private String cid;
//   private DataHandler dataHandler;
// 
//   public XRoadAttachment(String cid, String contentType, byte[] data) {
//     this.cid = cid;
//     this.dataHandler = new DataHandler(new ByteArrayDataSource(contentType, data));
//   }
// 
//   public XRoadAttachment(String cid, DataHandler dataHandler) {
//     this.cid = cid;
//     this.dataHandler = dataHandler;
//   }
// 
//   public InputStream getInputStream() throws IOException {
//     return dataHandler.getInputStream();
//   }
// 
//   @Override
//   public String toString() {
//     return "cid:" + cid;
//   }
// 
//   public String getCid() {
//     return cid;
//   }
// 
//   public byte[] getData() throws IOException {
//     return FileCopyUtils.copyToByteArray(dataHandler.getInputStream());
//   }
// 
//   public String getContentType() {
//     return dataHandler.getContentType();
//   }
// 
//   public DataHandler getDataHandler() {
//     return dataHandler;
//   }
// 
// }
// 
// Path: common/src/main/java/com/nortal/jroad/util/AttachmentUtil.java
// public class AttachmentUtil {
//   private static long salt = 0;
// 
//   public static String getUniqueCid() {
//     salt++;
//     return UUID.randomUUID().toString() + String.valueOf(salt);
//   }
// 
// }


// Path: client-service/tor/src/main/java/com/nortal/jroad/client/tor/TorXTeeServiceImpl.java
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.activation.DataHandler;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.transform.TransformerException;
import org.springframework.stereotype.Service;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.soap.saaj.SaajSoapMessage;
import com.nortal.jroad.client.exception.XRoadServiceConsumptionException;
import com.nortal.jroad.client.service.XRoadDatabaseService;
import com.nortal.jroad.client.service.callback.CustomCallback;
import com.nortal.jroad.client.tor.types.ee.x_road.emtav5.producer.DownloadMimeDocument;
import com.nortal.jroad.client.tor.types.ee.x_road.emtav5.producer.DownloadMimeDocument.DownloadMime;
import com.nortal.jroad.client.tor.types.ee.x_road.emtav5.producer.DownloadMimeResponseDocument.DownloadMimeResponse;
import com.nortal.jroad.client.tor.types.ee.x_road.emtav5.producer.DownloadMimeType;
import com.nortal.jroad.client.tor.types.ee.x_road.emtav5.producer.TORIKDocument;
import com.nortal.jroad.client.tor.types.ee.x_road.emtav5.producer.TORIKDocument.TORIK;
import com.nortal.jroad.client.tor.types.ee.x_road.emtav5.producer.TORIKResponseDocument;
import com.nortal.jroad.client.tor.types.ee.x_road.emtav5.producer.TORIKResponseDocument.TORIKResponse;
import com.nortal.jroad.client.tor.types.ee.x_road.emtav5.producer.TorikRequestType;
import com.nortal.jroad.client.tor.types.ee.x_road.emtav5.producer.TorikRequestType.ParinguLiik;
import com.nortal.jroad.client.tor.types.ee.x_road.emtav5.producer.UploadMimeDocument;
import com.nortal.jroad.client.tor.types.ee.x_road.emtav5.producer.UploadMimeDocument.UploadMime;
import com.nortal.jroad.client.tor.types.ee.x_road.emtav5.producer.UploadMimeResponseDocument.UploadMimeResponse;
import com.nortal.jroad.client.tor.types.ee.x_road.emtav5.producer.UploadMimeType;
import com.nortal.jroad.client.tor.types.ee.x_road.emtav5.producer.UploadMimeType.Props;
import com.nortal.jroad.client.tor.types.ee.x_road.emtav5.producer.UploadMimeType.Props.Prop;
import com.nortal.jroad.model.XRoadAttachment;
import com.nortal.jroad.model.XRoadMessage;
import com.nortal.jroad.model.XmlBeansXRoadMessage;
import com.nortal.jroad.util.AttachmentUtil;


    DownloadMimeType request = downloadMimeDocument.addNewRequest();
    request.setTarget(target);

    XRoadMessage<DownloadMimeResponse> response =
        send(new XmlBeansXRoadMessage<DownloadMimeDocument.DownloadMime>(downloadMimeDocument),
             METHOD_DOWNLOAD_MIME,
             V1,
             new TorCallback(),
             null);
    return response;
  }

  @Override
  public UploadMimeResponse uploadMime(String target, String operation, String id, DataHandler fail)
      throws XRoadServiceConsumptionException {
    UploadMime uploadMimeDocument = UploadMimeDocument.UploadMime.Factory.newInstance();

    UploadMimeType request = uploadMimeDocument.addNewRequest();
    request.setTarget(target);
    request.setOperation(operation);
    Props props = request.addNewProps();
    Prop prop = props.addNewProp();
    prop.setKey(UPLOAD_ID);
    prop.setStringValue(id);

    XmlBeansXRoadMessage<UploadMimeDocument.UploadMime> XRoadMessage =
        new XmlBeansXRoadMessage<UploadMimeDocument.UploadMime>(uploadMimeDocument);
    List<XRoadAttachment> attachments = XRoadMessage.getAttachments();

    String failCid = AttachmentUtil.getUniqueCid();