mirror of
https://github.com/Rogiel/httpchannel
synced 2025-12-06 07:32:50 +00:00
Modularize in maven projects the httpchannel library
This commit creates several maven modules for each segment of the library. Now it is possible to include only individual services to the classpath instead of the full library.
This commit is contained in:
@@ -1,52 +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;
|
||||
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpResponse;
|
||||
|
||||
import com.rogiel.httpchannel.util.ThreadUtils;
|
||||
|
||||
/**
|
||||
* @author rogiel
|
||||
*/
|
||||
public abstract class AbstractDownloader implements Downloader {
|
||||
protected int parseTimer(String stringTimer) {
|
||||
int timer = 0;
|
||||
if (stringTimer != null && stringTimer.length() > 0) {
|
||||
timer = Integer.parseInt(stringTimer);
|
||||
}
|
||||
return timer;
|
||||
}
|
||||
|
||||
protected long getContentLength(HttpResponse response) {
|
||||
final Header contentLengthHeader = response
|
||||
.getFirstHeader("Content-Length");
|
||||
long contentLength = -1;
|
||||
if (contentLengthHeader != null) {
|
||||
contentLength = Long.valueOf(contentLengthHeader.getValue());
|
||||
}
|
||||
return contentLength;
|
||||
}
|
||||
|
||||
protected void timer(DownloadListener listener, long timer) {
|
||||
if (listener != null) {
|
||||
listener.timer(timer);
|
||||
}
|
||||
ThreadUtils.sleep(timer);
|
||||
}
|
||||
}
|
||||
@@ -1,158 +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;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
|
||||
import com.rogiel.httpchannel.service.captcha.CaptchaResolver;
|
||||
import com.rogiel.httpchannel.service.config.ServiceConfiguration;
|
||||
import com.rogiel.httpchannel.util.AlwaysRedirectStrategy;
|
||||
import com.rogiel.httpchannel.util.HttpClientUtils;
|
||||
import com.rogiel.httpchannel.util.htmlparser.HTMLPage;
|
||||
|
||||
/**
|
||||
* Abstract base service for HTTP enabled services.
|
||||
*
|
||||
* @author Rogiel
|
||||
* @since 1.0
|
||||
*/
|
||||
public abstract class AbstractHttpService<T extends ServiceConfiguration>
|
||||
extends AbstractService<T> implements Service {
|
||||
private static final ExecutorService threadPool = Executors
|
||||
.newCachedThreadPool();
|
||||
|
||||
/**
|
||||
* The {@link HttpClient} instance for this service
|
||||
*/
|
||||
protected DefaultHttpClient client = new DefaultHttpClient();
|
||||
|
||||
/**
|
||||
* The captcha resolver
|
||||
*/
|
||||
protected CaptchaResolver captchaResolver;
|
||||
|
||||
protected AbstractHttpService(T configuration) {
|
||||
super(configuration);
|
||||
client.setRedirectStrategy(new AlwaysRedirectStrategy());
|
||||
// client.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS,
|
||||
// true);
|
||||
// client.getParams().setIntParameter(ClientPNames.MAX_REDIRECTS, 10);
|
||||
// client.setRedirectStrategy(new DefaultRedirectStrategy());
|
||||
}
|
||||
|
||||
protected HttpResponse get(String url) throws ClientProtocolException,
|
||||
IOException {
|
||||
final HttpGet request = new HttpGet(url);
|
||||
return client.execute(request);
|
||||
}
|
||||
|
||||
protected String getAsString(String url) throws ClientProtocolException,
|
||||
IOException {
|
||||
return HttpClientUtils.toString(get(url));
|
||||
}
|
||||
|
||||
protected HTMLPage getAsPage(String url) throws ClientProtocolException,
|
||||
IOException {
|
||||
return HTMLPage.parse(getAsString(url));
|
||||
}
|
||||
|
||||
public Future<HttpResponse> getAsync(final String url) throws IOException {
|
||||
return threadPool.submit(new Callable<HttpResponse>() {
|
||||
@Override
|
||||
public HttpResponse call() throws Exception {
|
||||
return get(url);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Future<String> getAsStringAsync(final String url) throws IOException {
|
||||
return threadPool.submit(new Callable<String>() {
|
||||
@Override
|
||||
public String call() throws Exception {
|
||||
return getAsString(url);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Future<HTMLPage> getAsPageAsync(final String url) throws IOException {
|
||||
return threadPool.submit(new Callable<HTMLPage>() {
|
||||
@Override
|
||||
public HTMLPage call() throws Exception {
|
||||
return getAsPage(url);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected HttpResponse post(String url, HttpEntity entity)
|
||||
throws ClientProtocolException, IOException {
|
||||
final HttpPost request = new HttpPost(url);
|
||||
request.setEntity(entity);
|
||||
return client.execute(request);
|
||||
}
|
||||
|
||||
protected String postAsString(String url, HttpEntity entity)
|
||||
throws ClientProtocolException, IOException {
|
||||
return HttpClientUtils.toString(post(url, entity));
|
||||
}
|
||||
|
||||
protected HTMLPage postAsPage(String url, HttpEntity entity)
|
||||
throws ClientProtocolException, IOException {
|
||||
return HTMLPage.parse(postAsString(url, entity));
|
||||
}
|
||||
|
||||
protected Future<HttpResponse> postAsync(final String url,
|
||||
final HttpEntity entity) throws IOException {
|
||||
return threadPool.submit(new Callable<HttpResponse>() {
|
||||
@Override
|
||||
public HttpResponse call() throws Exception {
|
||||
return post(url, entity);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected Future<String> postAsStringAsync(final String url,
|
||||
final HttpEntity entity) throws IOException {
|
||||
return threadPool.submit(new Callable<String>() {
|
||||
@Override
|
||||
public String call() throws Exception {
|
||||
return postAsString(url, entity);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected Future<HTMLPage> postAsPageAsync(final String url,
|
||||
final HttpEntity entity) throws IOException {
|
||||
return threadPool.submit(new Callable<HTMLPage>() {
|
||||
@Override
|
||||
public HTMLPage call() throws Exception {
|
||||
return postAsPage(url, entity);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,43 +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;
|
||||
|
||||
import com.rogiel.httpchannel.service.config.ServiceConfiguration;
|
||||
|
||||
/**
|
||||
* This is an abstract {@link Service} implementation.
|
||||
*
|
||||
* @author Rogiel
|
||||
* @version 1.0
|
||||
* @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.
|
||||
*/
|
||||
public abstract class AbstractService<T extends ServiceConfiguration>
|
||||
implements Service {
|
||||
protected final T configuration;
|
||||
|
||||
protected AbstractService(T configuration) {
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T getServiceConfiguration() {
|
||||
return configuration;
|
||||
}
|
||||
}
|
||||
@@ -1,44 +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;
|
||||
|
||||
/**
|
||||
* Implements an service capable of authenticating into an account.
|
||||
*
|
||||
* @author Rogiel
|
||||
* @since 1.0
|
||||
*/
|
||||
public interface AuthenticationService extends Service {
|
||||
/**
|
||||
* Creates {@link Authenticator} instance for this service. This instance is
|
||||
* attached to an {@link Credential} and to its parent {@link Service}.
|
||||
*
|
||||
* @param credential
|
||||
* the credential
|
||||
* @return an new {@link Authenticator} instance
|
||||
*/
|
||||
Authenticator getAuthenticator(Credential credential);
|
||||
|
||||
/**
|
||||
* Return the matrix of capabilities for this {@link Authenticator}.
|
||||
*
|
||||
* @return {@link CapabilityMatrix} with all capabilities of this
|
||||
* {@link Authenticator}.
|
||||
* @see AuthenticatorCapability
|
||||
*/
|
||||
CapabilityMatrix<AuthenticatorCapability> getAuthenticationCapability();
|
||||
}
|
||||
@@ -1,51 +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;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.rogiel.httpchannel.service.exception.AuthenticationInvalidCredentialException;
|
||||
|
||||
/**
|
||||
* This interfaces provides authentication for an service.
|
||||
*
|
||||
* @author Rogiel
|
||||
* @since 1.0
|
||||
*/
|
||||
public interface Authenticator {
|
||||
/**
|
||||
* Login into the {@link Service}. Once the authentication is done, it is
|
||||
* persistent for the entire service's operation.<br>
|
||||
* <b>Note</b>: If you want to logout the user, see
|
||||
* {@link Authenticator#logout()}
|
||||
*
|
||||
* @throws IOException
|
||||
* if any IO error occur
|
||||
* @throws AuthenticationInvalidCredentialException
|
||||
* if the credentials are not valid or cannot be used
|
||||
*/
|
||||
void login() throws IOException, AuthenticationInvalidCredentialException;
|
||||
|
||||
/**
|
||||
* Logout into the {@link Service}. The session is restored to an not
|
||||
* logged-in state.
|
||||
*
|
||||
* @throws IOException
|
||||
* if any IO error occur
|
||||
*/
|
||||
void logout() throws IOException;
|
||||
}
|
||||
@@ -1,30 +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;
|
||||
|
||||
/**
|
||||
* Capability an certain {@link Authenticator} can have.
|
||||
*
|
||||
* @author Rogiel
|
||||
* @since 1.0
|
||||
*/
|
||||
public enum AuthenticatorCapability {
|
||||
/**
|
||||
* Mark an {@link Authenticator} capable of fetching account information.
|
||||
*/
|
||||
FETCH_ACCOUNT_INFORMATION;
|
||||
}
|
||||
@@ -1,58 +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;
|
||||
|
||||
/**
|
||||
* This is an utility class to help manage Capabilities of all the services.
|
||||
*
|
||||
* @author Rogiel
|
||||
* @param <T>
|
||||
* the capability enumeration
|
||||
* @since 1.0
|
||||
*/
|
||||
public class CapabilityMatrix<T> {
|
||||
/**
|
||||
* The list of all supported capabilities
|
||||
*/
|
||||
private final T[] matrix;
|
||||
|
||||
/**
|
||||
* Creates a new matrix of capabilities
|
||||
*
|
||||
* @param matrix
|
||||
* all the capabilities this service support
|
||||
*/
|
||||
public CapabilityMatrix(T... matrix) {
|
||||
this.matrix = matrix;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether an certatin capability is in the matrix or not.
|
||||
*
|
||||
* @param capability
|
||||
* the capability being searched in the matrix
|
||||
* @return true if existent, false otherwise
|
||||
*/
|
||||
public boolean has(T capability) {
|
||||
for (final T capScan : matrix) {
|
||||
if (capScan == capability) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,59 +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;
|
||||
|
||||
/**
|
||||
* Pair of username-password used for authenticating into services.
|
||||
*
|
||||
* @author Rogiel
|
||||
* @since 1.0
|
||||
*/
|
||||
public class Credential {
|
||||
private final String username;
|
||||
private final String password;
|
||||
|
||||
/**
|
||||
* Creates a new pair of username-password credential
|
||||
*
|
||||
* @param username
|
||||
* the username
|
||||
* @param password
|
||||
* the password
|
||||
*/
|
||||
public Credential(String username, String password) {
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the username
|
||||
*
|
||||
* @return the username
|
||||
*/
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the password
|
||||
*
|
||||
* @return the password
|
||||
*/
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
}
|
||||
@@ -1,44 +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;
|
||||
|
||||
import java.nio.channels.Channel;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
|
||||
/**
|
||||
* This is an {@link Channel} for downloads. Any data to be downloaded, must be
|
||||
* Redden from this channel.
|
||||
* <p>
|
||||
* Since this {@link Channel} <tt>implements</tt> {@link ReadableByteChannel}
|
||||
* you can treat it as any other regular IO {@link Channel}.
|
||||
* <p>
|
||||
* <b>Remember</b>: always close the {@link Channel}, if you do otherwise, the
|
||||
* resources will not be freed and will consume machine resources.
|
||||
*
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*/
|
||||
public interface DownloadChannel extends ReadableByteChannel {
|
||||
/**
|
||||
* @return the file size
|
||||
*/
|
||||
long getFilesize();
|
||||
|
||||
/**
|
||||
* @return the file name
|
||||
*/
|
||||
String getFilename();
|
||||
}
|
||||
@@ -1,36 +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;
|
||||
|
||||
/**
|
||||
* This listener keeps an track on the progress on an {@link Downloader}
|
||||
* service.
|
||||
*
|
||||
* @author Rogiel
|
||||
* @since 1.0
|
||||
*/
|
||||
public interface DownloadListener {
|
||||
/**
|
||||
* Inform that the downloader will be waiting for an certain amount of time
|
||||
* due to an timer in the download site.
|
||||
*
|
||||
* @param time
|
||||
* the time in ms in which the service will be be waiting.
|
||||
* @return true if desires to wait, false otherwise
|
||||
*/
|
||||
boolean timer(long time);
|
||||
}
|
||||
@@ -1,64 +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;
|
||||
|
||||
import java.net.URL;
|
||||
|
||||
import javax.tools.FileObject;
|
||||
|
||||
/**
|
||||
* Implements an service capable of downloading a file.
|
||||
*
|
||||
* @author Rogiel
|
||||
* @since 1.0
|
||||
*/
|
||||
public interface DownloadService extends Service {
|
||||
/**
|
||||
* Creates a new instance of the {@link Downloader}. This instance will be
|
||||
* attached to the {@link URL}, {@link FileObject} provided through the the
|
||||
* arguments and the parent {@link Service} instance.
|
||||
*
|
||||
* @param url
|
||||
* the url to be downloaded
|
||||
* @param file
|
||||
* the destination file
|
||||
* @return an new instance of {@link Downloader}
|
||||
*/
|
||||
Downloader getDownloader(URL url);
|
||||
|
||||
/**
|
||||
* Check if this {@link Service} can download from this URL. Implemtations
|
||||
* might or might not perform network activity.
|
||||
* <p>
|
||||
* <b>Please note</b> that the value returned by this method may vary based
|
||||
* on it's state (i.e. premium or not).
|
||||
*
|
||||
* @param url
|
||||
* the {@link URL} to be tested.
|
||||
* @return true if supported, false otherwise.
|
||||
*/
|
||||
boolean matchURL(URL url);
|
||||
|
||||
/**
|
||||
* Return the matrix of capabilities for this {@link Downloader}.
|
||||
*
|
||||
* @return {@link CapabilityMatrix} with all capabilities of this
|
||||
* {@link Downloader}.
|
||||
* @see DownloaderCapability
|
||||
*/
|
||||
CapabilityMatrix<DownloaderCapability> getDownloadCapabilities();
|
||||
}
|
||||
@@ -1,49 +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;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.rogiel.httpchannel.service.exception.DownloadLimitExceededException;
|
||||
import com.rogiel.httpchannel.service.exception.DownloadLinkNotFoundException;
|
||||
|
||||
/**
|
||||
* This interfaces provides downloading for an service.
|
||||
*
|
||||
* @author Rogiel
|
||||
* @since 1.0
|
||||
*/
|
||||
public interface Downloader {
|
||||
/**
|
||||
* Starts the download process.
|
||||
*
|
||||
* @param listener
|
||||
* the listener to keep a track on the download progress
|
||||
*
|
||||
* @return the {@link DownloadChannel} instance
|
||||
* @throws IOException
|
||||
* if any IO error occur
|
||||
* @throws DownloadLinkNotFoundException
|
||||
* if the direct download link cannot be found (the file could
|
||||
* have been deleted)
|
||||
* @throws DownloadLimitExceededException
|
||||
* if the download limit has been exceed, most times thrown when
|
||||
* downloading as a non-premium user
|
||||
*/
|
||||
DownloadChannel download(DownloadListener listener) throws IOException,
|
||||
DownloadLinkNotFoundException, DownloadLimitExceededException;
|
||||
}
|
||||
@@ -1,46 +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;
|
||||
|
||||
/**
|
||||
* Capability an certain {@link Downloader} can have.
|
||||
*
|
||||
* @author Rogiel
|
||||
* @since 1.0
|
||||
*/
|
||||
public enum DownloaderCapability {
|
||||
/**
|
||||
* Can download files while not authenticated
|
||||
*/
|
||||
UNAUTHENTICATED_DOWNLOAD,
|
||||
/**
|
||||
* Can download files while authenticated with non-premium account
|
||||
*/
|
||||
NON_PREMIUM_ACCOUNT_DOWNLOAD,
|
||||
/**
|
||||
* Can download files while authenticated with premium account
|
||||
*/
|
||||
PREMIUM_ACCOUNT_DOWNLOAD,
|
||||
/**
|
||||
* Resume interrupted downloads are possible and supported.
|
||||
*/
|
||||
RESUME,
|
||||
/**
|
||||
* Can check the status of the given link before starting download.
|
||||
*/
|
||||
STATUS_CHECK;
|
||||
}
|
||||
@@ -1,61 +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;
|
||||
|
||||
import com.rogiel.httpchannel.service.config.ServiceConfiguration;
|
||||
|
||||
/**
|
||||
* Base interface for all the services. Whenever the operation suported by the
|
||||
* {@link Service} it must implement this interface. Most of implementations
|
||||
* will benefit from abstract instances of this interface:
|
||||
* <ul>
|
||||
* <li>{@link AbstractHttpService}: provides an basic support for HTTP services.
|
||||
* </li>
|
||||
* </ul>
|
||||
*
|
||||
* @author Rogiel
|
||||
* @since 1.0
|
||||
*/
|
||||
public interface Service {
|
||||
/**
|
||||
* Get the ServiceID.
|
||||
*
|
||||
* @return the id of the service
|
||||
*/
|
||||
String getID();
|
||||
|
||||
/**
|
||||
* Get Major version of this service
|
||||
*
|
||||
* @return the major version
|
||||
*/
|
||||
int getMajorVersion();
|
||||
|
||||
/**
|
||||
* Get the minor version of this service
|
||||
*
|
||||
* @return the minor version
|
||||
*/
|
||||
int getMinorVersion();
|
||||
|
||||
/**
|
||||
* Returns this {@link ServiceConfiguration} instance
|
||||
*
|
||||
* @return the {@link ServiceConfiguration} instance
|
||||
*/
|
||||
ServiceConfiguration getServiceConfiguration();
|
||||
}
|
||||
@@ -1,50 +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;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
/**
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*/
|
||||
public class ServiceHelper {
|
||||
private final Service service;
|
||||
|
||||
/**
|
||||
* @param service
|
||||
* the service
|
||||
*/
|
||||
public ServiceHelper(Service service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
public UploadChannel upload(Path path, String description)
|
||||
throws IOException {
|
||||
return ((UploadService) service).getUploader(
|
||||
path.getFileName().toString(), Files.size(path), description)
|
||||
.upload();
|
||||
}
|
||||
|
||||
public UploadChannel upload(File file, String description)
|
||||
throws IOException {
|
||||
return ((UploadService) service).getUploader(file.getName(),
|
||||
file.length(), description).upload();
|
||||
}
|
||||
}
|
||||
@@ -1,63 +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;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.channels.Channel;
|
||||
import java.nio.channels.WritableByteChannel;
|
||||
|
||||
import com.rogiel.httpchannel.service.exception.UploadLinkNotFoundException;
|
||||
|
||||
/**
|
||||
* This is an {@link Channel} for uploads. Any data to be uploaded, must be
|
||||
* written into this channel.
|
||||
* <p>
|
||||
* Since this {@link Channel} <tt>implements</tt> {@link WritableByteChannel}
|
||||
* you can treat it as any other regular IO {@link Channel}.
|
||||
* <p>
|
||||
* <b>Remember</b>: always close the {@link Channel}, if you do otherwise, your
|
||||
* upload will not finish and will never return the link.
|
||||
*
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*/
|
||||
public interface UploadChannel extends WritableByteChannel {
|
||||
/**
|
||||
* @return the file size
|
||||
*/
|
||||
long getFilesize();
|
||||
|
||||
/**
|
||||
* @return the file name
|
||||
*/
|
||||
String getFilename();
|
||||
|
||||
/**
|
||||
* The link is located after you call {@link UploadChannel#close()}, but it
|
||||
* can only be retrieved by calling this method. If {@link #close()} throwed
|
||||
* an exception, this method might return <tt>null</tt>.
|
||||
*
|
||||
* @return the download link for this upload
|
||||
*/
|
||||
String getDownloadLink();
|
||||
|
||||
/**
|
||||
* @throws UploadLinkNotFoundException
|
||||
* if after the upload, the download link cannot be found
|
||||
*/
|
||||
@Override
|
||||
void close() throws IOException, UploadLinkNotFoundException;
|
||||
}
|
||||
@@ -1,71 +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;
|
||||
|
||||
/**
|
||||
* Implements an service capable of uploading a file.
|
||||
*
|
||||
* @author Rogiel
|
||||
* @since 1.0
|
||||
*/
|
||||
public interface UploadService extends Service {
|
||||
/**
|
||||
* Creates a new instance of {@link Uploader}. This instance is attached
|
||||
* with the parent {@link Service} instance.<br>
|
||||
* <b>Note</b>: not all services might support <tt>description</tt>
|
||||
*
|
||||
* @param filename
|
||||
* the name of the file to be uploaded
|
||||
* @param filesize
|
||||
* the size of the file to be uploaded. This must be exact.
|
||||
* @param description
|
||||
* the description of the upload. If supported.
|
||||
* @return the new {@link Uploader} instance
|
||||
*/
|
||||
Uploader getUploader(String filename, long filesize, String description);
|
||||
|
||||
/**
|
||||
* Get the maximum upload file size supported by this service.
|
||||
* <p>
|
||||
* <b>Please note</b> that the value returned by this method may vary based
|
||||
* on it's state (i.e. premium or not).
|
||||
*
|
||||
* @return the maximum filesize supported
|
||||
*/
|
||||
long getMaximumFilesize();
|
||||
|
||||
/**
|
||||
* Get the list of all supported extensions. Might return <tt>null</tt> if
|
||||
* there is no restriction.
|
||||
* <p>
|
||||
* <b>Please note</b> that the value returned by this method may vary based
|
||||
* on it's state (i.e. premium or not).
|
||||
*
|
||||
* @return the list of supported file extensions. Can return <tt>null</tt>
|
||||
* if there is not restriction
|
||||
*/
|
||||
String[] getSupportedExtensions();
|
||||
|
||||
/**
|
||||
* Return the matrix of capabilities for this {@link Uploader}.
|
||||
*
|
||||
* @return {@link CapabilityMatrix} with all capabilities of this
|
||||
* {@link Uploader}.
|
||||
* @see UploaderCapability
|
||||
*/
|
||||
CapabilityMatrix<UploaderCapability> getUploadCapabilities();
|
||||
}
|
||||
@@ -1,36 +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;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* This interfaces provides uploading for an service.
|
||||
*
|
||||
* @author Rogiel
|
||||
* @since 1.0
|
||||
*/
|
||||
public interface Uploader {
|
||||
/**
|
||||
* Starts the upload process on this service.
|
||||
*
|
||||
* @return the {@link UploadChannel} instance
|
||||
* @throws IOException
|
||||
* if any IO error occur
|
||||
*/
|
||||
UploadChannel upload() throws IOException;
|
||||
}
|
||||
@@ -1,38 +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;
|
||||
|
||||
/**
|
||||
* Capability an certain {@link Uploader} can have.
|
||||
*
|
||||
* @author Rogiel
|
||||
* @since 1.0
|
||||
*/
|
||||
public enum UploaderCapability {
|
||||
/**
|
||||
* Can upload while not authenticated into any account
|
||||
*/
|
||||
UNAUTHENTICATED_UPLOAD,
|
||||
/**
|
||||
* Can upload while authenticated with a non-premium account
|
||||
*/
|
||||
NON_PREMIUM_ACCOUNT_UPLOAD,
|
||||
/**
|
||||
* Can upload while authenticated with a premium account
|
||||
*/
|
||||
PREMIUM_ACCOUNT_UPLOAD;
|
||||
}
|
||||
@@ -1,26 +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.captcha;
|
||||
|
||||
/**
|
||||
* @author Rogiel
|
||||
* @since 1.0
|
||||
*/
|
||||
public interface Captcha {
|
||||
String getAnswer();
|
||||
void setAnswer(String answer);
|
||||
}
|
||||
@@ -1,32 +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.captcha;
|
||||
|
||||
/**
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*
|
||||
*/
|
||||
public interface CaptchaResolver {
|
||||
/**
|
||||
* Passes an captcha by parameter and waits for the response of the
|
||||
* challenge.
|
||||
*
|
||||
* @param captcha
|
||||
* the captcha challenge
|
||||
*/
|
||||
String resolve(Captcha captcha);
|
||||
}
|
||||
@@ -1,47 +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.captcha;
|
||||
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
* @author Rogiel
|
||||
* @since 1.0
|
||||
*/
|
||||
public class ImageCaptcha implements Captcha {
|
||||
private URL url;
|
||||
|
||||
private String answer;
|
||||
|
||||
public URL getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(URL url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAnswer() {
|
||||
return answer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAnswer(String answer) {
|
||||
this.answer = answer;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -1,74 +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;
|
||||
import com.rogiel.httpchannel.util.ThreadUtils;
|
||||
|
||||
/**
|
||||
* {@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()) {
|
||||
ThreadUtils.sleep(500);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCharset() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getContentLength() {
|
||||
return channel.getFilesize();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTransferEncoding() {
|
||||
return "binary";
|
||||
}
|
||||
}
|
||||
@@ -1,41 +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.config;
|
||||
|
||||
import java.lang.reflect.Proxy;
|
||||
|
||||
import com.rogiel.httpchannel.util.transformer.Transformer;
|
||||
|
||||
|
||||
/**
|
||||
* This is an flag interface to indicate that an certain Interface is the
|
||||
* configuration for the service.<br>
|
||||
* <br>
|
||||
* Every service must create an <tt>interface</tt> with the configuration
|
||||
* methods, additionally an Annotation informing the default value.
|
||||
* ServiceConfiguration implementations might use reflection ({@link Proxy}),
|
||||
* hard-coding or any other way for fetching the data.<br>
|
||||
* <br>
|
||||
* String data stored in the annotation is converted to Java Types using the
|
||||
* {@link Transformer} class.
|
||||
*
|
||||
* @author Rogiel
|
||||
* @version 1.0
|
||||
* @see Transformer
|
||||
*/
|
||||
public interface ServiceConfiguration {
|
||||
}
|
||||
@@ -1,113 +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.config;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.Properties;
|
||||
|
||||
import com.rogiel.httpchannel.util.transformer.TransformerFactory;
|
||||
|
||||
|
||||
/**
|
||||
* Helper class for {@link ServiceConfiguration} system.
|
||||
*
|
||||
* @author Rogiel
|
||||
* @since 1.0
|
||||
*/
|
||||
public class ServiceConfigurationHelper {
|
||||
/**
|
||||
* Creates a Proxy Class that returns all the default values of
|
||||
* configuration interfaces. The values are mapped by
|
||||
* {@link ServiceConfigurationProperty} annotation.
|
||||
*
|
||||
* @param <T>
|
||||
* the interface extending {@link ServiceConfiguration}. Service
|
||||
* specific.
|
||||
* @param type
|
||||
* the type Class representing T.
|
||||
* @return the proxied {@link ServiceConfiguration} instance.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T extends ServiceConfiguration> T defaultConfiguration(
|
||||
Class<T> type) {
|
||||
return (T) Proxy.newProxyInstance(
|
||||
ServiceConfiguration.class.getClassLoader(),
|
||||
new Class<?>[] { type }, new InvocationHandler() {
|
||||
@Override
|
||||
public Object invoke(Object object, Method method,
|
||||
Object[] arguments) throws Throwable {
|
||||
final ServiceConfigurationProperty property = method
|
||||
.getAnnotation(ServiceConfigurationProperty.class);
|
||||
if (property != null)
|
||||
return TransformerFactory.getTransformer(
|
||||
method.getReturnType()).transform(
|
||||
property.defaultValue());
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a Proxy Class that returns all the default values of
|
||||
* configuration interfaces. The values are mapped by
|
||||
* {@link ServiceConfigurationProperty} annotation.
|
||||
*
|
||||
* @param <T>
|
||||
* the interface extending {@link ServiceConfiguration}. Service
|
||||
* specific.
|
||||
* @param type
|
||||
* the type Class representing T.
|
||||
* @return the proxied {@link ServiceConfiguration} instance.
|
||||
* @throws IOException
|
||||
* @throws FileNotFoundException
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T extends ServiceConfiguration> T file(Class<T> type,
|
||||
File file) throws FileNotFoundException, IOException {
|
||||
final Properties properties = new Properties();
|
||||
properties.load(new FileInputStream(file));
|
||||
|
||||
return (T) Proxy.newProxyInstance(
|
||||
ServiceConfiguration.class.getClassLoader(),
|
||||
new Class<?>[] { type }, new InvocationHandler() {
|
||||
@Override
|
||||
public Object invoke(Object object, Method method,
|
||||
Object[] arguments) throws Throwable {
|
||||
final ServiceConfigurationProperty property = method
|
||||
.getAnnotation(ServiceConfigurationProperty.class);
|
||||
if (property != null)
|
||||
return TransformerFactory.getTransformer(
|
||||
method.getReturnType()).transform(
|
||||
get(property));
|
||||
return null;
|
||||
}
|
||||
|
||||
private String get(ServiceConfigurationProperty property) {
|
||||
String value = properties.getProperty(property.key());
|
||||
if (value == null)
|
||||
value = property.defaultValue();
|
||||
return value;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,51 +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.config;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Annotation that defines the default value for an {@link ServiceConfiguration}
|
||||
* method.<br>
|
||||
* <br>
|
||||
* <h1>Usage example</h1>
|
||||
*
|
||||
* <pre>
|
||||
* public interface DummyServiceConfiguration extends ServiceConfiguration {
|
||||
* @ServiceConfigurationProperty(defaultValue = "true")
|
||||
* boolean retryAllowed();
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* The default implementation created by
|
||||
* {@link ServiceConfigurationHelper#defaultConfiguration()} will always return
|
||||
* the <tt>defaultValue</tt>.
|
||||
*
|
||||
* @author Rogiel
|
||||
* @version 1.0
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
@Documented
|
||||
public @interface ServiceConfigurationProperty {
|
||||
String key();
|
||||
String defaultValue();
|
||||
}
|
||||
@@ -1,61 +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.exception;
|
||||
|
||||
/**
|
||||
* Exception thrown if the authentication credential is not valid.
|
||||
*
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*/
|
||||
public class AuthenticationInvalidCredentialException extends
|
||||
AuthenticationServiceException {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* Creates a new empty instance of this exception
|
||||
*/
|
||||
public AuthenticationInvalidCredentialException() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param message
|
||||
* the message
|
||||
* @param cause
|
||||
* the root cause
|
||||
*/
|
||||
public AuthenticationInvalidCredentialException(String message,
|
||||
Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param message
|
||||
* the message
|
||||
*/
|
||||
public AuthenticationInvalidCredentialException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param cause
|
||||
* the root cause
|
||||
*/
|
||||
public AuthenticationInvalidCredentialException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
||||
@@ -1,51 +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.exception;
|
||||
|
||||
/**
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*
|
||||
*/
|
||||
public class AuthenticationServiceException extends ChannelServiceException {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public AuthenticationServiceException() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param message
|
||||
* @param cause
|
||||
*/
|
||||
public AuthenticationServiceException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param message
|
||||
*/
|
||||
public AuthenticationServiceException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param cause
|
||||
*/
|
||||
public AuthenticationServiceException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
||||
@@ -1,53 +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.exception;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*
|
||||
*/
|
||||
public class ChannelServiceException extends IOException {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public ChannelServiceException() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param message
|
||||
* @param cause
|
||||
*/
|
||||
public ChannelServiceException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param message
|
||||
*/
|
||||
public ChannelServiceException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param cause
|
||||
*/
|
||||
public ChannelServiceException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
||||
@@ -1,62 +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.exception;
|
||||
|
||||
import com.rogiel.httpchannel.service.captcha.CaptchaResolver;
|
||||
|
||||
/**
|
||||
* Exception thrown if the {@link CaptchaResolver} has returned an invalid
|
||||
* captcha
|
||||
*
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*/
|
||||
public class DownloadInvalidCaptchaException extends DownloadServiceException {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* Creates a new empty instance of this exception
|
||||
*/
|
||||
public DownloadInvalidCaptchaException() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param message
|
||||
* the message
|
||||
* @param cause
|
||||
* the root cause
|
||||
*/
|
||||
public DownloadInvalidCaptchaException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param message
|
||||
* the message
|
||||
*/
|
||||
public DownloadInvalidCaptchaException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param cause
|
||||
* the root cause
|
||||
*/
|
||||
public DownloadInvalidCaptchaException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
||||
@@ -1,59 +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.exception;
|
||||
|
||||
/**
|
||||
* Exception thrown if the download limit has been exceeded.
|
||||
*
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*/
|
||||
public class DownloadLimitExceededException extends DownloadServiceException {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* Creates a new empty instance of this exception
|
||||
*/
|
||||
public DownloadLimitExceededException() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param message
|
||||
* the message
|
||||
* @param cause
|
||||
* the root cause
|
||||
*/
|
||||
public DownloadLimitExceededException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param message
|
||||
* the message
|
||||
*/
|
||||
public DownloadLimitExceededException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param cause
|
||||
* the root cause
|
||||
*/
|
||||
public DownloadLimitExceededException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
||||
@@ -1,59 +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.exception;
|
||||
|
||||
/**
|
||||
* Exception thrown if the direct download link could not be found.
|
||||
*
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*/
|
||||
public class DownloadLinkNotFoundException extends DownloadServiceException {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* Creates a new empty instance of this exception
|
||||
*/
|
||||
public DownloadLinkNotFoundException() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param message
|
||||
* the message
|
||||
* @param cause
|
||||
* the root cause
|
||||
*/
|
||||
public DownloadLinkNotFoundException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param message
|
||||
* the message
|
||||
*/
|
||||
public DownloadLinkNotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param cause
|
||||
* the root cause
|
||||
*/
|
||||
public DownloadLinkNotFoundException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
||||
@@ -1,51 +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.exception;
|
||||
|
||||
/**
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*
|
||||
*/
|
||||
public class DownloadServiceException extends ChannelServiceException {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public DownloadServiceException() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param message
|
||||
* @param cause
|
||||
*/
|
||||
public DownloadServiceException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param message
|
||||
*/
|
||||
public DownloadServiceException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param cause
|
||||
*/
|
||||
public DownloadServiceException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
||||
@@ -1,59 +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.exception;
|
||||
|
||||
/**
|
||||
* Exception thrown if the download link could not be located after the upload.
|
||||
*
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*/
|
||||
public class UploadLinkNotFoundException extends UploadServiceException {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* Creates a new empty instance of this exception
|
||||
*/
|
||||
public UploadLinkNotFoundException() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param message
|
||||
* the message
|
||||
* @param cause
|
||||
* the root cause
|
||||
*/
|
||||
public UploadLinkNotFoundException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param message
|
||||
* the message
|
||||
*/
|
||||
public UploadLinkNotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param cause
|
||||
* the root cause
|
||||
*/
|
||||
public UploadLinkNotFoundException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
||||
@@ -1,51 +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.exception;
|
||||
|
||||
/**
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*
|
||||
*/
|
||||
public class UploadServiceException extends ChannelServiceException {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public UploadServiceException() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param message
|
||||
* @param cause
|
||||
*/
|
||||
public UploadServiceException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param message
|
||||
*/
|
||||
public UploadServiceException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param cause
|
||||
*/
|
||||
public UploadServiceException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
||||
@@ -1,319 +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.impl;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.entity.mime.MultipartEntity;
|
||||
import org.apache.http.entity.mime.content.StringBody;
|
||||
import org.htmlparser.Tag;
|
||||
|
||||
import com.rogiel.httpchannel.service.AbstractDownloader;
|
||||
import com.rogiel.httpchannel.service.AbstractHttpService;
|
||||
import com.rogiel.httpchannel.service.AuthenticationService;
|
||||
import com.rogiel.httpchannel.service.Authenticator;
|
||||
import com.rogiel.httpchannel.service.AuthenticatorCapability;
|
||||
import com.rogiel.httpchannel.service.CapabilityMatrix;
|
||||
import com.rogiel.httpchannel.service.Credential;
|
||||
import com.rogiel.httpchannel.service.DownloadChannel;
|
||||
import com.rogiel.httpchannel.service.DownloadListener;
|
||||
import com.rogiel.httpchannel.service.DownloadService;
|
||||
import com.rogiel.httpchannel.service.Downloader;
|
||||
import com.rogiel.httpchannel.service.DownloaderCapability;
|
||||
import com.rogiel.httpchannel.service.Service;
|
||||
import com.rogiel.httpchannel.service.UploadChannel;
|
||||
import com.rogiel.httpchannel.service.UploadService;
|
||||
import com.rogiel.httpchannel.service.Uploader;
|
||||
import com.rogiel.httpchannel.service.UploaderCapability;
|
||||
import com.rogiel.httpchannel.service.channel.InputStreamDownloadChannel;
|
||||
import com.rogiel.httpchannel.service.channel.LinkedUploadChannel;
|
||||
import com.rogiel.httpchannel.service.channel.LinkedUploadChannelContentBody;
|
||||
import com.rogiel.httpchannel.service.channel.LinkedUploadChannel.LinkedUploadChannelCloseCallback;
|
||||
import com.rogiel.httpchannel.service.config.ServiceConfiguration;
|
||||
import com.rogiel.httpchannel.service.exception.AuthenticationInvalidCredentialException;
|
||||
import com.rogiel.httpchannel.service.impl.HotFileService.HotFileServiceConfiguration;
|
||||
import com.rogiel.httpchannel.util.ThreadUtils;
|
||||
import com.rogiel.httpchannel.util.htmlparser.HTMLPage;
|
||||
|
||||
/**
|
||||
* This service handles login, upload and download to HotFile.com.
|
||||
*
|
||||
* @author Rogiel
|
||||
* @since 1.0
|
||||
*/
|
||||
public class HotFileService extends
|
||||
AbstractHttpService<HotFileServiceConfiguration> implements Service,
|
||||
UploadService, DownloadService, AuthenticationService {
|
||||
private static final Pattern UPLOAD_URL_PATTERN = Pattern
|
||||
.compile("http://u[0-9]*\\.hotfile\\.com/upload\\.cgi\\?[0-9]*");
|
||||
|
||||
private static final Pattern DOWNLOAD_DIRECT_LINK_PATTERN = Pattern
|
||||
.compile("http://hotfile\\.com/get/([0-9]*)/([A-Za-z0-9]*)/([A-Za-z0-9]*)/(.*)");
|
||||
// private static final Pattern DOWNLOAD_TIMER = Pattern
|
||||
// .compile("timerend=d\\.getTime\\(\\)\\+([0-9]*);");
|
||||
// private static final Pattern DOWNLOAD_FILESIZE = Pattern
|
||||
// .compile("[0-9]*(\\.[0-9]*)? (K|M|G)B");
|
||||
|
||||
private static final Pattern DOWNLOAD_URL_PATTERN = Pattern
|
||||
.compile("http://hotfile\\.com/dl/([0-9]*)/([A-Za-z0-9]*)/(.*)");
|
||||
|
||||
public HotFileService(final HotFileServiceConfiguration configuration) {
|
||||
super(configuration);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getID() {
|
||||
return "hotfile";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMajorVersion() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMinorVersion() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Uploader getUploader(String filename, long filesize,
|
||||
String description) {
|
||||
return new HotFileUploader(filename, filesize);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getMaximumFilesize() {
|
||||
return 1 * 1024 * 1024 * 1024;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getSupportedExtensions() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CapabilityMatrix<UploaderCapability> getUploadCapabilities() {
|
||||
return new CapabilityMatrix<UploaderCapability>(
|
||||
UploaderCapability.UNAUTHENTICATED_UPLOAD,
|
||||
UploaderCapability.NON_PREMIUM_ACCOUNT_UPLOAD,
|
||||
UploaderCapability.PREMIUM_ACCOUNT_UPLOAD);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Downloader getDownloader(URL url) {
|
||||
return new HotFileDownloader(url);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matchURL(URL url) {
|
||||
return DOWNLOAD_URL_PATTERN.matcher(url.toString()).matches();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CapabilityMatrix<DownloaderCapability> getDownloadCapabilities() {
|
||||
return new CapabilityMatrix<DownloaderCapability>(
|
||||
DownloaderCapability.PREMIUM_ACCOUNT_DOWNLOAD);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Authenticator getAuthenticator(Credential credential) {
|
||||
return new HotFileAuthenticator(credential);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CapabilityMatrix<AuthenticatorCapability> getAuthenticationCapability() {
|
||||
return new CapabilityMatrix<AuthenticatorCapability>();
|
||||
}
|
||||
|
||||
protected class HotFileUploader implements Uploader,
|
||||
LinkedUploadChannelCloseCallback {
|
||||
private final String filename;
|
||||
private final long filesize;
|
||||
|
||||
private Future<HTMLPage> uploadFuture;
|
||||
|
||||
public HotFileUploader(String filename, long filesize) {
|
||||
super();
|
||||
this.filename = filename;
|
||||
this.filesize = filesize;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UploadChannel upload() throws IOException {
|
||||
final HTMLPage page = getAsPage("http://www.hotfile.com/");
|
||||
final String action = page.getFormAction(UPLOAD_URL_PATTERN);
|
||||
|
||||
final LinkedUploadChannel channel = new LinkedUploadChannel(this,
|
||||
filesize, filename);
|
||||
final MultipartEntity entity = new MultipartEntity();
|
||||
|
||||
entity.addPart("uploads[]", new LinkedUploadChannelContentBody(channel));
|
||||
|
||||
uploadFuture = postAsPageAsync(action, entity);
|
||||
while (!channel.isLinked() && !uploadFuture.isDone()) {
|
||||
ThreadUtils.sleep(100);
|
||||
}
|
||||
return channel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String finish() throws IOException {
|
||||
try {
|
||||
return uploadFuture.get().getInputValue(DOWNLOAD_URL_PATTERN);
|
||||
} catch (InterruptedException e) {
|
||||
return null;
|
||||
} catch (ExecutionException e) {
|
||||
throw (IOException) e.getCause();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected class HotFileDownloader extends AbstractDownloader {
|
||||
private final URL url;
|
||||
|
||||
public HotFileDownloader(URL url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DownloadChannel download(DownloadListener listener)
|
||||
throws IOException {
|
||||
final HTMLPage page = getAsPage(url.toString());
|
||||
|
||||
// // try to find timer
|
||||
// final String stringTimer = PatternUtils.find(DOWNLOAD_TIMER,
|
||||
// content, 2, 1);
|
||||
// int timer = 0;
|
||||
// if (stringTimer != null && stringTimer.length() > 0) {
|
||||
// timer = Integer.parseInt(stringTimer);
|
||||
// }
|
||||
// if (timer > 0) {
|
||||
// throw new DownloadLimitExceededException("Must wait " + timer
|
||||
// + " milliseconds");
|
||||
// }
|
||||
|
||||
final String downloadUrl = page
|
||||
.getLink(DOWNLOAD_DIRECT_LINK_PATTERN);
|
||||
// final String tmHash = PatternUtils.find(DOWNLOAD_TMHASH_PATTERN,
|
||||
// content);F
|
||||
if (downloadUrl != null && downloadUrl.length() > 0) {
|
||||
final HttpResponse downloadResponse = get(downloadUrl);
|
||||
|
||||
final String filename = FilenameUtils.getName(downloadUrl);
|
||||
long contentLength = getContentLength(downloadResponse);
|
||||
|
||||
return new InputStreamDownloadChannel(downloadResponse
|
||||
.getEntity().getContent(), contentLength, filename);
|
||||
// } else if (tmHash != null) {
|
||||
// String dlUrl = PatternUtils.find(FREE_DOWNLOAD_URL_PATTERN,
|
||||
// content);
|
||||
//
|
||||
// String action = PatternUtils.find(DOWNLOAD_ACTION_PATTERN,
|
||||
// content, 1);
|
||||
// int tm = PatternUtils.findInt(DOWNLOAD_TM_PATTERN, content,
|
||||
// 1);
|
||||
// int wait = PatternUtils.findInt(DOWNLOAD_WAIT_PATTERN,
|
||||
// content,
|
||||
// 1);
|
||||
// String waitHash =
|
||||
// PatternUtils.find(DOWNLOAD_WAITHASH_PATTERN,
|
||||
// content, 1);
|
||||
// String upId = PatternUtils.find(DOWNLOAD_UPIDHASH_PATTERN,
|
||||
// content, 1);
|
||||
//
|
||||
// System.out.println("Wait time: "+wait);
|
||||
//
|
||||
// if (wait > 0)
|
||||
// timer(listener, wait * 1000);
|
||||
//
|
||||
// final HttpPost downloadPost = new
|
||||
// HttpPost("http://www.hotfile.com"+dlUrl);
|
||||
// final List<NameValuePair> pairs = new
|
||||
// ArrayList<NameValuePair>();
|
||||
// pairs.add(new BasicNameValuePair("action", action));
|
||||
// pairs.add(new BasicNameValuePair("tm",
|
||||
// Integer.toString(tm)));
|
||||
// pairs.add(new BasicNameValuePair("tmhash", tmHash));
|
||||
// pairs.add(new BasicNameValuePair("wait",
|
||||
// Integer.toString(wait)));
|
||||
// pairs.add(new BasicNameValuePair("waithash", waitHash));
|
||||
// pairs.add(new BasicNameValuePair("upidhash", upId));
|
||||
//
|
||||
// downloadPost.setEntity(new UrlEncodedFormEntity(pairs));
|
||||
//
|
||||
// final HttpResponse downloadResponse = client
|
||||
// .execute(downloadPost);
|
||||
// System.out.println(IOUtils.toString(downloadResponse.getEntity().getContent()));
|
||||
//
|
||||
// return new InputStreamDownloadChannel(downloadResponse
|
||||
// .getEntity().getContent(), 0, "haha");
|
||||
} else {
|
||||
throw new IOException("Download link not found");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected class HotFileAuthenticator implements Authenticator {
|
||||
private final Credential credential;
|
||||
|
||||
public HotFileAuthenticator(Credential credential) {
|
||||
this.credential = credential;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void login() throws ClientProtocolException, IOException {
|
||||
final MultipartEntity entity = new MultipartEntity();
|
||||
|
||||
entity.addPart("returnto", new StringBody("/index.php"));
|
||||
entity.addPart("user", new StringBody(credential.getUsername()));
|
||||
entity.addPart("pass", new StringBody(credential.getPassword()));
|
||||
|
||||
HTMLPage page = postAsPage("http://www.hotfile.com/login.php",
|
||||
entity);
|
||||
final Tag accountTag = page.getTagByID("account");
|
||||
if (accountTag == null)
|
||||
throw new AuthenticationInvalidCredentialException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logout() throws IOException {
|
||||
final MultipartEntity entity = new MultipartEntity();
|
||||
entity.addPart("logout", new StringBody("1"));
|
||||
|
||||
postAsString("http://www.megaupload.com/?c=account", entity);
|
||||
// TODO check logout status
|
||||
}
|
||||
}
|
||||
|
||||
public static interface HotFileServiceConfiguration extends
|
||||
ServiceConfiguration {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.getClass().getSimpleName() + " " + getMajorVersion() + "."
|
||||
+ getMinorVersion();
|
||||
}
|
||||
}
|
||||
@@ -1,328 +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.impl;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.entity.mime.MultipartEntity;
|
||||
import org.apache.http.entity.mime.content.StringBody;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
|
||||
import com.rogiel.httpchannel.service.AbstractDownloader;
|
||||
import com.rogiel.httpchannel.service.AbstractHttpService;
|
||||
import com.rogiel.httpchannel.service.AuthenticationService;
|
||||
import com.rogiel.httpchannel.service.Authenticator;
|
||||
import com.rogiel.httpchannel.service.AuthenticatorCapability;
|
||||
import com.rogiel.httpchannel.service.CapabilityMatrix;
|
||||
import com.rogiel.httpchannel.service.Credential;
|
||||
import com.rogiel.httpchannel.service.DownloadChannel;
|
||||
import com.rogiel.httpchannel.service.DownloadListener;
|
||||
import com.rogiel.httpchannel.service.DownloadService;
|
||||
import com.rogiel.httpchannel.service.Downloader;
|
||||
import com.rogiel.httpchannel.service.DownloaderCapability;
|
||||
import com.rogiel.httpchannel.service.Service;
|
||||
import com.rogiel.httpchannel.service.UploadChannel;
|
||||
import com.rogiel.httpchannel.service.UploadService;
|
||||
import com.rogiel.httpchannel.service.Uploader;
|
||||
import com.rogiel.httpchannel.service.UploaderCapability;
|
||||
import com.rogiel.httpchannel.service.channel.InputStreamDownloadChannel;
|
||||
import com.rogiel.httpchannel.service.channel.LinkedUploadChannel;
|
||||
import com.rogiel.httpchannel.service.channel.LinkedUploadChannel.LinkedUploadChannelCloseCallback;
|
||||
import com.rogiel.httpchannel.service.channel.LinkedUploadChannelContentBody;
|
||||
import com.rogiel.httpchannel.service.config.ServiceConfiguration;
|
||||
import com.rogiel.httpchannel.service.config.ServiceConfigurationProperty;
|
||||
import com.rogiel.httpchannel.service.exception.AuthenticationInvalidCredentialException;
|
||||
import com.rogiel.httpchannel.service.exception.DownloadLimitExceededException;
|
||||
import com.rogiel.httpchannel.service.exception.DownloadLinkNotFoundException;
|
||||
import com.rogiel.httpchannel.service.exception.UploadLinkNotFoundException;
|
||||
import com.rogiel.httpchannel.service.impl.MegaUploadService.MegaUploadServiceConfiguration;
|
||||
import com.rogiel.httpchannel.util.HttpClientUtils;
|
||||
import com.rogiel.httpchannel.util.PatternUtils;
|
||||
import com.rogiel.httpchannel.util.ThreadUtils;
|
||||
import com.rogiel.httpchannel.util.htmlparser.HTMLPage;
|
||||
|
||||
/**
|
||||
* This service handles login, upload and download to MegaUpload.com.
|
||||
*
|
||||
* @author Rogiel
|
||||
* @since 1.0
|
||||
*/
|
||||
public class MegaUploadService extends
|
||||
AbstractHttpService<MegaUploadServiceConfiguration> implements Service,
|
||||
UploadService, DownloadService, AuthenticationService {
|
||||
private static final Pattern UPLOAD_URL_PATTERN = Pattern
|
||||
.compile("http://www([0-9]*)\\.megaupload\\.com/upload_done\\.php\\?UPLOAD_IDENTIFIER=[0-9]*");
|
||||
|
||||
private static final Pattern DOWNLOAD_DIRECT_LINK_PATTERN = Pattern
|
||||
.compile("http://www([0-9]*)\\.megaupload\\.com/files/([A-Za-z0-9]*)/(.*)");
|
||||
private static final Pattern DOWNLOAD_TIMER = Pattern
|
||||
.compile("count=([0-9]*);");
|
||||
// private static final Pattern DOWNLOAD_FILESIZE = Pattern
|
||||
// .compile("[0-9]*(\\.[0-9]*)? (K|M|G)B");
|
||||
|
||||
private static final Pattern DOWNLOAD_URL_PATTERN = Pattern
|
||||
.compile("http://www\\.megaupload\\.com/\\?d=([A-Za-z0-9]*)");
|
||||
|
||||
private static final Pattern LOGIN_USERNAME_PATTERN = Pattern
|
||||
.compile("flashvars\\.username = \"(.*)\";");
|
||||
|
||||
public MegaUploadService(final MegaUploadServiceConfiguration configuration) {
|
||||
super(configuration);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getID() {
|
||||
return "megaupload";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMajorVersion() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMinorVersion() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Uploader getUploader(String filename, long filesize,
|
||||
String description) {
|
||||
return new MegaUploadUploader(filename, filesize, description);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getMaximumFilesize() {
|
||||
return 1 * 1024 * 1024 * 1024;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getSupportedExtensions() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CapabilityMatrix<UploaderCapability> getUploadCapabilities() {
|
||||
return new CapabilityMatrix<UploaderCapability>(
|
||||
UploaderCapability.UNAUTHENTICATED_UPLOAD,
|
||||
UploaderCapability.NON_PREMIUM_ACCOUNT_UPLOAD,
|
||||
UploaderCapability.PREMIUM_ACCOUNT_UPLOAD);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Downloader getDownloader(URL url) {
|
||||
return new MegaUploadDownloader(url);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matchURL(URL url) {
|
||||
return DOWNLOAD_URL_PATTERN.matcher(url.toString()).matches();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CapabilityMatrix<DownloaderCapability> getDownloadCapabilities() {
|
||||
return new CapabilityMatrix<DownloaderCapability>(
|
||||
DownloaderCapability.UNAUTHENTICATED_DOWNLOAD,
|
||||
DownloaderCapability.NON_PREMIUM_ACCOUNT_DOWNLOAD,
|
||||
DownloaderCapability.PREMIUM_ACCOUNT_DOWNLOAD);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Authenticator getAuthenticator(Credential credential) {
|
||||
return new MegaUploadAuthenticator(credential);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CapabilityMatrix<AuthenticatorCapability> getAuthenticationCapability() {
|
||||
return new CapabilityMatrix<AuthenticatorCapability>();
|
||||
}
|
||||
|
||||
protected class MegaUploadUploader implements Uploader,
|
||||
LinkedUploadChannelCloseCallback {
|
||||
private final String filename;
|
||||
private final long filesize;
|
||||
private final String description;
|
||||
|
||||
private Future<String> uploadFuture;
|
||||
|
||||
public MegaUploadUploader(String filename, long filesize,
|
||||
String description) {
|
||||
this.filename = filename;
|
||||
this.filesize = filesize;
|
||||
this.description = (description != null ? description
|
||||
: configuration.getDefaultUploadDescription());
|
||||
}
|
||||
|
||||
@Override
|
||||
public UploadChannel upload() throws IOException {
|
||||
final HTMLPage page = getAsPage("http://www.megaupload.com/multiupload/");
|
||||
final String url = page.getFormAction(UPLOAD_URL_PATTERN);
|
||||
|
||||
final LinkedUploadChannel channel = new LinkedUploadChannel(this,
|
||||
filesize, filename);
|
||||
final MultipartEntity entity = new MultipartEntity();
|
||||
|
||||
entity.addPart("multifile_0", new LinkedUploadChannelContentBody(
|
||||
channel));
|
||||
entity.addPart("multimessage_0", new StringBody(description));
|
||||
|
||||
uploadFuture = postAsStringAsync(url, entity);
|
||||
while (!channel.isLinked() && !uploadFuture.isDone()) {
|
||||
ThreadUtils.sleep(100);
|
||||
}
|
||||
return channel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String finish() throws IOException {
|
||||
try {
|
||||
String link = PatternUtils.find(DOWNLOAD_URL_PATTERN,
|
||||
uploadFuture.get());
|
||||
if (link == null)
|
||||
throw new UploadLinkNotFoundException();
|
||||
return link;
|
||||
} catch (InterruptedException e) {
|
||||
return null;
|
||||
} catch (ExecutionException e) {
|
||||
throw (IOException) e.getCause();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected class MegaUploadDownloader extends AbstractDownloader {
|
||||
private final URL url;
|
||||
|
||||
public MegaUploadDownloader(URL url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DownloadChannel download(DownloadListener listener)
|
||||
throws IOException {
|
||||
HttpResponse response = get(url.toString());
|
||||
|
||||
// disable direct downloads, we don't support them!
|
||||
if (response.getEntity().getContentType().getValue()
|
||||
.equals("application/octet-stream")) {
|
||||
// close connection
|
||||
response.getEntity().getContent().close();
|
||||
|
||||
final List<NameValuePair> pairs = new ArrayList<NameValuePair>();
|
||||
pairs.add(new BasicNameValuePair("do", "directdownloads"));
|
||||
pairs.add(new BasicNameValuePair("accountupdate", "1"));
|
||||
pairs.add(new BasicNameValuePair("set_ddl", "0"));
|
||||
|
||||
// execute update
|
||||
postAsString("http://www.megaupload.com/?c=account",
|
||||
new UrlEncodedFormEntity(pairs));
|
||||
|
||||
// execute and re-request download
|
||||
response = get(url.toString());
|
||||
}
|
||||
|
||||
final HTMLPage page = HttpClientUtils.toPage(response);
|
||||
|
||||
// try to find timer
|
||||
int timer = page.findIntegerInScript(DOWNLOAD_TIMER, 1);
|
||||
if (timer > 0 && configuration.respectWaitTime()) {
|
||||
timer(listener, timer * 1000);
|
||||
}
|
||||
final String downloadUrl = page
|
||||
.getLink(DOWNLOAD_DIRECT_LINK_PATTERN);
|
||||
if (downloadUrl != null && downloadUrl.length() > 0) {
|
||||
final HttpResponse downloadResponse = get(downloadUrl);
|
||||
if (downloadResponse.getStatusLine().getStatusCode() == HttpStatus.SC_FORBIDDEN
|
||||
|| downloadResponse.getStatusLine().getStatusCode() == HttpStatus.SC_SERVICE_UNAVAILABLE) {
|
||||
downloadResponse.getEntity().getContent().close();
|
||||
throw new DownloadLimitExceededException("HTTP "
|
||||
+ downloadResponse.getStatusLine().getStatusCode()
|
||||
+ " response");
|
||||
} else {
|
||||
final String filename = FilenameUtils.getName(downloadUrl);
|
||||
final long contentLength = getContentLength(downloadResponse);
|
||||
|
||||
return new InputStreamDownloadChannel(downloadResponse
|
||||
.getEntity().getContent(), contentLength, filename);
|
||||
}
|
||||
} else {
|
||||
throw new DownloadLinkNotFoundException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected class MegaUploadAuthenticator implements Authenticator {
|
||||
private final Credential credential;
|
||||
|
||||
public MegaUploadAuthenticator(Credential credential) {
|
||||
this.credential = credential;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void login() throws IOException {
|
||||
final MultipartEntity entity = new MultipartEntity();
|
||||
|
||||
entity.addPart("login", new StringBody("1"));
|
||||
entity.addPart("username", new StringBody(credential.getUsername()));
|
||||
entity.addPart("password", new StringBody(credential.getPassword()));
|
||||
|
||||
final HTMLPage page = postAsPage(
|
||||
"http://www.megaupload.com/?c=login", entity);
|
||||
String username = page.findInScript(LOGIN_USERNAME_PATTERN, 1);
|
||||
|
||||
if (username == null)
|
||||
throw new AuthenticationInvalidCredentialException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logout() throws IOException {
|
||||
final MultipartEntity entity = new MultipartEntity();
|
||||
entity.addPart("logout", new StringBody("1"));
|
||||
|
||||
postAsString("http://www.megaupload.com/?c=account", entity);
|
||||
// TODO check logout status
|
||||
}
|
||||
}
|
||||
|
||||
public static interface MegaUploadServiceConfiguration extends
|
||||
ServiceConfiguration {
|
||||
@ServiceConfigurationProperty(key = "megaupload.wait", defaultValue = "true")
|
||||
boolean respectWaitTime();
|
||||
|
||||
@ServiceConfigurationProperty(key = "megaupload.port", defaultValue = "80")
|
||||
int getPreferedDownloadPort();
|
||||
|
||||
@ServiceConfigurationProperty(key = "megaupload.description", defaultValue = "Uploaded by seedbox-httpchannel")
|
||||
String getDefaultUploadDescription();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.getClass().getSimpleName() + " " + getMajorVersion() + "."
|
||||
+ getMinorVersion();
|
||||
}
|
||||
}
|
||||
@@ -1,43 +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.util;
|
||||
|
||||
import org.apache.http.HttpRequest;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.ProtocolException;
|
||||
import org.apache.http.client.methods.HttpUriRequest;
|
||||
import org.apache.http.impl.client.DefaultRedirectStrategy;
|
||||
import org.apache.http.protocol.HttpContext;
|
||||
|
||||
/**
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*
|
||||
*/
|
||||
public class AlwaysRedirectStrategy extends DefaultRedirectStrategy {
|
||||
@Override
|
||||
public boolean isRedirected(HttpRequest request, HttpResponse response,
|
||||
HttpContext context) throws ProtocolException {
|
||||
return response.getFirstHeader("location") != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpUriRequest getRedirect(HttpRequest request,
|
||||
HttpResponse response, HttpContext context)
|
||||
throws ProtocolException {
|
||||
return super.getRedirect(request, response, context);
|
||||
}
|
||||
}
|
||||
@@ -1,51 +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.util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.nio.channels.WritableByteChannel;
|
||||
|
||||
/**
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*
|
||||
*/
|
||||
public class ChannelUtils {
|
||||
public static void copy(ReadableByteChannel in, WritableByteChannel out)
|
||||
throws IOException {
|
||||
// First, we need a buffer to hold blocks of copied bytes.
|
||||
ByteBuffer buffer = ByteBuffer.allocateDirect(32 * 1024);
|
||||
|
||||
// Now loop until no more bytes to read and the buffer is empty
|
||||
while (in.read(buffer) != -1 || buffer.position() > 0) {
|
||||
// The read() call leaves the buffer in "fill mode". To prepare
|
||||
// to write bytes from the bufferwe have to put it in "drain mode"
|
||||
// by flipping it: setting limit to position and position to zero
|
||||
buffer.flip();
|
||||
|
||||
// Now write some or all of the bytes out to the output channel
|
||||
out.write(buffer);
|
||||
|
||||
// Compact the buffer by discarding bytes that were written,
|
||||
// and shifting any remaining bytes. This method also
|
||||
// prepares the buffer for the next call to read() by setting the
|
||||
// position to the limit and the limit to the buffer capacity.
|
||||
buffer.compact();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,86 +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.util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpUriRequest;
|
||||
|
||||
import com.rogiel.httpchannel.util.htmlparser.HTMLPage;
|
||||
|
||||
public class HttpClientUtils {
|
||||
private static final ExecutorService threadPool = Executors
|
||||
.newCachedThreadPool();
|
||||
|
||||
public static HttpResponse get(HttpClient client, String url)
|
||||
throws IOException {
|
||||
return client.execute(new HttpGet(url));
|
||||
}
|
||||
|
||||
public static String getString(HttpClient client, String url)
|
||||
throws IOException {
|
||||
return toString(get(client, url));
|
||||
}
|
||||
|
||||
public static String execute(HttpClient client, HttpUriRequest request)
|
||||
throws IOException {
|
||||
return toString(client.execute(request));
|
||||
}
|
||||
|
||||
public static Future<String> executeAsync(final HttpClient client,
|
||||
final HttpUriRequest request) throws IOException {
|
||||
return threadPool.submit(new Callable<String>() {
|
||||
@Override
|
||||
public String call() throws Exception {
|
||||
return HttpClientUtils.toString(client.execute(request));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static Future<HttpResponse> executeAsyncHttpResponse(
|
||||
final HttpClient client, final HttpUriRequest request)
|
||||
throws IOException {
|
||||
return threadPool.submit(new Callable<HttpResponse>() {
|
||||
@Override
|
||||
public HttpResponse call() throws Exception {
|
||||
return client.execute(request);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static String toString(HttpResponse response) throws IOException {
|
||||
final InputStream in = response.getEntity().getContent();
|
||||
try {
|
||||
return IOUtils.toString(in);
|
||||
} finally {
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
|
||||
public static HTMLPage toPage(HttpResponse response) throws IOException {
|
||||
return HTMLPage.parse(toString(response));
|
||||
}
|
||||
}
|
||||
@@ -1,59 +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.util;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class PatternUtils {
|
||||
public static String find(Pattern pattern, String text) {
|
||||
return find(pattern, text, 0);
|
||||
}
|
||||
|
||||
public static int findInt(Pattern pattern, String text, int n) {
|
||||
String found = find(pattern, text, n);
|
||||
return (found != null ? Integer.parseInt(found) : 0);
|
||||
}
|
||||
|
||||
public static String find(Pattern pattern, String text, int n) {
|
||||
final Matcher matcher = pattern.matcher(text);
|
||||
if (matcher.find()) {
|
||||
return matcher.group(n);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String find(Pattern pattern, String text, int index, int n) {
|
||||
final Matcher matcher = pattern.matcher(text);
|
||||
int found = 0;
|
||||
while (matcher.find() && (++found) < index) {
|
||||
}
|
||||
return (found == 0 ? null : matcher.group(n));
|
||||
}
|
||||
|
||||
public static String match(Pattern pattern, String text) {
|
||||
return match(pattern, text, 0);
|
||||
}
|
||||
|
||||
public static String match(Pattern pattern, String text, int n) {
|
||||
final Matcher matcher = pattern.matcher(text);
|
||||
if (matcher.matches()) {
|
||||
return matcher.group(n);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,30 +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.util;
|
||||
|
||||
/**
|
||||
* @author Rogiel
|
||||
* @since 1.0
|
||||
*/
|
||||
public class ThreadUtils {
|
||||
public static void sleep(long time) {
|
||||
try {
|
||||
Thread.sleep(time);
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,245 +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.util.htmlparser;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.htmlparser.Node;
|
||||
import org.htmlparser.NodeFilter;
|
||||
import org.htmlparser.Parser;
|
||||
import org.htmlparser.Tag;
|
||||
import org.htmlparser.tags.FormTag;
|
||||
import org.htmlparser.tags.InputTag;
|
||||
import org.htmlparser.tags.LinkTag;
|
||||
import org.htmlparser.tags.ScriptTag;
|
||||
import org.htmlparser.util.NodeIterator;
|
||||
import org.htmlparser.util.NodeList;
|
||||
import org.htmlparser.util.ParserException;
|
||||
|
||||
/**
|
||||
* @author <a href="http://www.rogiel.com">Rogiel</a>
|
||||
*/
|
||||
public class HTMLPage {
|
||||
private final Parser parser;
|
||||
|
||||
private HTMLPage(Parser parser) {
|
||||
this.parser = parser;
|
||||
}
|
||||
|
||||
public String getLink(final Pattern pattern) {
|
||||
NodeList nodes;
|
||||
try {
|
||||
nodes = parser.extractAllNodesThatMatch(new NodeFilter() {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public boolean accept(Node node) {
|
||||
if (!(node instanceof LinkTag))
|
||||
return false;
|
||||
final LinkTag link = (LinkTag) node;
|
||||
return pattern.matcher(link.getLink()).matches();
|
||||
}
|
||||
});
|
||||
} catch (ParserException e) {
|
||||
return null;
|
||||
}
|
||||
if (nodes.size() >= 1)
|
||||
return ((LinkTag) nodes.elements().nextNode()).getLink();
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getFormAction(final Pattern pattern) {
|
||||
NodeList nodes;
|
||||
try {
|
||||
nodes = parser.extractAllNodesThatMatch(new NodeFilter() {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public boolean accept(Node node) {
|
||||
if (!(node instanceof FormTag))
|
||||
return false;
|
||||
final FormTag form = (FormTag) node;
|
||||
return pattern.matcher(form.getFormLocation()).matches();
|
||||
}
|
||||
});
|
||||
} catch (ParserException e) {
|
||||
return null;
|
||||
}
|
||||
if (nodes.size() >= 1)
|
||||
return ((FormTag) nodes.elements().nextNode()).getFormLocation();
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getInputValue(final String inputName) {
|
||||
NodeList nodes;
|
||||
try {
|
||||
nodes = parser.extractAllNodesThatMatch(new NodeFilter() {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public boolean accept(Node node) {
|
||||
if (!(node instanceof InputTag))
|
||||
return false;
|
||||
final InputTag input = (InputTag) node;
|
||||
if (!input.getAttribute("name").equals(inputName))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
});
|
||||
} catch (ParserException e) {
|
||||
return null;
|
||||
}
|
||||
if (nodes.size() >= 1)
|
||||
return ((InputTag) nodes.elements().nextNode())
|
||||
.getAttribute("value");
|
||||
return null;
|
||||
}
|
||||
|
||||
public int getIntegerInputValue(final String inputName) {
|
||||
return Integer.parseInt(getInputValue(inputName));
|
||||
}
|
||||
|
||||
public String getInputValue(final Pattern pattern) {
|
||||
NodeList nodes;
|
||||
try {
|
||||
nodes = parser.extractAllNodesThatMatch(new NodeFilter() {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public boolean accept(Node node) {
|
||||
if (!(node instanceof InputTag))
|
||||
return false;
|
||||
final InputTag input = (InputTag) node;
|
||||
if (input.getAttribute("value") == null)
|
||||
return false;
|
||||
if (!pattern.matcher(input.getAttribute("value")).matches())
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
});
|
||||
} catch (ParserException e) {
|
||||
return null;
|
||||
}
|
||||
if (nodes.size() >= 1)
|
||||
return ((InputTag) nodes.elements().nextNode())
|
||||
.getAttribute("value");
|
||||
return null;
|
||||
}
|
||||
|
||||
public Tag getTagByID(final String id) {
|
||||
NodeList nodes;
|
||||
try {
|
||||
nodes = parser.extractAllNodesThatMatch(new NodeFilter() {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public boolean accept(Node node) {
|
||||
if (!(node instanceof Tag))
|
||||
return false;
|
||||
if (((Tag) node).getAttribute("id") == null)
|
||||
return false;
|
||||
return ((Tag) node).getAttribute("id").equals(id);
|
||||
}
|
||||
});
|
||||
} catch (ParserException e) {
|
||||
return null;
|
||||
}
|
||||
if (nodes.size() >= 1)
|
||||
return ((Tag) nodes.elements().nextNode());
|
||||
return null;
|
||||
}
|
||||
|
||||
public Tag getTagByName(final String name) {
|
||||
NodeList nodes;
|
||||
try {
|
||||
nodes = parser.extractAllNodesThatMatch(new NodeFilter() {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public boolean accept(Node node) {
|
||||
if (!(node instanceof Tag))
|
||||
return false;
|
||||
return ((Tag) node).getAttribute("name").equals(name);
|
||||
}
|
||||
});
|
||||
} catch (ParserException e) {
|
||||
return null;
|
||||
}
|
||||
if (nodes.size() >= 1)
|
||||
return ((Tag) nodes.elements().nextNode());
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean contains(final String text) {
|
||||
try {
|
||||
for (NodeIterator e = parser.elements(); e.hasMoreNodes();) {
|
||||
if (e.nextNode().toPlainTextString().contains(text))
|
||||
return true;
|
||||
}
|
||||
} catch (ParserException e) {
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public String findInScript(final Pattern pattern, int n) {
|
||||
NodeList nodes;
|
||||
try {
|
||||
nodes = parser.extractAllNodesThatMatch(new NodeFilter() {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public boolean accept(Node node) {
|
||||
if (!(node instanceof ScriptTag))
|
||||
return false;
|
||||
final ScriptTag script = (ScriptTag) node;
|
||||
return pattern.matcher(script.getScriptCode()).find();
|
||||
}
|
||||
});
|
||||
} catch (ParserException e) {
|
||||
return null;
|
||||
}
|
||||
if (nodes.size() >= 1) {
|
||||
final ScriptTag script = (ScriptTag) nodes.elements().nextNode();
|
||||
final Matcher matcher = pattern.matcher(script.getScriptCode());
|
||||
if (matcher.find())
|
||||
return matcher.group(n);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public int findIntegerInScript(final Pattern pattern, int n) {
|
||||
return Integer.parseInt(findInScript(pattern, n));
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
try {
|
||||
for (NodeIterator i = parser.elements(); i.hasMoreNodes();) {
|
||||
builder.append(i.nextNode().toHtml(true));
|
||||
}
|
||||
} catch (ParserException e) {
|
||||
return null;
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
public static HTMLPage parse(String html) {
|
||||
return new HTMLPage(Parser.createParser(html, null));
|
||||
}
|
||||
}
|
||||
@@ -1,55 +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.util.transformer;
|
||||
|
||||
/**
|
||||
* @author Rogiel
|
||||
* @since 1.0
|
||||
*/
|
||||
public class TransformationException extends Exception {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public TransformationException() {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param message
|
||||
* the message
|
||||
*/
|
||||
public TransformationException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param cause
|
||||
* the cause
|
||||
*/
|
||||
public TransformationException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param message
|
||||
* the message
|
||||
* @param cause
|
||||
* the cause
|
||||
*/
|
||||
public TransformationException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,25 +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.util.transformer;
|
||||
|
||||
/**
|
||||
* @author rogiel
|
||||
*
|
||||
*/
|
||||
public interface Transformer<O> {
|
||||
O transform(String data) throws TransformationException;
|
||||
}
|
||||
@@ -1,48 +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.util.transformer;
|
||||
|
||||
import java.net.URL;
|
||||
|
||||
import com.rogiel.httpchannel.util.transformer.impl.BooleanTransformer;
|
||||
import com.rogiel.httpchannel.util.transformer.impl.IntegerTransformer;
|
||||
import com.rogiel.httpchannel.util.transformer.impl.LongTransformer;
|
||||
import com.rogiel.httpchannel.util.transformer.impl.StringTransformer;
|
||||
import com.rogiel.httpchannel.util.transformer.impl.URLTransformer;
|
||||
|
||||
|
||||
/**
|
||||
* @author Rogiel
|
||||
* @since 1.0
|
||||
*/
|
||||
public class TransformerFactory {
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> Transformer<T> getTransformer(Class<T> type) {
|
||||
if (String.class.isAssignableFrom(type)) {
|
||||
return (Transformer<T>) new StringTransformer();
|
||||
} else if (Boolean.class.isAssignableFrom(type) || type == Boolean.TYPE) {
|
||||
return (Transformer<T>) new BooleanTransformer();
|
||||
} else if (Integer.class.isAssignableFrom(type) || type == Integer.TYPE) {
|
||||
return (Transformer<T>) new IntegerTransformer();
|
||||
} else if (Long.class.isAssignableFrom(type) || type == Long.TYPE) {
|
||||
return (Transformer<T>) new LongTransformer();
|
||||
} else if (URL.class.isAssignableFrom(type)) {
|
||||
return (Transformer<T>) new URLTransformer();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,30 +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.util.transformer.impl;
|
||||
|
||||
import com.rogiel.httpchannel.util.transformer.Transformer;
|
||||
|
||||
/**
|
||||
* @author rogiel
|
||||
*
|
||||
*/
|
||||
public class BooleanTransformer implements Transformer<Boolean> {
|
||||
@Override
|
||||
public Boolean transform(String data) {
|
||||
return Boolean.parseBoolean(data);
|
||||
}
|
||||
}
|
||||
@@ -1,30 +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.util.transformer.impl;
|
||||
|
||||
import com.rogiel.httpchannel.util.transformer.Transformer;
|
||||
|
||||
/**
|
||||
* @author rogiel
|
||||
*
|
||||
*/
|
||||
public class IntegerTransformer implements Transformer<Integer> {
|
||||
@Override
|
||||
public Integer transform(String data) {
|
||||
return Integer.parseInt(data);
|
||||
}
|
||||
}
|
||||
@@ -1,30 +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.util.transformer.impl;
|
||||
|
||||
import com.rogiel.httpchannel.util.transformer.Transformer;
|
||||
|
||||
/**
|
||||
* @author rogiel
|
||||
*
|
||||
*/
|
||||
public class LongTransformer implements Transformer<Long> {
|
||||
@Override
|
||||
public Long transform(String data) {
|
||||
return Long.parseLong(data);
|
||||
}
|
||||
}
|
||||
@@ -1,30 +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.util.transformer.impl;
|
||||
|
||||
import com.rogiel.httpchannel.util.transformer.Transformer;
|
||||
|
||||
/**
|
||||
* @author rogiel
|
||||
*
|
||||
*/
|
||||
public class StringTransformer implements Transformer<String> {
|
||||
@Override
|
||||
public String transform(String data) {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
@@ -1,39 +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.util.transformer.impl;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
import com.rogiel.httpchannel.util.transformer.TransformationException;
|
||||
import com.rogiel.httpchannel.util.transformer.Transformer;
|
||||
|
||||
|
||||
/**
|
||||
* @author rogiel
|
||||
*
|
||||
*/
|
||||
public class URLTransformer implements Transformer<URL> {
|
||||
@Override
|
||||
public URL transform(String data) throws TransformationException {
|
||||
try {
|
||||
return new URL(data);
|
||||
} catch (MalformedURLException e) {
|
||||
throw new TransformationException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,186 +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.impl;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.SeekableByteChannel;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Properties;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.rogiel.httpchannel.service.AuthenticationService;
|
||||
import com.rogiel.httpchannel.service.Credential;
|
||||
import com.rogiel.httpchannel.service.DownloadChannel;
|
||||
import com.rogiel.httpchannel.service.DownloadService;
|
||||
import com.rogiel.httpchannel.service.Service;
|
||||
import com.rogiel.httpchannel.service.ServiceHelper;
|
||||
import com.rogiel.httpchannel.service.UploadChannel;
|
||||
import com.rogiel.httpchannel.service.UploadService;
|
||||
import com.rogiel.httpchannel.service.UploaderCapability;
|
||||
import com.rogiel.httpchannel.service.config.ServiceConfigurationHelper;
|
||||
import com.rogiel.httpchannel.service.exception.AuthenticationInvalidCredentialException;
|
||||
import com.rogiel.httpchannel.service.impl.HotFileService.HotFileServiceConfiguration;
|
||||
import com.rogiel.httpchannel.util.ChannelUtils;
|
||||
|
||||
public class HotFileServiceTest {
|
||||
private Service service;
|
||||
private ServiceHelper helper;
|
||||
|
||||
/**
|
||||
* See <b>src/test/resources/config/hotfile.properties</b>
|
||||
* <p>
|
||||
* Key: username
|
||||
*/
|
||||
private String VALID_USERNAME;
|
||||
/**
|
||||
* See <b>src/test/resources/config/hotfile.properties</b>
|
||||
* <p>
|
||||
* Key: password
|
||||
*/
|
||||
private String VALID_PASSWORD;
|
||||
|
||||
private static final String INVALID_USERNAME = "invalid";
|
||||
private static final String INVALID_PASSWORD = "abc";
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
// MegaUploadServiceConfiguration.class;
|
||||
service = new HotFileService(
|
||||
ServiceConfigurationHelper
|
||||
.defaultConfiguration(HotFileServiceConfiguration.class));
|
||||
helper = new ServiceHelper(service);
|
||||
|
||||
final Properties properties = new Properties();
|
||||
properties.load(new FileInputStream(
|
||||
"src/test/resources/config/hotfile.properties"));
|
||||
VALID_USERNAME = properties.getProperty("username");
|
||||
VALID_PASSWORD = properties.getProperty("password");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testServiceId() {
|
||||
System.out.println("Service: " + service.toString());
|
||||
assertEquals("hotfile", service.getID());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidAuthenticator() throws IOException {
|
||||
((AuthenticationService) service).getAuthenticator(
|
||||
new Credential(VALID_USERNAME, VALID_PASSWORD)).login();
|
||||
}
|
||||
|
||||
@Test(expected = AuthenticationInvalidCredentialException.class)
|
||||
public void testInvalidAuthenticator() throws IOException {
|
||||
((AuthenticationService) service).getAuthenticator(
|
||||
new Credential(INVALID_USERNAME, INVALID_PASSWORD)).login();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNonLoguedInUploader() throws IOException,
|
||||
URISyntaxException {
|
||||
assertTrue(
|
||||
"This service does not have the capability UploadCapability.FREE_UPLOAD",
|
||||
((UploadService) service).getUploadCapabilities().has(
|
||||
UploaderCapability.NON_PREMIUM_ACCOUNT_UPLOAD));
|
||||
|
||||
final Path path = Paths.get("src/test/resources/upload-test-file.txt");
|
||||
final UploadChannel channel = helper.upload(path,
|
||||
"httpchannel test upload");
|
||||
final SeekableByteChannel inChannel = Files.newByteChannel(path);
|
||||
|
||||
try {
|
||||
ChannelUtils.copy(inChannel, channel);
|
||||
} finally {
|
||||
inChannel.close();
|
||||
channel.close();
|
||||
}
|
||||
|
||||
System.out.println(channel.getDownloadLink());
|
||||
Assert.assertNotNull(channel.getDownloadLink());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoguedInUploader() throws IOException {
|
||||
assertTrue(
|
||||
"This service does not have the capability UploadCapability.PREMIUM_UPLOAD",
|
||||
((UploadService) service).getUploadCapabilities().has(
|
||||
UploaderCapability.PREMIUM_ACCOUNT_UPLOAD));
|
||||
|
||||
((AuthenticationService) service).getAuthenticator(
|
||||
new Credential(VALID_USERNAME, VALID_PASSWORD)).login();
|
||||
|
||||
final Path path = Paths.get("src/test/resources/upload-test-file.txt");
|
||||
final UploadChannel channel = helper.upload(path,
|
||||
"httpchannel test upload");
|
||||
final SeekableByteChannel inChannel = Files.newByteChannel(path);
|
||||
|
||||
try {
|
||||
ChannelUtils.copy(inChannel, channel);
|
||||
} finally {
|
||||
inChannel.close();
|
||||
channel.close();
|
||||
}
|
||||
|
||||
System.out.println(channel.getDownloadLink());
|
||||
Assert.assertNotNull(channel.getDownloadLink());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDownloader() throws IOException, MalformedURLException {
|
||||
final DownloadChannel channel = ((DownloadService) service)
|
||||
.getDownloader(
|
||||
new URL(
|
||||
"http://hotfile.com/dl/129251605/9b4faf2/simulado_2010_1_res_all.zip.html"))
|
||||
.download(null);
|
||||
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
||||
IOUtils.copy(Channels.newInputStream(channel), bout);
|
||||
System.out.println(bout.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoggedInDownloader() throws IOException,
|
||||
MalformedURLException {
|
||||
((AuthenticationService) service).getAuthenticator(
|
||||
new Credential(VALID_USERNAME, VALID_PASSWORD)).login();
|
||||
|
||||
final DownloadChannel channel = ((DownloadService) service)
|
||||
.getDownloader(
|
||||
new URL(
|
||||
"http://hotfile.com/dl/129251605/9b4faf2/simulado_2010_1_res_all.zip.html"))
|
||||
.download(null);
|
||||
|
||||
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
||||
IOUtils.copy(Channels.newInputStream(channel), bout);
|
||||
System.out.println(bout.size());
|
||||
}
|
||||
}
|
||||
@@ -1,213 +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.impl;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.SeekableByteChannel;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Properties;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.rogiel.httpchannel.service.AuthenticationService;
|
||||
import com.rogiel.httpchannel.service.Credential;
|
||||
import com.rogiel.httpchannel.service.DownloadChannel;
|
||||
import com.rogiel.httpchannel.service.DownloadListener;
|
||||
import com.rogiel.httpchannel.service.DownloadService;
|
||||
import com.rogiel.httpchannel.service.Service;
|
||||
import com.rogiel.httpchannel.service.ServiceHelper;
|
||||
import com.rogiel.httpchannel.service.UploadChannel;
|
||||
import com.rogiel.httpchannel.service.UploadService;
|
||||
import com.rogiel.httpchannel.service.UploaderCapability;
|
||||
import com.rogiel.httpchannel.service.config.ServiceConfigurationHelper;
|
||||
import com.rogiel.httpchannel.service.exception.AuthenticationInvalidCredentialException;
|
||||
import com.rogiel.httpchannel.service.impl.MegaUploadService.MegaUploadServiceConfiguration;
|
||||
import com.rogiel.httpchannel.util.ChannelUtils;
|
||||
|
||||
public class MegaUploadServiceTest {
|
||||
private Service service;
|
||||
private ServiceHelper helper;
|
||||
|
||||
/**
|
||||
* See <b>src/test/resources/config/megaupload.properties</b>
|
||||
* <p>
|
||||
* Key: username
|
||||
*/
|
||||
private String VALID_USERNAME;
|
||||
/**
|
||||
* See <b>src/test/resources/config/megaupload.properties</b>
|
||||
* <p>
|
||||
* Key: password
|
||||
*/
|
||||
private String VALID_PASSWORD;
|
||||
|
||||
private static final String INVALID_USERNAME = "invalid";
|
||||
private static final String INVALID_PASSWORD = "abc";
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
// MegaUploadServiceConfiguration.class;
|
||||
service = new MegaUploadService(
|
||||
ServiceConfigurationHelper
|
||||
.defaultConfiguration(MegaUploadServiceConfiguration.class));
|
||||
helper = new ServiceHelper(service);
|
||||
|
||||
final Properties properties = new Properties();
|
||||
properties.load(new FileInputStream(
|
||||
"src/test/resources/config/megaupload.properties"));
|
||||
VALID_USERNAME = properties.getProperty("username");
|
||||
VALID_PASSWORD = properties.getProperty("password");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testServiceId() {
|
||||
System.out.println("Service: " + service.toString());
|
||||
assertEquals("megaupload", service.getID());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidAuthenticator() throws IOException {
|
||||
((AuthenticationService) service).getAuthenticator(
|
||||
new Credential(VALID_USERNAME, VALID_PASSWORD)).login();
|
||||
}
|
||||
|
||||
@Test(expected = AuthenticationInvalidCredentialException.class)
|
||||
public void testInvalidAuthenticator() throws IOException {
|
||||
((AuthenticationService) service).getAuthenticator(
|
||||
new Credential(INVALID_USERNAME, INVALID_PASSWORD)).login();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNonLoguedInUploader() throws IOException {
|
||||
assertTrue(
|
||||
"This service does not have the capability UploadCapability.FREE_UPLOAD",
|
||||
((UploadService) service).getUploadCapabilities().has(
|
||||
UploaderCapability.NON_PREMIUM_ACCOUNT_UPLOAD));
|
||||
final Path path = Paths.get("src/test/resources/upload-test-file.txt");
|
||||
final UploadChannel channel = helper.upload(path,
|
||||
"httpchannel test upload");
|
||||
final SeekableByteChannel inChannel = Files.newByteChannel(path);
|
||||
|
||||
try {
|
||||
ChannelUtils.copy(inChannel, channel);
|
||||
} finally {
|
||||
inChannel.close();
|
||||
channel.close();
|
||||
}
|
||||
|
||||
System.out.println(channel.getDownloadLink());
|
||||
Assert.assertNotNull(channel.getDownloadLink());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoguedInUploader() throws IOException {
|
||||
assertTrue(
|
||||
"This service does not have the capability UploadCapability.PREMIUM_UPLOAD",
|
||||
((UploadService) service).getUploadCapabilities().has(
|
||||
UploaderCapability.PREMIUM_ACCOUNT_UPLOAD));
|
||||
|
||||
((AuthenticationService) service).getAuthenticator(
|
||||
new Credential(VALID_USERNAME, VALID_PASSWORD)).login();
|
||||
|
||||
final Path path = Paths.get("src/test/resources/upload-test-file.txt");
|
||||
final UploadChannel channel = helper.upload(path,
|
||||
"httpchannel test upload");
|
||||
final SeekableByteChannel inChannel = Files.newByteChannel(path);
|
||||
|
||||
try {
|
||||
ChannelUtils.copy(inChannel, channel);
|
||||
} finally {
|
||||
inChannel.close();
|
||||
channel.close();
|
||||
}
|
||||
|
||||
System.out.println(channel.getDownloadLink());
|
||||
Assert.assertNotNull(channel.getDownloadLink());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFreeDownloader() throws IOException, MalformedURLException {
|
||||
final DownloadChannel channel = ((DownloadService) service)
|
||||
.getDownloader(new URL("http://www.megaupload.com/?d=CVQKJ1KM"))
|
||||
.download(new DownloadListener() {
|
||||
@Override
|
||||
public boolean timer(long time) {
|
||||
System.out.println("Waiting " + time);
|
||||
// if (reason == TimerWaitReason.DOWNLOAD_TIMER)
|
||||
// return true;
|
||||
return true;
|
||||
}
|
||||
});
|
||||
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
||||
IOUtils.copy(Channels.newInputStream(channel), bout);
|
||||
System.out.println(bout.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPremiumDownloader() throws IOException,
|
||||
MalformedURLException {
|
||||
((AuthenticationService) service).getAuthenticator(
|
||||
new Credential(VALID_USERNAME, VALID_PASSWORD)).login();
|
||||
|
||||
final DownloadChannel channel = ((DownloadService) service)
|
||||
.getDownloader(new URL("http://www.megaupload.com/?d=CVQKJ1KM"))
|
||||
.download(new DownloadListener() {
|
||||
@Override
|
||||
public boolean timer(long time) {
|
||||
System.out.println("Waiting " + time);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
||||
IOUtils.copy(Channels.newInputStream(channel), bout);
|
||||
System.out.println(bout.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoWaitDownloader() throws IOException,
|
||||
MalformedURLException {
|
||||
service = new MegaUploadService(ServiceConfigurationHelper.file(
|
||||
MegaUploadServiceConfiguration.class, new File(
|
||||
"src/test/resources/megaupload-nowait.properties")));
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
final DownloadChannel channel = ((DownloadService) service)
|
||||
.getDownloader(new URL("http://www.megaupload.com/?d=CVQKJ1KM"))
|
||||
.download(new DownloadListener() {
|
||||
@Override
|
||||
public boolean timer(long time) {
|
||||
System.out.println("Waiting " + time);
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
1
src/test/resources/.gitignore
vendored
1
src/test/resources/.gitignore
vendored
@@ -1 +0,0 @@
|
||||
/config
|
||||
@@ -1,9 +0,0 @@
|
||||
log4j.rootLogger=INFO, stdout
|
||||
|
||||
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.stdout.layout.ConversionPattern=%5p [%c] %m%n
|
||||
|
||||
log4j.logger.org.apache.http.impl.conn=DEBUG
|
||||
log4j.logger.org.apache.http.impl.client=DEBUG
|
||||
log4j.logger.org.apache.http.client=DEBUG
|
||||
@@ -1,3 +0,0 @@
|
||||
This is a simple upload test file.
|
||||
|
||||
This is for testing purposes only.
|
||||
Reference in New Issue
Block a user