/*
* $Id: URLContainer.java,v 1.4 2002/09/16 08:05:06 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.server;
import java.io.File;
import java.io.InputStream;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.PrintStream;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.net.URL;
import java.net.URLConnection;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.util.Calendar;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import anvil.util.Conversions;
/**
* class URLContainer
*
* @author: Jani Lehtim�ki
*/
public class URLContainer implements Container
{
protected URL _url;
public URLContainer(URL url)
{
_url = url;
}
protected String adapt(Zone zone, String pathinfo)
{
String basepath = zone.getBasePath();
return pathinfo.substring(basepath.length()-1);
}
public URL getResourceURL(Zone zone, Address address)
{
String pathinfo = adapt(zone, address.getPathinfo());
try {
if (pathinfo.startsWith("/")) {
pathinfo = pathinfo.substring(1);
}
URL url = new URL(_url, pathinfo);
return url;
} catch (MalformedURLException e) {
}
return null;
}
public Resource getResource(Zone zone, Address address, Context context) throws ContainerException
{
return getResource(zone, address);
}
public Resource getResource(Zone zone, Address address) throws ContainerException
{
String pathinfo = address.getPathinfo();
URL url = getResourceURL(zone, address);
if (url == null) {
throw new ContainerException(400, "Invalid URL: "+pathinfo);
}
String str = url.toString();
int dotpos = str.lastIndexOf('.');
int slashpos = str.lastIndexOf('/');
if (slashpos > dotpos) {
String dirindex = zone.getDirectoryIndex();
if (dirindex != null) {
if (pathinfo.endsWith("/")) {
pathinfo = pathinfo + dirindex;
} else {
pathinfo = pathinfo + '/' + dirindex;
}
url = getResourceURL(zone, address);
}
}
if (url == null) {
throw new ContainerException(400, "Invalid URL: "+pathinfo);
}
try {
URLConnection conn = url.openConnection();
conn.connect();
if (conn instanceof HttpURLConnection) {
int code = ((HttpURLConnection)conn).getResponseCode();
if (code != 200) {
throw new ContainerException(code, "Resource: "+url);
}
}
return new URLResource(conn, pathinfo);
} catch (IOException e) {
throw new ContainerException(404, "Resource not found: " + e.getMessage());
}
}
public static class URLResource implements Resource
{
private URLConnection _conn;
private String _pathinfo;
public URLResource(URLConnection conn, String pathinfo)
{
_conn = conn;
_pathinfo = pathinfo;
}
public String getPathinfo()
{
return _pathinfo;
}
public URL getURL()
{
return _conn.getURL();
}
public String getContentType()
{
return _conn.getContentType();
}
public long getLastModified()
{
return _conn.getLastModified();
}
public int getLength()
{
return _conn.getContentLength();
}
public InputStream getInputStream() throws IOException
{
return _conn.getInputStream();
}
public void writeTo(OutputStream output) throws IOException
{
InputStream input = _conn.getInputStream();
byte[] buffer = new byte[1024];
for(;;) {
int read = input.read(buffer, 0, buffer.length);
if (read <= 0) {
break;
}
output.write(buffer, 0, read);
}
input.close();
}
public void close()
{
}
}
}