net.shopxx.plugin.OAuthPlugin.java Source code

Java tutorial

Introduction

Here is the source code for net.shopxx.plugin.OAuthPlugin.java

Source

/*
 * Copyright 2014 Alibaba Group Holding Ltd.
 *
 * 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.
 */
// Created on 201478
// $Id$

package net.shopxx.plugin;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.Map;

import javax.annotation.Resource;

import net.shopxx.entity.PluginConfig;
import net.shopxx.service.PluginConfigService;
import net.shopxx.util.HttpUtils;

import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.springframework.stereotype.Component;

/**
 * oauth?
 * 
 * @author czllfy
 */
public abstract class OAuthPlugin implements Comparable<OAuthPlugin> {

    /** ????? */
    public static final String OAUTH_NAME_ATTRIBUTE_NAME = "oAuthName";

    /** LOGO?? */
    public static final String LOGO_ATTRIBUTE_NAME = "logo";

    /** ???? */
    public static final String DESCRIPTION_ATTRIBUTE_NAME = "description";

    @Resource(name = "pluginConfigServiceImpl")
    private PluginConfigService pluginConfigService;

    /**
     * ?ID
     * 
     * @return ID
     */
    public final String getId() {
        return getClass().getAnnotation(Component.class).value();
    }

    /**
     * ???
     * 
     * @return ??
     */
    public abstract String getName();

    /**
     * ?
     * 
     * @return 
     */
    public abstract String getVersion();

    /**
     * ?
     * 
     * @return 
     */
    public abstract String getAuthor();

    /**
     * ??
     * 
     * @return ?
     */
    public abstract String getSiteUrl();

    /**
     * ?URL
     * 
     * @return URL
     */
    public abstract String getInstallUrl();

    /**
     * ??URL
     * 
     * @return ?URL
     */
    public abstract String getUninstallUrl();

    /**
     * ?URL
     * 
     * @return URL
     */
    public abstract String getSettingUrl();

    /**
     * ?
     * 
     * @return
     */
    public abstract boolean bind();

    /**
     * ?
     * 
     * @return
     */
    public abstract boolean unbind();

    /**
     * 
     */
    public abstract void login();

    protected String getAuthorizeUrl(String authorize, Map<String, String> params)
            throws UnsupportedEncodingException {
        return HttpUtils.initParams(authorize, params);
    }

    protected String doPost(String url, Map<String, String> params)
            throws IOException, KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException {
        return HttpUtils.post(url, params);
    }

    protected String doGet(String url, Map<String, String> params)
            throws IOException, KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException {
        return HttpUtils.get(url, params);
    }

    protected String doGetWithHeaders(String url, Map<String, String> headers)
            throws IOException, KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException {
        return HttpUtils.get(url, null, headers);
    }

    /**
     * ??
     * 
     * @return ?
     */
    public boolean getIsInstalled() {
        return pluginConfigService.pluginIdExists(getId());
    }

    /**
     * ???
     * 
     * @return ??
     */
    public PluginConfig getPluginConfig() {
        return pluginConfigService.findByPluginId(getId());
    }

    /**
     * ???
     * 
     * @return ??
     */
    public boolean getIsEnabled() {
        PluginConfig pluginConfig = getPluginConfig();
        return pluginConfig != null ? pluginConfig.getIsEnabled() : false;
    }

    /**
     * ?
     * 
     * @param name ??
     * @return 
     */
    public String getAttribute(String name) {
        PluginConfig pluginConfig = getPluginConfig();
        return pluginConfig != null ? pluginConfig.getAttribute(name) : null;
    }

    /**
     * ??
     * 
     * @return ?
     */
    public Integer getOrder() {
        PluginConfig pluginConfig = getPluginConfig();
        return pluginConfig != null ? pluginConfig.getOrder() : null;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        if (this == obj) {
            return true;
        }
        StoragePlugin other = (StoragePlugin) obj;
        return new EqualsBuilder().append(getId(), other.getId()).isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37).append(getId()).toHashCode();
    }

    public int compareTo(OAuthPlugin storagePlugin) {
        return new CompareToBuilder().append(getOrder(), storagePlugin.getOrder())
                .append(getId(), storagePlugin.getId()).toComparison();
    }

}