1
0
mirror of https://github.com/Rogiel/httpchannel synced 2025-12-06 15:42:50 +00:00

Improve documentation and implement a new ServiceID class

This commit is contained in:
2011-11-21 00:57:03 -02:00
parent 1c8db5cf36
commit f210afd16a
23 changed files with 717 additions and 106 deletions

View File

@@ -26,11 +26,12 @@ import com.rogiel.httpchannel.service.config.ServiceConfiguration;
* @param <T>
* The {@link ServiceConfiguration} <b>interface</b> type used by the
* {@link Service}. Note that this <b>must</b> be an interface!s
* @see ServiceConfiguration ServiceConfiguration for details on the configuration interface.
* @see ServiceConfiguration ServiceConfiguration for details on the
* configuration interface.
*/
public abstract class AbstractService<T extends ServiceConfiguration>
implements Service {
protected final T configuration;
protected T configuration;
protected AbstractService(T configuration) {
this.configuration = configuration;
@@ -40,4 +41,19 @@ public abstract class AbstractService<T extends ServiceConfiguration>
public T getServiceConfiguration() {
return configuration;
}
@Override
@SuppressWarnings("unchecked")
public void setServiceConfiguration(ServiceConfiguration configuration) {
this.configuration = (T) configuration;
}
@Override
public Service clone() {
try {
return (Service) super.clone();
} catch (CloneNotSupportedException e) {
return null;
}
}
}

View File

@@ -20,6 +20,8 @@ import java.io.IOException;
import com.rogiel.httpchannel.service.exception.DownloadLimitExceededException;
import com.rogiel.httpchannel.service.exception.DownloadLinkNotFoundException;
import com.rogiel.httpchannel.service.exception.DownloadNotAuthorizedException;
import com.rogiel.httpchannel.service.exception.DownloadNotResumableException;
/**
* This interfaces provides downloading for an service.
@@ -29,12 +31,26 @@ import com.rogiel.httpchannel.service.exception.DownloadLinkNotFoundException;
*/
public interface Downloader {
/**
* Starts the download process.
* Opens a new {@link DownloadChannel} that will be immediately ready to
* read data from the download stream.
* <p>
* Whether an channel is returned or any exception is thrown it is possible
* to reuse the same instance for more than one download, this, however, is
* very unlikely to be done. The most common usage usage scenario for this
* is when an {@link DownloadNotResumableException} is thrown and you wish
* to restart the download from start giving <tt>position</tt> equal to
* zero.
* <p>
* Please remember to close the channel by calling
* {@link DownloadChannel#close()}, otherwise some of the resources (such as
* network connections and file handlers) might still be open for the whole
* runtime or until they time out, which could never occur.
*
* @param listener
* the listener to keep a track on the download progress
* @param position
* the download start position. If seek is supported by service.
* If zero, download will start from the beginning.
*
* @return the {@link DownloadChannel} instance
* @throws IOException
@@ -45,8 +61,15 @@ public interface Downloader {
* @throws DownloadLimitExceededException
* if the download limit has been exceed, most times thrown when
* downloading as a non-premium user
* @throws DownloadNotAuthorizedException
* if the user (or guest) account does not have necessary rights
* to download the file
* @throws DownloadNotResumableException
* if the download cannot be started at <tt>position</tt>. Will
* only be thrown if <tt>position > 0</tt>.
*/
DownloadChannel download(DownloadListener listener, long position)
throws IOException, DownloadLinkNotFoundException,
DownloadLimitExceededException;
DownloadLimitExceededException, DownloadNotAuthorizedException,
DownloadNotResumableException;
}

View File

@@ -30,13 +30,13 @@ import com.rogiel.httpchannel.service.config.ServiceConfiguration;
* @author Rogiel
* @since 1.0
*/
public interface Service {
public interface Service extends Cloneable {
/**
* Get the ServiceID.
* Get the {@link ServiceID}.
*
* @return the id of the service
*/
String getID();
ServiceID getID();
/**
* Get Major version of this service
@@ -58,4 +58,17 @@ public interface Service {
* @return the {@link ServiceConfiguration} instance
*/
ServiceConfiguration getServiceConfiguration();
/**
* Sets this {@link ServiceConfiguration} instance
*
* @param configuration
* the {@link ServiceConfiguration} instance
*/
void setServiceConfiguration(ServiceConfiguration configuration);
/**
* @return a cloned version of this service
*/
Service clone();
}

View File

@@ -0,0 +1,108 @@
/*
* This file is part of seedbox <github.com/seedbox>.
*
* seedbox is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* seedbox 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with seedbox. If not, see <http://www.gnu.org/licenses/>.
*/
package com.rogiel.httpchannel.service;
import java.io.Serializable;
/**
* An ID used to represent the given service
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class ServiceID implements Serializable, Cloneable {
/**
* This class serialization version UID
*/
private static final long serialVersionUID = -1078456596792552200L;
/**
* The raw ID
*/
private final String id;
/**
* @param id
* the raw id
*/
private ServiceID(final String id) {
this.id = id;
}
/**
* @return the raw id
*/
public String getRawID() {
return id;
}
/**
* @param id
* the raw id
*/
public static ServiceID create(String id) {
return new ServiceID(id);
}
/**
* Returns the service associated to this ID. This has the same effect as
* calling:
*
* <pre>
* <code> ServiceID id = ...;
* Services.getService(id);</code>
* </pre>
*
* @return the associated service, if any.
*/
public Service getService() {
return Services.getService(this);
}
@Override
public ServiceID clone() {
try {
return (ServiceID) super.clone();
} catch (CloneNotSupportedException e) {
return null;
}
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ServiceID other = (ServiceID) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}

View File

@@ -0,0 +1,116 @@
/*
* This file is part of seedbox <github.com/seedbox>.
*
* seedbox is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* seedbox 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with seedbox. If not, see <http://www.gnu.org/licenses/>.
*/
package com.rogiel.httpchannel.service;
import java.net.URL;
import java.util.Iterator;
import java.util.ServiceLoader;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class Services {
private static ServiceLoader<Service> services = ServiceLoader
.load(Service.class);
/**
* Reloads the list of available services in the classpath
*
* @see java.util.ServiceLoader#reload()
*/
public static void reload() {
services.reload();
}
/**
* Tries to detect which service should be used to download the given URL
*
* @param url
* the URL
* @return the matched service
*/
public static DownloadService matchURL(URL url) {
for (final Service service : iterate()) {
if (!(service instanceof DownloadService))
continue;
if (((DownloadService) service).matchURL(url))
return (DownloadService) service;
}
return null;
}
/**
* Tries to detect which service has the given <tt>id</tt>
*
* @param id
* the service id
* @return the matched service
*/
public static Service getService(ServiceID id) {
for (final Service service : iterate()) {
if (service.getID().equals(id))
return service;
}
return null;
}
/**
* Creates a new {@link Iterable} instance to iterate over services
*
* @return the {@link Iterable} instance
*/
public static Iterable<Service> iterate() {
return new Iterable<Service>() {
@Override
public Iterator<Service> iterator() {
return services.iterator();
}
};
}
/**
* Creates a new {@link Iterable} instance to iterate over service ids
*
* @return the {@link Iterable} instance
*/
public static Iterable<ServiceID> iterateIDs() {
return new Iterable<ServiceID>() {
@Override
public Iterator<ServiceID> iterator() {
return new Iterator<ServiceID>() {
private final Iterator<Service> iterator = iterate()
.iterator();
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
public ServiceID next() {
return iterator.next().getID();
}
@Override
public void remove() {
iterator.remove();
}
};
}
};
}
}

View File

@@ -26,7 +26,20 @@ import java.io.IOException;
*/
public interface Uploader {
/**
* Starts the upload process on this service.
* Opens a new {@link UploadChannel} that will be immediately ready to
* receive data to be sent to the upload stream.
* <p>
* Whether an channel is returned or any exception is thrown it is <b><span
* style="color:red">NOT</span></b> possible to reuse the same instance for
* more than one upload!
* <p>
* Please remember to close the channel before calling
* {@link UploadChannel#getDownloadLink()} or aborting the upload. The
* {@link UploadChannel#close()} method will finish upload (may take some
* time) and release any of the resources (such as network connections and
* file handlers) that could continue open for the whole runtime or until
* they time out, which could never occur. Note that you should close the
* channel even when an exception is thrown.
*
* @return the {@link UploadChannel} instance
* @throws IOException

View File

@@ -1,69 +0,0 @@
/*
* This file is part of seedbox <github.com/seedbox>.
*
* seedbox is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* seedbox 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with seedbox. If not, see <http://www.gnu.org/licenses/>.
*/
package com.rogiel.httpchannel.service.channel;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import com.rogiel.httpchannel.service.DownloadChannel;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class InputStreamDownloadChannel implements DownloadChannel {
private final ReadableByteChannel channel;
private final long length;
private final String filename;
public InputStreamDownloadChannel(InputStream in, final long length,
final String filename) {
this.channel = Channels.newChannel(in);
this.length = length;
this.filename = filename;
}
@Override
public int read(ByteBuffer dst) throws IOException {
return channel.read(dst);
}
@Override
public boolean isOpen() {
return channel.isOpen();
}
@Override
public void close() throws IOException {
channel.close();
}
@Override
public long getFilesize() {
return length;
}
@Override
public String getFilename() {
return filename;
}
}

View File

@@ -1,93 +0,0 @@
/*
* This file is part of seedbox <github.com/seedbox>.
*
* seedbox is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* seedbox 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with seedbox. If not, see <http://www.gnu.org/licenses/>.
*/
package com.rogiel.httpchannel.service.channel;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.WritableByteChannel;
import com.rogiel.httpchannel.service.UploadChannel;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class LinkedUploadChannel implements UploadChannel {
private WritableByteChannel channel;
private final LinkedUploadChannelCloseCallback closeCallback;
private final long length;
private final String filename;
private String downloadLink;
private boolean open = true;
public LinkedUploadChannel(LinkedUploadChannelCloseCallback closeCallback,
long filesize, String filename) {
this.closeCallback = closeCallback;
this.filename = filename;
this.length = filesize;
}
@Override
public int write(ByteBuffer src) throws IOException {
if (channel == null)
throw new IOException("Channel is not linked yet");
return channel.write(src);
}
@Override
public boolean isOpen() {
return (channel != null ? channel.isOpen() : true) && open;
}
@Override
public void close() throws IOException {
open = false;
downloadLink = closeCallback.finish();
}
public interface LinkedUploadChannelCloseCallback {
String finish() throws IOException;
}
@Override
public long getFilesize() {
return length;
}
@Override
public String getFilename() {
return filename;
}
@Override
public String getDownloadLink() {
return downloadLink;
}
public void linkChannel(WritableByteChannel channel) throws IOException {
if(this.channel != null)
throw new IOException("This channel is already linked.");
this.channel = channel;
}
public boolean isLinked() {
return channel != null;
}
}

View File

@@ -1,75 +0,0 @@
/*
* This file is part of seedbox <github.com/seedbox>.
*
* seedbox is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* seedbox 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with seedbox. If not, see <http://www.gnu.org/licenses/>.
*/
package com.rogiel.httpchannel.service.channel;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.channels.Channels;
import java.nio.channels.WritableByteChannel;
import org.apache.http.entity.mime.content.AbstractContentBody;
import org.apache.http.entity.mime.content.ContentBody;
import com.rogiel.httpchannel.service.Uploader;
/**
* {@link ContentBody} used to upload files in {@link Uploader} implementations.
*
* @author Rogiel
* @since 1.0
*/
public class LinkedUploadChannelContentBody extends AbstractContentBody {
private final LinkedUploadChannel channel;
public LinkedUploadChannelContentBody(LinkedUploadChannel channel) {
super("application/octet-stream");
this.channel = channel;
}
@Override
public String getFilename() {
return channel.getFilename();
}
@Override
public void writeTo(OutputStream out) throws IOException {
final WritableByteChannel outputChannel = Channels.newChannel(out);
channel.linkChannel(outputChannel);
while (channel.isOpen() && outputChannel.isOpen()) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
}
@Override
public String getCharset() {
return null;
}
@Override
public long getContentLength() {
return channel.getFilesize();
}
@Override
public String getTransferEncoding() {
return "binary";
}
}

View File

@@ -0,0 +1,60 @@
/*
* This file is part of seedbox <github.com/seedbox>.
*
* seedbox is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* seedbox 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with seedbox. If not, see <http://www.gnu.org/licenses/>.
*/
package com.rogiel.httpchannel.service.exception;
/**
* Exception thrown if the file exists, but the user (or guest) does not have
* authorization to download it.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class DownloadNotAuthorizedException extends DownloadServiceException {
private static final long serialVersionUID = 1L;
/**
* Creates a new empty instance of this exception
*/
public DownloadNotAuthorizedException() {
super();
}
/**
* @param message
* the message
* @param cause
* the root cause
*/
public DownloadNotAuthorizedException(String message, Throwable cause) {
super(message, cause);
}
/**
* @param message
* the message
*/
public DownloadNotAuthorizedException(String message) {
super(message);
}
/**
* @param cause
* the root cause
*/
public DownloadNotAuthorizedException(Throwable cause) {
super(cause);
}
}

View File

@@ -0,0 +1,59 @@
/*
* This file is part of seedbox <github.com/seedbox>.
*
* seedbox is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* seedbox 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with seedbox. If not, see <http://www.gnu.org/licenses/>.
*/
package com.rogiel.httpchannel.service.exception;
/**
* Exception thrown if the download cannot be resumed at the given point.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class DownloadNotResumableException extends DownloadServiceException {
private static final long serialVersionUID = 1L;
/**
* Creates a new empty instance of this exception
*/
public DownloadNotResumableException() {
super();
}
/**
* @param message
* the message
* @param cause
* the root cause
*/
public DownloadNotResumableException(String message, Throwable cause) {
super(message, cause);
}
/**
* @param message
* the message
*/
public DownloadNotResumableException(String message) {
super(message);
}
/**
* @param cause
* the root cause
*/
public DownloadNotResumableException(Throwable cause) {
super(cause);
}
}