Java tutorial
/* * Copyright 2007 Yusuke Yamamoto * * Licensed 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 cmu.edu.homework.mediaUpload; import android.util.Log; import twitter4j.*; import twitter4j.auth.OAuthAuthorization; import twitter4j.conf.Configuration; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.util.*; /** * @author Rmy Rakic - remy.rakic at gmail.com * @author Takao Nakaguchi - takao.nakaguchi at gmail.com * @author withgod - noname at withgod.jp * @since Twitter4J 2.1.8 */ abstract class AbstractVideoUploadImpl implements VideoUpload { static final String TWITTER_VERIFY_CREDENTIALS_JSON_V1_1 = "https://api.twitter.com/1.1/account/verify_credentials.json"; static final String TAG = "****"; private HttpClient client; private Configuration conf = null; protected String apiKey = null; OAuthAuthorization oauth = null; String uploadUrl = null; HttpParameter[] postParameter = null; private HttpParameter[] appendParameter = null; String video = null; HttpParameter message = null; final Map<String, String> headers = new HashMap<String, String>(); HttpResponse httpResponse = null; private static final Logger logger = Logger.getLogger(AbstractVideoUploadImpl.class); AbstractVideoUploadImpl(Configuration conf, OAuthAuthorization oauth) { this.oauth = oauth; this.conf = conf; try { } catch (Exception e) { e.printStackTrace(); } client = HttpClientFactory.getInstance(conf.getHttpClientConfiguration()); } AbstractVideoUploadImpl(Configuration conf, String apiKey, OAuthAuthorization oauth) { this(conf, oauth); this.apiKey = apiKey; } @Override public String upload(String videoFileName, InputStream videoBody) throws TwitterException { // this.video = new HttpParameter("media", videoFileName, videoBody); return upload(); } @Override public String upload(String videoFileName, InputStream videoBody, String message) throws TwitterException { // this.video = new HttpParameter("media", videoFileName, videoBody); this.message = new HttpParameter("message", message); return upload(); } @Override public String upload(File file, String message) throws TwitterException { this.video = file.getAbsolutePath(); this.message = new HttpParameter("message", message); String media_id = upload(); long[] media_ids = new long[1]; media_ids[0] = Long.parseLong(media_id); return media_id; } private String post(String message, long[] media_ids) { String postUrl = "https://api.twitter.com/1.1/statuses/update.json"; HttpParameter[] params = new HttpParameter[2]; params[0] = new HttpParameter("status", message); params[1] = new HttpParameter("media_ids", String.valueOf(media_ids)); postParameter = params; headers.putAll(client.getRequestHeaders()); String authheader = generateVerifyCredentialsAuthorizationHeader("POST"); headers.put("Authorization", authheader); HttpRequest req2 = new HttpRequest(RequestMethod.POST, uploadUrl, postParameter, null, headers); try { httpResponse = client.request(req2, null); if (httpResponse.getStatusCode() != 202) { Log.e("Error from Twitter", "get error in INIT:" + httpResponse.getStatusCode() + ", details:" + httpResponse.toString()); } return "202"; } catch (Exception e) { e.printStackTrace(); } return "Error"; } @Override public String upload(File file) throws TwitterException { this.video = file.getAbsolutePath(); return upload(); } private String upload() throws TwitterException { if (conf.getMediaProviderParameters() != null && this.appendParameter.length > 0) { this.postParameter = appendHttpParameters(this.postParameter, this.appendParameter); } String media_id_string_init = init(); String media_id_string_append = append(media_id_string_init); String media_id_string_finalized = finalize(media_id_string_init); return media_id_string_init; } private String init() { uploadUrl = "https://upload.twitter.com/1.1/media/upload.json"; HttpParameter[] params = new HttpParameter[3]; params[0] = new HttpParameter("command", "INIT"); long total_bytes = new File(video).length(); params[1] = new HttpParameter("total_bytes", total_bytes); params[2] = new HttpParameter("media_type", "video/mp4"); if (this.postParameter != null && this.postParameter.length > 0) { this.appendHttpParameters(params, this.postParameter); } else { this.postParameter = params; } headers.putAll(client.getRequestHeaders()); HttpRequest req = new HttpRequest(RequestMethod.POST, uploadUrl, postParameter, null, headers); String authheader = oauth.getAuthorizationHeader(req); headers.put("Authorization", authheader); HttpRequest req2 = new HttpRequest(RequestMethod.POST, uploadUrl, postParameter, null, headers); try { httpResponse = client.request(req2, null); if (httpResponse.getStatusCode() != 202) { Log.e("Error from Twitter", "get error in INIT:" + httpResponse.getStatusCode() + ", details:" + httpResponse.toString()); } return httpResponse.asJSONObject().getString("media_id_string"); } catch (Exception e) { e.printStackTrace(); } return null; } private String append(String media_id_string) { HttpParameter[] params = new HttpParameter[4]; params[0] = new HttpParameter("command", "APPEND"); params[1] = new HttpParameter("media_id", media_id_string); params[2] = new HttpParameter("segment_index", "0"); try { InputStream is = new FileInputStream(new File(video)); params[3] = new HttpParameter("media", video, is); } catch (Exception e) { e.printStackTrace(); } if (this.postParameter != null && this.postParameter.length > 0) { this.appendHttpParameters(params, this.postParameter); } else { this.postParameter = params; } headers.putAll(client.getRequestHeaders()); HttpRequest req = new HttpRequest(RequestMethod.POST, uploadUrl, postParameter, null, headers); String authheader = oauth.getAuthorizationHeader(req); headers.put("Authorization", authheader); HttpRequest req2 = new HttpRequest(RequestMethod.POST, uploadUrl, postParameter, null, headers); try { httpResponse = client.request(req2, null); if (httpResponse.getStatusCode() != 202) { Log.e("Error from Twitter", "get error in INIT:" + httpResponse.getStatusCode() + ", details:" + httpResponse.toString()); } return httpResponse.asJSONObject().getString("media_id_string"); } catch (Exception e) { e.printStackTrace(); } return null; } private String finalize(String media_id_string) { HttpParameter[] params = new HttpParameter[2]; params[0] = new HttpParameter("command", "FINALIZE"); params[1] = new HttpParameter("media_id", media_id_string); if (this.postParameter != null && this.postParameter.length > 0) { this.appendHttpParameters(params, this.postParameter); } else { this.postParameter = params; } headers.putAll(client.getRequestHeaders()); HttpRequest req = new HttpRequest(RequestMethod.POST, uploadUrl, postParameter, null, headers); String authheader = oauth.getAuthorizationHeader(req); headers.put("Authorization", authheader); HttpRequest req2 = new HttpRequest(RequestMethod.POST, uploadUrl, postParameter, null, headers); try { httpResponse = client.request(req2, null); if (httpResponse.getStatusCode() != 202) { Log.e("Error from Twitter", "get error in INIT:" + httpResponse.getStatusCode() + ", details:" + httpResponse.toString()); } Log.d(TAG, "Response after finalized:" + httpResponse.asString()); return httpResponse.asJSONObject().getString("media_id_string"); } catch (Exception e) { e.printStackTrace(); } return null; } protected abstract void preUpload() throws TwitterException; protected abstract String postUpload() throws TwitterException; HttpParameter[] appendHttpParameters(HttpParameter[] src, HttpParameter[] dst) { int srcLen = src.length; int dstLen = dst.length; HttpParameter[] ret = new HttpParameter[srcLen + dstLen]; System.arraycopy(src, 0, ret, 0, srcLen); System.arraycopy(dst, 0, ret, srcLen, dstLen); return ret; } String generateVerifyCredentialsAuthorizationHeader(String method) { List<HttpParameter> oauthSignatureParams = oauth.generateOAuthSignatureHttpParams(method, AbstractVideoUploadImpl.TWITTER_VERIFY_CREDENTIALS_JSON_V1_1); return "OAuth realm=\"https://api.twitter.com/\"," + OAuthAuthorization.encodeParameters(oauthSignatureParams, ",", true); } protected String generateVerifyCredentialsAuthorizationURL(String verifyCredentialsUrl) { List<HttpParameter> oauthSignatureParams = oauth.generateOAuthSignatureHttpParams("GET", verifyCredentialsUrl); return verifyCredentialsUrl + "?" + OAuthAuthorization.encodeParameters(oauthSignatureParams); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; AbstractVideoUploadImpl that = (AbstractVideoUploadImpl) o; if (apiKey != null ? !apiKey.equals(that.apiKey) : that.apiKey != null) return false; if (!Arrays.equals(appendParameter, that.appendParameter)) return false; if (client != null ? !client.equals(that.client) : that.client != null) return false; if (conf != null ? !conf.equals(that.conf) : that.conf != null) return false; if (headers != null ? !headers.equals(that.headers) : that.headers != null) return false; if (httpResponse != null ? !httpResponse.equals(that.httpResponse) : that.httpResponse != null) return false; if (video != null ? !video.equals(that.video) : that.video != null) return false; if (message != null ? !message.equals(that.message) : that.message != null) return false; if (oauth != null ? !oauth.equals(that.oauth) : that.oauth != null) return false; if (!Arrays.equals(postParameter, that.postParameter)) return false; if (uploadUrl != null ? !uploadUrl.equals(that.uploadUrl) : that.uploadUrl != null) return false; return true; } @Override public int hashCode() { int result = client != null ? client.hashCode() : 0; result = 31 * result + (conf != null ? conf.hashCode() : 0); result = 31 * result + (apiKey != null ? apiKey.hashCode() : 0); result = 31 * result + (oauth != null ? oauth.hashCode() : 0); result = 31 * result + (uploadUrl != null ? uploadUrl.hashCode() : 0); result = 31 * result + (postParameter != null ? Arrays.hashCode(postParameter) : 0); result = 31 * result + (appendParameter != null ? Arrays.hashCode(appendParameter) : 0); result = 31 * result + (video != null ? video.hashCode() : 0); result = 31 * result + (message != null ? message.hashCode() : 0); result = 31 * result + (headers != null ? headers.hashCode() : 0); result = 31 * result + (httpResponse != null ? httpResponse.hashCode() : 0); return result; } @Override public String toString() { return "AbstractVideoUploadImpl{" + "client=" + client + ", conf=" + conf + ", apiKey='" + apiKey + '\'' + ", oauth=" + oauth + ", uploadUrl='" + uploadUrl + '\'' + ", postParameter=" + (postParameter == null ? null : Arrays.asList(postParameter)) + ", appendParameter=" + (appendParameter == null ? null : Arrays.asList(appendParameter)) + ", video=" + video + ", message=" + message + ", headers=" + headers + ", httpResponse=" + httpResponse + '}'; } }