/**
*
*/
package org.servicemix.components.jaxws;
import com.sun.xml.ws.client.ClientTransportException;
import com.sun.xml.ws.pept.ept.EPTFactory;
import com.sun.xml.ws.pept.transport.Connection;
import com.sun.xml.ws.spi.runtime.WSConnection;
import org.servicemix.jbi.jaxp.BytesSource;
import org.servicemix.jbi.jaxp.SourceTransformer;
import javax.jbi.messaging.NormalizedMessage;
import javax.xml.transform.stream.StreamSource;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
class WSConnectionDelegate implements WSConnection, Connection {
private NormalizedMessage in;
private NormalizedMessage out;
private InputStream is;
private ByteArrayOutputStream baos;
int status = OK;
public WSConnectionDelegate(NormalizedMessage in, NormalizedMessage out) {
this.in = in;
this.out = out;
}
public Map<String, List<String>> getHeaders() {
Map<String, List<String>> headers = new HashMap<String, List<String>>();
List<String> l = new ArrayList<String>();
l.add("text/xml");
headers.put("Content-Type", l);
return headers;
}
public void setHeaders(Map<String, List<String>> headers) {
// TODO Auto-generated method stub
}
public void setStatus(int status) {
this.status = status;
}
public int getStatus() {
return status;
}
public InputStream getInput() {
if (is == null) {
try {
SourceTransformer st = new SourceTransformer();
StreamSource ss = st.toStreamSource(in.getContent());
is = ss.getInputStream();
if (is == null) {
Reader r = ss.getReader();
StringBuilder sb = new StringBuilder();
char[] buffer = new char[1024];
int nb = 0;
while ((nb = r.read(buffer)) > 0) {
sb.append(buffer, 0, nb);
}
is = new ByteArrayInputStream(sb.toString().getBytes());
}
} catch (Exception e) {
throw (ClientTransportException) new ClientTransportException("Error retrieving input").initCause(e);
}
}
return is;
}
public void closeInput() {
}
public OutputStream getOutput() {
if (baos == null) {
baos = new ByteArrayOutputStream();
}
return baos;
}
public void closeOutput() {
try {
baos.flush();
baos.close();
out.setContent(new BytesSource(baos.toByteArray()));
} catch (Exception e) {
throw (ClientTransportException) new ClientTransportException("Error retrieving input").initCause(e);
}
}
public OutputStream getDebug() {
// TODO Auto-generated method stub
return null;
}
public void close() {
}
public void write(ByteBuffer byteBuffer) {
throw new UnsupportedOperationException();
}
public EPTFactory getEPTFactory() {
throw new UnsupportedOperationException();
}
public int read(ByteBuffer byteBuffer) {
throw new UnsupportedOperationException();
}
public ByteBuffer readUntilEnd() {
throw new UnsupportedOperationException();
}
}