Skip to content

Commit

Permalink
Implements wupload.com service
Browse files Browse the repository at this point in the history
  • Loading branch information
Rogiel committed Jan 20, 2012
1 parent 7f558b3 commit f190e80
Show file tree
Hide file tree
Showing 16 changed files with 555 additions and 12 deletions.
Expand Up @@ -33,7 +33,7 @@ public void setUp() throws IOException {
}

@Test
public void test() throws IOException {
public void testUpload() throws IOException {
AuthenticationServices.authenticator(service,
properties.getProperty("4shared.username"),
properties.getProperty("4shared.password")).login();
Expand Down
Expand Up @@ -23,6 +23,7 @@

import javax.xml.bind.JAXB;

import com.rogiel.httpchannel.http.HttpContext;
import com.rogiel.httpchannel.service.filesonic.xml.FSAPI;
import com.rogiel.httpchannel.service.filesonic.xml.FSGetUploadURL;
import com.rogiel.httpchannel.service.filesonic.xml.FSUpload;
Expand All @@ -32,12 +33,14 @@
*/
public class FileSonicAPI {
private static final String BASE_URI = "http://api.filesonic.com/";

private final HttpContext ctx;

private String email;
private String password;

public Object getInfo(int id) {
return id;
public FileSonicAPI(HttpContext ctx) {
this.ctx = ctx;
}

public URI getUploadURI() throws IOException {
Expand Down Expand Up @@ -66,6 +69,6 @@ private <T extends FSAPI> T execute(Class<T> type, String requestURI)
throws IOException {
final URI uri = URI.create(BASE_URI + requestURI + "&u=" + email
+ "&p=" + password + "&format=xml");
return JAXB.unmarshal(uri.toURL().openStream(), type);
return JAXB.unmarshal(ctx.get(uri).asStream(), type);
}
}
Expand Up @@ -56,17 +56,17 @@ public class FileSonicService extends AbstractHttpService implements Service,
/**
* This service ID
*/
public static final ServiceID SERVICE_ID = ServiceID.create("megaupload");
public static final ServiceID SERVICE_ID = ServiceID.create("filesonic");

/**
* The download URI pattern
*/
private static final Pattern DOWNLOAD_URI_PATTERN = Pattern
.compile("http://www.filesonic.com/file/[0-9A-z]*");
.compile("http://www\\.filesonic\\.com/file/[0-9A-z]*");
/**
* The FileSonic API
*/
private final FileSonicAPI api = new FileSonicAPI();
private final FileSonicAPI api = new FileSonicAPI(http);

@Override
public ServiceID getServiceID() {
Expand Down Expand Up @@ -201,9 +201,7 @@ public void login() throws IOException {

@Override
public void logout() throws IOException {
post("http://www.megaupload.com/?c=account").parameter("logout",
true).request();
// TODO check logout status
api.logout();
}
}

Expand Down
Expand Up @@ -35,7 +35,7 @@ public class FSGetUploadURL extends FSResponse {

@XmlAccessorType(XmlAccessType.NONE)
public static class FSGetUploadURLResponse {
@XmlElement(name = "uri")
@XmlElement(name = "url")
private String uploadURI;
@XmlElement(name = "max-filesize")
private long maxFilesize;
Expand Down
13 changes: 13 additions & 0 deletions httpchannel-service/httpchannel-service-wupload/pom.xml
@@ -0,0 +1,13 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>httpchannel-service</artifactId>
<groupId>com.rogiel.httpchannel</groupId>
<version>1.0.1-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<artifactId>httpchannel-service-wupload</artifactId>
<groupId>com.rogiel.httpchannel.services</groupId>
<name>HttpChannel/Service/WUpload</name>
<description>Provides download and upload access to wupload.com</description>
</project>
@@ -0,0 +1,74 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.rogiel.httpchannel.service.wupload;

import java.io.IOException;
import java.net.URI;

import javax.xml.bind.JAXB;

import com.rogiel.httpchannel.http.HttpContext;
import com.rogiel.httpchannel.service.wupload.xml.FSAPI;
import com.rogiel.httpchannel.service.wupload.xml.FSGetUploadURL;
import com.rogiel.httpchannel.service.wupload.xml.FSUpload;

/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class WUploadAPI {
private static final String BASE_URI = "http://api.wupload.com/";

private final HttpContext ctx;

private String email;
private String password;

public WUploadAPI(HttpContext ctx) {
this.ctx = ctx;
}

public URI getUploadURI() throws IOException {
return URI.create((((FSGetUploadURL) execute(FSUpload.class,
"upload?method=getUploadUrl").getResponse()).getResponse()
.getUploadURI()));
}

public long getMaxFilesize() throws IOException {
return ((FSGetUploadURL) execute(FSUpload.class,
"upload?method=getUploadUrl").getResponse()).getResponse()
.getMaxFilesize();
}

public void login(String email, String password) {
this.email = email;
this.password = password;
}

public void logout() {
this.email = null;
this.password = null;
}

private <T extends FSAPI> T execute(Class<T> type, String requestURI)
throws IOException {
final URI uri = URI.create(BASE_URI + requestURI + "&u=" + email
+ "&p=" + password + "&format=xml");
return JAXB.unmarshal(ctx.get(uri).asStream(), type);
}
}
@@ -0,0 +1,211 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.rogiel.httpchannel.service.wupload;

import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.regex.Pattern;

import com.rogiel.httpchannel.service.AbstractAuthenticator;
import com.rogiel.httpchannel.service.AbstractHttpService;
import com.rogiel.httpchannel.service.AbstractUploader;
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.Service;
import com.rogiel.httpchannel.service.ServiceID;
import com.rogiel.httpchannel.service.ServiceMode;
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.LinkedUploadChannel;
import com.rogiel.httpchannel.service.channel.LinkedUploadChannel.LinkedUploadChannelCloseCallback;
import com.rogiel.httpchannel.service.config.NullAuthenticatorConfiguration;
import com.rogiel.httpchannel.service.config.NullUploaderConfiguration;
import com.rogiel.httpchannel.util.PatternUtils;

/**
* This service handles login, upload and download to WUpload.com.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
* @since 1.0
*/
public class WUploadService extends AbstractHttpService implements Service,
UploadService<NullUploaderConfiguration>,
AuthenticationService<NullAuthenticatorConfiguration> {
/**
* This service ID
*/
public static final ServiceID SERVICE_ID = ServiceID.create("wupload");

/**
* The download URI pattern
*/
private static final Pattern DOWNLOAD_URI_PATTERN = Pattern
.compile("http://www\\.wupload\\.com/file/[0-9]*(/.*)?");
/**
* The FileSonic API
*/
private final WUploadAPI api = new WUploadAPI(http);

@Override
public ServiceID getServiceID() {
return SERVICE_ID;
}

@Override
public int getMajorVersion() {
return 1;
}

@Override
public int getMinorVersion() {
return 0;
}

@Override
public CapabilityMatrix<ServiceMode> getPossibleServiceModes() {
return new CapabilityMatrix<ServiceMode>(ServiceMode.UNAUTHENTICATED,
ServiceMode.NON_PREMIUM, ServiceMode.PREMIUM);
}

@Override
public Uploader<NullUploaderConfiguration> getUploader(String filename,
long filesize, NullUploaderConfiguration configuration) {
return new UploaderImpl(filename, filesize, configuration);
}

@Override
public Uploader<NullUploaderConfiguration> getUploader(String filename,
long filesize) {
return getUploader(filename, filesize, newUploaderConfiguration());
}

@Override
public NullUploaderConfiguration newUploaderConfiguration() {
return NullUploaderConfiguration.SHARED_INSTANCE;
}

@Override
public long getMaximumFilesize() {
try {
return api.getMaxFilesize();
} catch (IOException e) {
return -1;
}
}

@Override
public String[] getSupportedExtensions() {
return null;
}

@Override
public CapabilityMatrix<UploaderCapability> getUploadCapabilities() {
return new CapabilityMatrix<UploaderCapability>(
UploaderCapability.NON_PREMIUM_ACCOUNT_UPLOAD,
UploaderCapability.PREMIUM_ACCOUNT_UPLOAD);
}

@Override
public Authenticator<NullAuthenticatorConfiguration> getAuthenticator(
Credential credential, NullAuthenticatorConfiguration configuration) {
return new AuthenticatorImpl(credential, configuration);
}

@Override
public Authenticator<NullAuthenticatorConfiguration> getAuthenticator(
Credential credential) {
return getAuthenticator(credential, newAuthenticatorConfiguration());
}

@Override
public NullAuthenticatorConfiguration newAuthenticatorConfiguration() {
return NullAuthenticatorConfiguration.SHARED_INSTANCE;
}

@Override
public CapabilityMatrix<AuthenticatorCapability> getAuthenticationCapability() {
return new CapabilityMatrix<AuthenticatorCapability>();
}

protected class UploaderImpl extends
AbstractUploader<NullUploaderConfiguration> implements
Uploader<NullUploaderConfiguration>,
LinkedUploadChannelCloseCallback {
private Future<String> uploadFuture;

public UploaderImpl(String filename, long filesize,
NullUploaderConfiguration configuration) {
super(WUploadService.this, filename, filesize, configuration);
}

@Override
public UploadChannel openChannel() throws IOException {
logger.debug("Starting upload to wupload.com");
final LinkedUploadChannel channel = createLinkedChannel(this);
uploadFuture = multipartPost(api.getUploadURI().toString())
.parameter("files[]", channel).asStringAsync();
return waitChannelLink(channel, uploadFuture);
}

@Override
public String finish() throws IOException {
try {
return PatternUtils.find(DOWNLOAD_URI_PATTERN,
uploadFuture.get());
} catch (InterruptedException e) {
return null;
} catch (ExecutionException e) {
throw (IOException) e.getCause();
}
}
}

protected class AuthenticatorImpl extends
AbstractAuthenticator<NullAuthenticatorConfiguration> implements
Authenticator<NullAuthenticatorConfiguration> {
public AuthenticatorImpl(Credential credential,
NullAuthenticatorConfiguration configuration) {
super(credential, configuration);
}

@Override
public void login() throws IOException {
logger.debug("Logging to wupload.com");
api.login(credential.getUsername(), credential.getPassword());
serviceMode = ServiceMode.NON_PREMIUM;
}

@Override
public void logout() throws IOException {
api.logout();
}
}

@Override
public String toString() {
return this.getClass().getSimpleName() + " " + getMajorVersion() + "."
+ getMinorVersion();
}
}

0 comments on commit f190e80

Please sign in to comment.