agirbot/src/org/april/hebdobot/pastebin/PastebinClient.java
Christian P. MOMON ca28c68007 Change Pastebin connection from HTTP to HTTPS (note in API page:
IMPORTANT: Please update all API calls to HTTPS before March 1st, 2018.
We will then block all API calls over HTTP.").
2017-12-28 15:36:27 +01:00

493 lines
13 KiB
Java

/**
* Copyright (C) 2011-2013 Nicolas Vinot <aeris@imirhil.fr>
* Copyright (C) 2017 Christian Pierre MOMON <cmomon@april.org>
*
* This file is part of (April) Hebdobot.
*
* Hebdobot is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Hebdobot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Hebdobot. If not, see <http://www.gnu.org/licenses/>
*/
package org.april.hebdobot.pastebin;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.april.hebdobot.HebdobotException;
/**
* The Class PastebinClient.
*/
public class PastebinClient
{
private static final String API_LOGIN_URL = "https://pastebin.com/api/api_login.php";
private static final String API_POST_URL = "https://pastebin.com/api/api_post.php";
private static final String API_DEV_KEY = "api_dev_key";
private static final String API_USER_KEY = "api_user_key";
private static final String API_USER_NAME = "api_user_name";
private static final String API_USER_PASSWORD = "api_user_password";
private static final String API_OPTION = "api_option";
private static final String API_PASTE_PRIVATE = "api_paste_private";
private static final String API_PASTE_NAME = "api_paste_name";
private static final String API_PASTE_EXPIRATION = "api_paste_expire_date";
private static final String API_PASTE_FORMAT = "api_paste_format";
private static final String API_PASTE_CODE = "api_paste_code";
public static final String API_ERROR = "Bad API request,";
private String apiKey;
private String apiUserKey;
private HttpClient httpClient;
/**
* Instantiates a new pastebin client.
*
* @param apiKey
* the api key
*/
public PastebinClient(final String apiKey)
{
this.apiKey = apiKey;
this.httpClient = new DefaultHttpClient();
}
/**
* Login.
*
* @param name
* the name
* @param password
* the password
* @throws HebdobotException
* the hebdobot exception
*/
public void login(final String name, final String password) throws HebdobotException
{
try
{
final List<NameValuePair> params = new LinkedList<NameValuePair>();
setParameter(params, API_DEV_KEY, this.apiKey);
setParameter(params, API_USER_NAME, name);
setParameter(params, API_USER_PASSWORD, password);
final HttpPost request = new HttpPost(API_LOGIN_URL);
request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
final HttpResponse response = this.httpClient.execute(request);
final String content = IOUtils.toString(response.getEntity().getContent());
APIException.throwIfError(content);
this.apiUserKey = content;
}
catch (UnsupportedEncodingException exception)
{
throw new HebdobotException(exception);
}
catch (ClientProtocolException exception)
{
throw new HebdobotException(exception);
}
catch (IOException exception)
{
throw new HebdobotException(exception);
}
catch (APIException exception)
{
throw new HebdobotException(exception);
}
}
/**
* Paste.
*
* @param code
* the code
* @return the string
* @throws HebdobotException
* the hebdobot exception
*/
public String paste(final String code) throws HebdobotException
{
String result;
result = this.paste(code, null, Format.NONE, Private.PUBLIC, Expiration.DAY_1);
//
return result;
}
/**
* Paste.
*
* @param code
* the code
* @param expiration
* the expiration
* @return the string
* @throws HebdobotException
* the hebdobot exception
*/
public String paste(final String code, final Expiration expiration) throws HebdobotException
{
String result;
result = this.paste(code, null, Format.NONE, Private.PUBLIC, expiration);
//
return result;
}
/**
* Paste.
*
* @param code
* the code
* @param format
* the format
* @return the string
* @throws HebdobotException
* the hebdobot exception
*/
public String paste(final String code, final Format format) throws HebdobotException
{
String result;
result = this.paste(code, null, format, Private.PUBLIC, Expiration.DAY_1);
//
return result;
}
/**
* Paste.
*
* @param code
* the code
* @param format
* the format
* @param expiration
* the expiration
* @return the string
* @throws HebdobotException
* the hebdobot exception
*/
public String paste(final String code, final Format format, final Expiration expiration) throws HebdobotException
{
String result;
result = this.paste(code, null, format, Private.PUBLIC, expiration);
//
return result;
}
/**
* Paste.
*
* @param code
* the code
* @param format
* the format
* @param privat
* the privat
* @return the string
* @throws HebdobotException
* the hebdobot exception
*/
public String paste(final String code, final Format format, final Private privat) throws HebdobotException
{
String result;
result = this.paste(code, null, format, privat, Expiration.DAY_1);
//
return result;
}
/**
* Paste.
*
* @param code
* the code
* @param format
* the format
* @param privat
* the privat
* @param expiration
* the expiration
* @return the string
* @throws HebdobotException
* the hebdobot exception
*/
public String paste(final String code, final Format format, final Private privat, final Expiration expiration) throws HebdobotException
{
String result;
result = this.paste(code, null, format, privat, expiration);
//
return result;
}
/**
* Paste.
*
* @param code
* the code
* @param privat
* the privat
* @return the string
* @throws HebdobotException
* the hebdobot exception
*/
public String paste(final String code, final Private privat) throws HebdobotException
{
String result;
result = this.paste(code, null, Format.NONE, privat, Expiration.DAY_1);
//
return result;
}
/**
* Paste.
*
* @param code
* the code
* @param privat
* the privat
* @param expiration
* the expiration
* @return the string
* @throws HebdobotException
* the hebdobot exception
*/
public String paste(final String code, final Private privat, final Expiration expiration) throws HebdobotException
{
String result;
result = this.paste(code, null, Format.NONE, privat, expiration);
//
return result;
}
/**
* Paste.
*
* @param code
* the code
* @param name
* the name
* @return the string
* @throws HebdobotException
* the hebdobot exception
*/
public String paste(final String code, final String name) throws HebdobotException
{
String result;
result = this.paste(code, name, Format.NONE, Private.PUBLIC, Expiration.DAY_1);
//
return result;
}
/**
* Paste.
*
* @param code
* the code
* @param name
* the name
* @param format
* the format
* @return the string
* @throws HebdobotException
* the hebdobot exception
*/
public String paste(final String code, final String name, final Format format) throws HebdobotException
{
String result;
result = this.paste(code, name, format, Private.PUBLIC, Expiration.DAY_1);
//
return result;
}
/**
* Paste.
*
* @param code
* the code
* @param name
* the name
* @param format
* the format
* @param expiration
* the expiration
* @return the string
* @throws HebdobotException
* the hebdobot exception
*/
public String paste(final String code, final String name, final Format format, final Expiration expiration) throws HebdobotException
{
String result;
result = this.paste(code, name, format, Private.PUBLIC, expiration);
//
return result;
}
/**
* Paste.
*
* @param code
* the code
* @param name
* the name
* @param format
* the format
* @param privat
* the privat
* @return the string
* @throws HebdobotException
* the hebdobot exception
*/
public String paste(final String code, final String name, final Format format, final Private privat) throws HebdobotException
{
String result;
result = this.paste(code, name, format, privat, Expiration.DAY_1);
//
return result;
}
/**
* Paste.
*
* @param code
* the code
* @param name
* the name
* @param format
* the format
* @param privat
* the privat
* @param expiration
* the expiration
* @return the string
* @throws HebdobotException
* the exception
*/
public String paste(final String code, final String name, final Format format, final Private privat, final Expiration expiration)
throws HebdobotException
{
String result;
try
{
List<NameValuePair> params = new LinkedList<NameValuePair>();
setParameter(params, API_DEV_KEY, this.apiKey);
setParameter(params, API_USER_KEY, this.apiUserKey);
setParameter(params, API_OPTION, Option.PASTE.getValue());
setParameter(params, API_PASTE_PRIVATE, privat.getValue());
setParameter(params, API_PASTE_NAME, name);
setParameter(params, API_PASTE_EXPIRATION, expiration.getValue());
setParameter(params, API_PASTE_FORMAT, format.getValue());
setParameter(params, API_PASTE_CODE, code);
final HttpPost request = new HttpPost(API_POST_URL);
request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
final HttpResponse response = this.httpClient.execute(request);
result = IOUtils.toString(response.getEntity().getContent());
APIException.throwIfError(result);
}
catch (UnsupportedEncodingException exception)
{
throw new HebdobotException(exception);
}
catch (ClientProtocolException exception)
{
throw new HebdobotException(exception);
}
catch (IOException exception)
{
throw new HebdobotException(exception);
}
catch (APIException exception)
{
throw new HebdobotException(exception);
}
//
return result;
}
/**
* Paste.
*
* @param code
* the code
* @param name
* the name
* @param privat
* the privat
* @return the string
* @throws HebdobotException
* the hebdobot exception
*/
public String paste(final String code, final String name, final Private privat) throws HebdobotException
{
String result;
result = this.paste(code, name, Format.NONE, privat, Expiration.DAY_1);
//
return result;
}
/**
* Sets the parameter.
*
* @param params
* the params
* @param name
* the name
* @param value
* the value
*/
private static void setParameter(final List<NameValuePair> params, final String name, final String value)
{
if (value != null)
{
params.add(new BasicNameValuePair(name, value));
}
}
}