Skip to content

Instantly share code, notes, and snippets.

@cldrn cldrn/PasteBinDrain.java
Last active Aug 29, 2015

Embed
What would you like to do?
PasteBinDrain.java
package com.sinfonier.drains;
import java.sql.Timestamp;
import java.util.Date;
import org.glassfish.jersey.core;
/*
* PasteBinDrain
* Drain disenado para dumpear el contenido deseado en pastebin.com.
*
* Este drain utiliza la API oficial de pastebin para publicar el contenido.
* Los campos requeridos en la peticion son:
* api_option : Este valor debe ser la cadena de texto "paste" para publicar contenido.
* api_dev_key : API key.
* api_paste_code : Contenido del paste
*
*/
public class PasteBinDrain extends BaseSinfonierDrain {
private String apiKey;
private Integer visibility;
private String expirationTime;
private String pasteFormat;
private String pasteContent;
private String prefixName;
private final String API_URI = "http://pastebin.com/api/api_post.php";
/*
* Valores opcionales
*/
private final String DEFAULT_PASTE_NAME = "SINFONIERRULES"; //Prefijo de nombre
private final String DEFAULT_PASTE_FORMAT = "c"; //Pastebin soporta mas de 200 formatos: http://pastebin.com/api#3
private final Integer DEFAULT_PASTE_VISIBILITY = 0; //public = 0, unlisted = 1, private = 2
/*
* Posibles valores para tiempo de expiracion de un paste:
* N = Never
* 10M = 10 Minutes
* 1H = 1 Hour
* 1D = 1 Day
* 1W = 1 Week
* 2W = 2 Weeks
* 1M = 1 Month
*/
private final String DEFAULT_PASTE_EXPIRATION = "N";
public PasteBinDrain(String xmlFile) {
super(xmlFile);
}
@Override
public void userprepare() {
this.apiKey = (String)this.getParam("api_key");
this.visibility = Integer.parseInt(this.getParam("visibility"));
this.expirationTime = (String)this.getParam("expiration");
this.pasteFormat = (String)this.getParam("format");
}
@Override
public void userexecute() {
this.pasteContent = this.getRawJson();
setOptionalArgs();
submitPastebin();
}
public void usercleanup() {
this.pasteContent = "";
}
/*
* setOptionalArgs
* Funcion de ayuda para settear los valores opcionales en caso de que no hayan sido especificados.
*/
public void setOptionalArgs() {
if(this.pasteFormat == null) this.pasteFormat = DEFAULT_PASTE_FORMAT;
if(this.expirationTime == null) this.expirationTime = DEFAULT_PASTE_EXPIRATION;
if(this.visibility == null) this.visibility = DEFAULT_PASTE_VISIBILITY;
if(this.prefixName == null) this.prefixName = DEFAULT_PASTE_NAME;
}
/*
* submitPastebin
* Esta es la funcion que manda el contenido a la API de pastebin.com.
*/
public void submitPastebin() {
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(UriBuilder.fromUri(API_URI).build());
Form form = new Form();
form.add("api_option", "paste");
form.add("api_paste_private", this.visibility);
form.add("api_paste_name", this.namePrefix);
form.add("api_paste_expire_date", this.expirationTime);
form.add("api_paste_format", this.pasteFormat);
form.add("api_dev_key", this.apikey);
form.add("api_paste_code", this.pasteContent);
ClientResponse response = service.path("restPath").path("resourcePath").
type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class, form);
System.out.println("Respuesta:" + response.getEntity(String.class));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.