/*
* RIBAX, Making Web Applications Easy
* Copyright (C) 2006 Damian Hamill and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.ribax.datasources;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.Socket;
import org.ribax.ui.DataItem;
import org.ribax.ui.FileDataItem;
import org.ribax.parameters.FileParameter;
import org.ribax.parameters.Parameter;
import org.ribax.utils.log.LOG;
import org.ribax.utils.types.NameValuePair;
/**
* A class that implements a DataSource using TCP/IP sockets
*
* @version <tt>$Revision: $</tt>
* @author <a href="mailto:damian@ribax.org">Damian Hamill</a>
*/
public class SocketDataSource implements DataSource {
private String url;
private String host;
private int port;
@SuppressWarnings("unused")
private String name = null;
private Hashtable<String,String> headers = new Hashtable<String,String>();
public SocketDataSource(String url,String name) {
this.url = url;
this.name = name;
}
public void setHeader(String name, String value) {
headers.put(name,value);
}
private void nullParameterWarning(NameValuePair pair) {
if (pair == null)
LOG.warn(Messages.getString("SocketDataSource.0")); //$NON-NLS-1$
else
LOG.warn(Messages.getString("SocketDataSource.1")+pair.getName()+" value:"+pair.getValue()); //$NON-NLS-1$ //$NON-NLS-2$
}
public InputStream getInputStream(ArrayList<Object> params)
throws IOException
{
NameValuePair pair = null;
Iterator<Object> iter = null;
InputStream stream = null;
OutputStream out;
if (url.startsWith("telnet:")) //$NON-NLS-1$
url = url.replaceFirst("telnet:","http:"); //$NON-NLS-1$ //$NON-NLS-2$
try {
URI u = new URI(url);
host = u.getHost();
port = u.getPort();
} catch (URISyntaxException ex) {
LOG.warn(Messages.getString("SocketDataSource.6")+url); //$NON-NLS-1$
return null;
}
Socket sock = new Socket(host,port);
stream = sock.getInputStream();
out = sock.getOutputStream();
// send the form data
if (LOG.isDebugEnabled())
LOG.debug(Messages.getString("SocketDataSource.7")+url); //$NON-NLS-1$
int paramcount = 0;
iter = params.iterator();
while (iter.hasNext()) {
Object o = iter.next();
if (o == null)
continue;
if (o instanceof NameValuePair) {
pair = (NameValuePair)o;
if (pair == null || pair.getName() == null || pair.getValue() == null) {
nullParameterWarning(pair);
continue;
}
StringPart p = new StringPart(pair.getName(),pair.getValue());
out.write(p.toString().getBytes());
} else if (o instanceof FileParameter) {
FileParameter fp = (FileParameter)o;
pair = fp.getNameValuePair();
if (pair == null || pair.getName() == null || pair.getValue() == null) {
nullParameterWarning(pair);
continue;
}
File f = new File(pair.getValue());
if (f == null) {
LOG.warn(Messages.getString("SocketDataSource.8")+pair.getValue()); //$NON-NLS-1$
continue;
}
byte[] b = new byte[1024];
//send the file
FileInputStream fin = new FileInputStream(f);
while(fin.available() > 0) {
fin.read(b);
out.write(b);
}
} else if (o instanceof FileDataItem) {
FileDataItem fp = (FileDataItem)o;
pair = fp.getNameValuePair();
if (pair == null || pair.getName() == null || pair.getValue() == null) {
nullParameterWarning(pair);
continue;
}
File f = new File(pair.getValue());
if (f == null){
LOG.warn(Messages.getString("SocketDataSource.9")+pair.getValue()); //$NON-NLS-1$
continue;
}
//send the file
byte[] b = new byte[1024];
//send the file
FileInputStream fin = new FileInputStream(f);
while(fin.available() > 0) {
fin.read(b);
out.write(b);
}
} else if (o instanceof DataItem) {
DataItem fp = (DataItem)o;
pair = fp.getNameValuePair();
if (pair == null || pair.getName() == null || pair.getValue() == null) {
nullParameterWarning(pair);
continue;
}
StringPart p = new StringPart(pair.getName(),pair.getValue());
out.write(p.toString().getBytes());
} else if (o instanceof Parameter) {
Parameter param = (Parameter)o;
pair = param.getNameValuePair();
if (pair == null || pair.getName() == null || pair.getValue() == null) {
nullParameterWarning(pair);
continue;
}
StringPart p = new StringPart(pair.getName(),pair.getValue());
out.write(p.toString().getBytes());
}
paramcount++;
if (LOG.isDebugEnabled())
LOG.debug(url+Messages.getString("SocketDataSource.10")+pair.getName()+"="+pair.getValue()); //$NON-NLS-1$ //$NON-NLS-2$
}
return stream;
}
}