aNNiMON

PastebinRequest

Jun 28th, 2012
137
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.pastebin.pastebinplugin;
  2.  
  3. import java.io.UnsupportedEncodingException;
  4. import java.net.URLEncoder;
  5.  
  6. /**
  7.  *
  8.  * @author aNNiMON
  9.  */
  10. public class PastebinRequest {
  11.    
  12.     public static enum EXPIRE_DATE {
  13.         NEVER,
  14.         TEN_MINUTES,
  15.         ONE_HOUR,
  16.         ONE_DAY,
  17.         ONE_MONTH
  18.     };
  19.    
  20.     /** Unique developer API key. @see http://pastebin.com/api#1 */
  21.     private static final String API_DEV_KEY = "USE YOUR KEY!!";
  22.    
  23.     /** Storage period of pasted code. @see http://pastebin.com/api#6 */
  24.     private static final String[] EXPIRE_DATE_VALUES = {
  25.         "N", "10M", "1H", "1D", "1M"
  26.     };
  27.    
  28.     private StringBuilder requestMessage;
  29.    
  30.     public PastebinRequest() {
  31.         requestMessage = new StringBuilder();
  32.     }
  33.    
  34.     public void initRequest() {
  35.         requestMessage.setLength(0);
  36.         append("api_option", "paste");
  37.         append("api_dev_key", API_DEV_KEY);
  38.         append("api_user_key", "");
  39.     }
  40.    
  41.     public void setSourceCode(String source) {
  42.         append("api_paste_code", source);
  43.     }
  44.    
  45.     public void setFormat(String mimeType) {
  46.         append("api_paste_format", getFormat(mimeType));
  47.     }
  48.    
  49.     public void setExpireDate(EXPIRE_DATE date) {
  50.         append("api_paste_expire_date", EXPIRE_DATE_VALUES[date.ordinal()]);
  51.     }
  52.    
  53.     public void setUnlisted(boolean isUnlisted) {
  54.         append("api_paste_private", isUnlisted ? "1" : "0");
  55.     }
  56.    
  57.     public void setName(String name) {
  58.         append("api_paste_name", name);
  59.     }
  60.    
  61.     public String getRequestMessage() {
  62.         return requestMessage.toString();
  63.     }
  64.    
  65.     private void append(String key, String value) {
  66.         if (!key.equals("api_option")) requestMessage.append('&');
  67.         requestMessage.append(key).append('=').append(encode(value));
  68.     }
  69.    
  70.     /*
  71.      * Convert mime type of edit document to known pastebin format.
  72.      */
  73.     private String getFormat(String mimeType) {
  74.         if (mimeType.indexOf("xml") != -1) return "xml";
  75.         else if (mimeType.indexOf("html") != -1) return "html4strict";
  76.         else if (mimeType.indexOf("css") != -1) return "css";
  77.         else if (mimeType.indexOf("javascript") != -1) return "javascript";
  78.         else if (mimeType.indexOf("java") != -1) return "java";
  79.         else if (mimeType.indexOf("php") != -1) return "php";
  80.         else if (mimeType.indexOf("groovy") != -1) return "groovy";
  81.         else if (mimeType.indexOf("ruby") != -1) return "ruby";
  82.         else if (mimeType.indexOf("perl") != -1) return "perl";
  83.         return "text";
  84.     }
  85.    
  86.     private String encode(String str) {
  87.         try {
  88.             return URLEncoder.encode(str, "UTF-8");
  89.         } catch (UnsupportedEncodingException ex) {
  90.             return str;
  91.         }
  92.     }
  93. }
RAW Paste Data