Created
July 19, 2011 21:44
-
-
Save IndiceeCoder/1093806 to your computer and use it in GitHub Desktop.
Java Example - Avatar Uploader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2010- Indicee Inc. All Rights Reserved. | |
* | |
* Indicee, Inc. | |
* #2200-1050 West Pender Street | |
* Vancouver, BC, V6E 3S7, Canada | |
* 1-888-681 3840 | |
* | |
* IMPORTANT: This Indicee software is supplied to you by Indicee, Inc. ("Indicee") in | |
* consideration of your agreement to the following terms, and your use, installation, | |
* modification or redistribution of this Indicee software constitutes acceptance of these | |
* terms. If you do not agree with these terms, please do not use, install, modify or | |
* redistribute this Indicee software. | |
* | |
* In consideration of your agreement to abide by the following terms, and subject to these | |
* terms, Indicee grants you a personal, non-exclusive license, under Indicee's copyrights in | |
* this original Indicee software (the "Indicee Software"), to use, reproduce, modify and | |
* redistribute the Indicee Software, with or without modifications, in source and/or binary | |
* forms; provided that if you redistribute the Indicee Software in its entirety and without | |
* modifications, you must retain this notice and the following text and disclaimers in all | |
* such redistributions of the Indicee Software. Neither the name, trademarks, service marks | |
* or logos of Indicee, Inc. may be used to endorse or promote products derived from | |
* the Indicee Software without specific prior written permission from Indicee. Except as expressly | |
* stated in this notice, no other rights or licenses, express or implied, are granted by Indicee | |
* herein, including but not limited to any patent rights that may be infringed by your | |
* derivative works or by other works in which the Indicee Software may be incorporated. | |
* | |
* The Indicee Software is provided by Indicee on an "AS IS" basis. INDICEE MAKES NO WARRANTIES, | |
* EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, | |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE INDICEE SOFTWARE OR ITS | |
* USE AND OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. | |
* | |
* IN NO EVENT SHALL INDICEE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL | |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | |
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, | |
* REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE INDICEE SOFTWARE, HOWEVER CAUSED AND | |
* WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR | |
* OTHERWISE, EVEN IF INDICEE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
*/ | |
package com.indicee.rest.example; | |
import java.io.File; | |
import java.io.IOException; | |
import org.apache.http.HttpEntity; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.auth.AuthScope; | |
import org.apache.http.auth.UsernamePasswordCredentials; | |
import org.apache.http.client.ClientProtocolException; | |
import org.apache.http.client.methods.HttpPost; | |
import org.apache.http.conn.ssl.SSLSocketFactory; | |
import org.apache.http.entity.mime.MultipartEntity; | |
import org.apache.http.entity.mime.content.ContentBody; | |
import org.apache.http.entity.mime.content.FileBody; | |
import org.apache.http.impl.auth.BasicScheme; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.http.protocol.BasicHttpContext; | |
import org.apache.http.util.EntityUtils; | |
/** | |
* An example to use the indicee restful api to upload user's avatar Usage: java | |
* com.indicee.rest.example.AvatarUploader host username password file | |
*/ | |
public class AvatarUploader { | |
public static void main(String[] args) { | |
if (args.length != 4) { | |
System.out.println("Usage: java com.indicee.rest.example.AvatarUploader host username password file"); | |
System.exit(0); | |
} | |
String filename = args[3]; | |
File file = new File(filename); | |
if (!file.exists()) { | |
System.out.println("The file " + filename + " doesn't exist"); | |
System.exit(0); | |
} | |
if (file.isDirectory()) { | |
System.out.println("The file " + filename + " isn't a file"); | |
System.exit(0); | |
} | |
AvatarUploader uploader = new AvatarUploader(args[0], args[1], args[2]); | |
try { | |
HttpResponse httpResponse = uploader.upload(file); | |
// check the status of the response and out put the result | |
int status = httpResponse.getStatusLine().getStatusCode(); | |
HttpEntity entity = httpResponse.getEntity(); | |
if (status == 200) { | |
System.out.println("Request is processed."); | |
System.out.println("Return Result:" | |
+ EntityUtils.toString(entity)); | |
} else { | |
System.out.println("Request is failed. Status code:" + status); | |
System.out.println("Response Result:" | |
+ EntityUtils.toString(entity)); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
private final String username; // username | |
private final String password; // password | |
private final String host; // the host of api server | |
public AvatarUploader(String host, String username, String password) { | |
this.username = username; | |
this.password = password; | |
this.host = host; | |
} | |
//The usage of the api to upload the user's avatar. | |
private HttpResponse upload(File file) throws ClientProtocolException, | |
IOException { | |
String url = String.format("https://%s/api/user/avatar/upload.xml", | |
host); | |
HttpPost httpPost = new HttpPost(url); | |
//build post data. File upload needs multipart form data. | |
// see http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2 | |
MultipartEntity mentity = new MultipartEntity(); | |
//ensure the set the mime type to image/xxx, otherwise the file won't be accepted. | |
ContentBody fbody = new FileBody(file, "image/jpeg"); | |
mentity.addPart("file", fbody); | |
httpPost.setEntity(mentity); | |
// init a http client | |
DefaultHttpClient httpClient = new DefaultHttpClient(); | |
SSLSocketFactory sf = (SSLSocketFactory) httpClient.getConnectionManager() | |
.getSchemeRegistry() | |
.getScheme("https") | |
.getSocketFactory(); | |
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); | |
// set the http basic auth | |
httpClient.getCredentialsProvider() | |
.setCredentials( | |
new AuthScope(host, 443), // https port is 443 | |
new UsernamePasswordCredentials(username, | |
password)); | |
BasicHttpContext localContext = new BasicHttpContext(); | |
localContext.setAttribute("preemptive-auth", new BasicScheme()); | |
//send the data to get the response | |
return httpClient.execute(httpPost, localContext); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment