cmu.edu.homework.mediaUpload.PhotoUploadFactory.java Source code

Java tutorial

Introduction

Here is the source code for cmu.edu.homework.mediaUpload.PhotoUploadFactory.java

Source

/*
 * 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.auth.Authorization;
import twitter4j.auth.AuthorizationFactory;
import twitter4j.auth.OAuthAuthorization;
import twitter4j.conf.Configuration;
import twitter4j.conf.ConfigurationContext;
import twitter4j.media.MediaProvider;
import twitter4j.media.PhotoUpload;

import static twitter4j.media.MediaProvider.IMG_LY;
import static twitter4j.media.MediaProvider.MOBYPICTURE;
import static twitter4j.media.MediaProvider.TWIPPLE;
import static twitter4j.media.MediaProvider.TWITTER;
import static twitter4j.media.MediaProvider.YFROG;

/**
 * @author Yusuke Yamamoto - yusuke at mac.com
 * @since Twitter4J 2.1.12
 */
public class PhotoUploadFactory {
    private final Configuration conf;
    private final MediaProvider defaultMediaProvider;
    private final String apiKey;

    /**
     * Creates an PhotoUploadFactory with default configuration
     */
    public PhotoUploadFactory() {
        this(ConfigurationContext.getInstance());
    }

    /**
     * Creates an PhotoUploadFactory with the specified configuration
     * @param conf configuration to be associated
     */
    public PhotoUploadFactory(Configuration conf) {
        String mediaProvider = conf.getMediaProvider().toLowerCase();
        if ("twitter".equals(mediaProvider)) {
            defaultMediaProvider = TWITTER;
        } else if ("imgly".equals(mediaProvider) || "img_ly".equals(mediaProvider)) {
            defaultMediaProvider = IMG_LY;
        } else if ("twipple".equals(mediaProvider)) {
            defaultMediaProvider = TWIPPLE;
        } else if ("yfrog".equals(mediaProvider)) {
            defaultMediaProvider = YFROG;
        } else if ("mobypicture".equals(mediaProvider)) {
            defaultMediaProvider = MOBYPICTURE;
        } else {
            throw new IllegalArgumentException("unsupported media provider:" + mediaProvider);
        }
        this.conf = conf;
        apiKey = conf.getMediaProviderAPIKey();
    }

    /**
     * Returns an PhotoUpload instance associated with the default media provider
     *
     * @return PhotoUpload
     */
    public PhotoUpload getInstance() {
        return getInstance(defaultMediaProvider);
    }

    /**
     * Returns an PhotoUpload instance associated with the default media provider
     *
     * @param authorization authorization
     * @return PhotoUpload
     * @since Twitter4J 2.1.11
     */
    public PhotoUpload getInstance(Authorization authorization) {
        return getInstance(defaultMediaProvider, authorization);
    }

    /**
     * Returns an PhotoUploader instance associated with the specified media provider
     *
     * @param mediaProvider media provider
     * @return PhotoUploader
     */
    public PhotoUpload getInstance(MediaProvider mediaProvider) {
        Authorization authorization = AuthorizationFactory.getInstance(conf);
        OAuthAuthorization oauth = (OAuthAuthorization) authorization;
        return getInstance(mediaProvider, authorization);
    }

    /**
     * Returns an PhotoUpload instance associated with the specified media provider
     *
     * @param mediaProvider media provider
     * @param authorization authorization
     * @return PhotoUpload
     * @since Twitter4J 2.1.11
     */
    public PhotoUpload getInstance(MediaProvider mediaProvider, Authorization authorization) {
        if (!(authorization instanceof OAuthAuthorization)) {
            throw new IllegalArgumentException("OAuth authorization is required.");
        }
        OAuthAuthorization oauth = (OAuthAuthorization) authorization;
        if (mediaProvider == TWITTER) {
            return new MiPhotoUpload(conf, oauth);
            //            return new TwitterPhotoUpload(conf, oauth);
        } else {
            throw new AssertionError("Unknown provider");
        }
    }
}