// 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/jaxb/ByteArrayDataSource.java
// public class ByteArrayDataSource implements DataSource {
//   private byte[] data;
//   private String contentType;
// 
//   public ByteArrayDataSource(String contentType, byte[] data) {
//     this.contentType = contentType;
//     this.data = data;
//   }
// 
//   public InputStream getInputStream() throws IOException {
//     return new ByteArrayInputStream(data);
//   }
// 
//   public OutputStream getOutputStream() throws IOException {
//     throw new UnsupportedOperationException();
//   }
// 
//   public String getContentType() {
//     return contentType;
//   }
// 
//   public String getName() {
//     return "ByteArrayDataSource";
//   }
// }
// 
// Path: common/src/main/java/com/nortal/jroad/model/BeanXRoadMessage.java
// public class BeanXRoadMessage<T> implements XRoadMessage<T> {
//   private List<XRoadAttachment> attachments;
//   private XRoadHeader header;
//   private T content;
// 
//   public BeanXRoadMessage(XRoadHeader header, T content, List<XRoadAttachment> attachments) {
//     this.attachments = attachments;
//     this.header = header;
//     this.content = content;
//   }
// 
//   public List<XRoadAttachment> getAttachments() {
//     return attachments;
//   }
// 
//   public XRoadHeader getHeader() {
//     return header;
//   }
// 
//   public T getContent() {
//     return content;
//   }
// 
//   public void setContent(T content) {
//     this.content = content;
//   }
// 
//   public void setAttachments(List<XRoadAttachment> attachments) {
//     this.attachments = attachments;
//   }
// 
//   public void setHeader(XRoadHeader header) {
//     this.header = header;
//   }
// }
// 
// 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;
//   }
// 
// }


import com.nortal.jroad.client.exception.XRoadServiceConsumptionException;
import com.nortal.jroad.client.service.XRoadDatabaseService;
import com.nortal.jroad.example.client.database.NaidisXRoadDatabase;
import com.nortal.jroad.example.client.types.eu.x_road.naidis.AttachmentEchoRequest;
import com.nortal.jroad.example.client.types.eu.x_road.naidis.AttachmentEchoResponse;
import com.nortal.jroad.example.client.types.eu.x_road.naidis.EchoRequest;
import com.nortal.jroad.example.client.types.eu.x_road.naidis.EchoResponse;
import com.nortal.jroad.jaxb.ByteArrayDataSource;
import com.nortal.jroad.model.BeanXRoadMessage;
import com.nortal.jroad.model.XRoadAttachment;
import com.nortal.jroad.model.XRoadMessage;
import java.util.Arrays;
import javax.activation.DataHandler;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
package com.nortal.jroad.example.client;


/**
 * @author Lauri Lättemäe (lauri.lattemae@nortal.com)
 */
@Service("naidisXRoadService")
public class NaidisXRoadServiceImpl extends XRoadDatabaseService implements NaidisXRoadService {
  @Resource
  private NaidisXRoadDatabase naidisXRoadDatabase;

  @Override
  public AttachmentEchoResponse sendAttachment(String contentType, byte[] content)
      throws XRoadServiceConsumptionException {
    DataHandler reqHandler = new DataHandler(new ByteArrayDataSource(contentType, content));
    AttachmentEchoRequest request = AttachmentEchoRequest.Factory.newInstance();
    request.addNewNest().setAttachmentHandler(reqHandler);
    return naidisXRoadDatabase.attachmentEchoV1(request);
  }

  @Override
  public String sendEcho(String text) throws XRoadServiceConsumptionException {
    EchoRequest req = EchoRequest.Factory.newInstance();
    req.setText(text);
    return naidisXRoadDatabase.echoV1(req).getText();
  }

  @Override
  public String sendEchoMime(String text) throws XRoadServiceConsumptionException {
    EchoRequest echoReq = EchoRequest.Factory.newInstance();
    echoReq.setText(text);
    BeanXRoadMessage<EchoRequest> req =
        new BeanXRoadMessage<EchoRequest>(null,
                                          echoReq,
// 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/jaxb/ByteArrayDataSource.java
// public class ByteArrayDataSource implements DataSource {
//   private byte[] data;
//   private String contentType;
// 
//   public ByteArrayDataSource(String contentType, byte[] data) {
//     this.contentType = contentType;
//     this.data = data;
//   }
// 
//   public InputStream getInputStream() throws IOException {
//     return new ByteArrayInputStream(data);
//   }
// 
//   public OutputStream getOutputStream() throws IOException {
//     throw new UnsupportedOperationException();
//   }
// 
//   public String getContentType() {
//     return contentType;
//   }
// 
//   public String getName() {
//     return "ByteArrayDataSource";
//   }
// }
// 
// Path: common/src/main/java/com/nortal/jroad/model/BeanXRoadMessage.java
// public class BeanXRoadMessage<T> implements XRoadMessage<T> {
//   private List<XRoadAttachment> attachments;
//   private XRoadHeader header;
//   private T content;
// 
//   public BeanXRoadMessage(XRoadHeader header, T content, List<XRoadAttachment> attachments) {
//     this.attachments = attachments;
//     this.header = header;
//     this.content = content;
//   }
// 
//   public List<XRoadAttachment> getAttachments() {
//     return attachments;
//   }
// 
//   public XRoadHeader getHeader() {
//     return header;
//   }
// 
//   public T getContent() {
//     return content;
//   }
// 
//   public void setContent(T content) {
//     this.content = content;
//   }
// 
//   public void setAttachments(List<XRoadAttachment> attachments) {
//     this.attachments = attachments;
//   }
// 
//   public void setHeader(XRoadHeader header) {
//     this.header = header;
//   }
// }
// 
// 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: example/src/main/java/com/nortal/jroad/example/client/NaidisXRoadServiceImpl.java
import com.nortal.jroad.client.exception.XRoadServiceConsumptionException;
import com.nortal.jroad.client.service.XRoadDatabaseService;
import com.nortal.jroad.example.client.database.NaidisXRoadDatabase;
import com.nortal.jroad.example.client.types.eu.x_road.naidis.AttachmentEchoRequest;
import com.nortal.jroad.example.client.types.eu.x_road.naidis.AttachmentEchoResponse;
import com.nortal.jroad.example.client.types.eu.x_road.naidis.EchoRequest;
import com.nortal.jroad.example.client.types.eu.x_road.naidis.EchoResponse;
import com.nortal.jroad.jaxb.ByteArrayDataSource;
import com.nortal.jroad.model.BeanXRoadMessage;
import com.nortal.jroad.model.XRoadAttachment;
import com.nortal.jroad.model.XRoadMessage;
import java.util.Arrays;
import javax.activation.DataHandler;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

package com.nortal.jroad.example.client;


/**
 * @author Lauri Lättemäe (lauri.lattemae@nortal.com)
 */
@Service("naidisXRoadService")
public class NaidisXRoadServiceImpl extends XRoadDatabaseService implements NaidisXRoadService {
  @Resource
  private NaidisXRoadDatabase naidisXRoadDatabase;

  @Override
  public AttachmentEchoResponse sendAttachment(String contentType, byte[] content)
      throws XRoadServiceConsumptionException {
    DataHandler reqHandler = new DataHandler(new ByteArrayDataSource(contentType, content));
    AttachmentEchoRequest request = AttachmentEchoRequest.Factory.newInstance();
    request.addNewNest().setAttachmentHandler(reqHandler);
    return naidisXRoadDatabase.attachmentEchoV1(request);
  }

  @Override
  public String sendEcho(String text) throws XRoadServiceConsumptionException {
    EchoRequest req = EchoRequest.Factory.newInstance();
    req.setText(text);
    return naidisXRoadDatabase.echoV1(req).getText();
  }

  @Override
  public String sendEchoMime(String text) throws XRoadServiceConsumptionException {
    EchoRequest echoReq = EchoRequest.Factory.newInstance();
    echoReq.setText(text);
    BeanXRoadMessage<EchoRequest> req =
        new BeanXRoadMessage<EchoRequest>(null,
                                          echoReq,
                                          Arrays.asList(new XRoadAttachment("cid", "text/plain", text.getBytes())));