Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b45575b

Browse filesBrowse files
author
TheShadow
committed
Initial comment of API code
0 parents  commit b45575b
Copy full SHA for b45575b

File tree

Expand file treeCollapse file tree

10 files changed

+336
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

10 files changed

+336
-0
lines changed
Open diff view settings
Collapse file

‎PasteHTMLAPI.iml‎

Copy file name to clipboard
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
12+
Collapse file

‎README.markdown‎

Copy file name to clipboard
+12Lines changed: 12 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#Java-PasteHTML-API
2+
3+
This is the project for the PasteHTML API written in Java. PasteHTML main website http://pastehtml.com/
4+
5+
6+
## Features
7+
8+
* Paste anything to the website via a String or a File of any size
9+
* Paste any type. HTML, Text, or Markdown.
10+
* Retreives URL after paste
11+
* Expandable. More features can be easily added.
12+
* Somewhat documented
Collapse file
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.darkprograms.pastehtml.constants;
2+
3+
/**
4+
* Constant class that holds constants
5+
*/
6+
public class Constants {
7+
8+
/**
9+
* Paste HTML main URL
10+
*/
11+
public static final String MAIN_URL = "http://pastehtml.com/upload/create?input_type=";
12+
13+
/**
14+
* Result argument for URL
15+
*/
16+
public static final String RESULT_ARG = "&result=address";
17+
18+
19+
}
Collapse file
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.darkprograms.pastehtml.exception;
2+
3+
/**
4+
* Exception class for a no type defined error.
5+
* Thrown when a PasteObject has no type
6+
*/
7+
public class NoTypeDefined extends Exception {
8+
9+
/**
10+
* Constructor
11+
*
12+
* @param msg Message to "throw"
13+
*/
14+
public NoTypeDefined(String msg) {
15+
super(msg);
16+
}
17+
18+
}
Collapse file
+101Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.darkprograms.pastehtml.network;
2+
3+
import com.darkprograms.pastehtml.constants.Constants;
4+
5+
import java.io.*;
6+
import java.net.URL;
7+
import java.net.URLConnection;
8+
import java.net.URLEncoder;
9+
10+
/**
11+
* Class that performs the network side handling of the actual paste
12+
* Classes that need these methods will extend of this class.
13+
*/
14+
public class PostPaste {
15+
16+
17+
/**
18+
* Constructor. Protected
19+
*/
20+
protected PostPaste() {
21+
22+
}
23+
24+
/**
25+
* Posts a paste and retrieves the URL as a response.
26+
*
27+
* @param data Data to post
28+
* @param postType Post type, txt,html, or mrk
29+
* @return Returns URL of posted data.
30+
*/
31+
protected String postPaste(String data, String postType) {
32+
try {
33+
URL url = new URL(Constants.MAIN_URL + postType + Constants.RESULT_ARG);
34+
String postData = URLEncoder.encode("txt", "UTF-8") + "=" + URLEncoder.encode(data, "UTF-8");
35+
36+
URLConnection connection = url.openConnection();
37+
connection.setDoOutput(true);
38+
39+
OutputStream outputStream = connection.getOutputStream();
40+
outputStream.write(postData.getBytes("UTF-8"));
41+
outputStream.close();
42+
43+
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
44+
String response = br.readLine();
45+
46+
br.close();
47+
48+
return response;
49+
} catch (Exception ex) {
50+
ex.printStackTrace();
51+
}
52+
return null;
53+
}
54+
55+
/**
56+
* Posts a paste from data from a file and retrieves the URL as a response.
57+
* Data is buffered, so any size of file can be posted.
58+
*
59+
* @param file File to post
60+
* @param postType Post type, txt,html, or mrk
61+
* @return Returns URL of posted data.
62+
*/
63+
protected String postPaste(File file, String postType) {
64+
try {
65+
URL url = new URL(Constants.MAIN_URL + postType + Constants.RESULT_ARG);
66+
String param = URLEncoder.encode("txt", "UTF-8") + "=";
67+
68+
InputStream inputStream = new FileInputStream(file);
69+
70+
71+
URLConnection connection = url.openConnection();
72+
connection.setDoOutput(true);
73+
74+
OutputStream outputStream = connection.getOutputStream();
75+
outputStream.write(param.getBytes("UTF-8"));
76+
77+
byte[] bytes = new byte[256];
78+
79+
int read;
80+
81+
while ((read = inputStream.read(bytes, 0, 256)) != -1) {
82+
outputStream.write(bytes, 0, read);
83+
}
84+
85+
86+
outputStream.close();
87+
88+
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
89+
String response = br.readLine();
90+
91+
br.close();
92+
93+
return response;
94+
} catch (Exception ex) {
95+
ex.printStackTrace();
96+
}
97+
return null;
98+
}
99+
100+
101+
}
Collapse file
+135Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package com.darkprograms.pastehtml.objects;
2+
3+
import com.darkprograms.pastehtml.exception.NoTypeDefined;
4+
import com.darkprograms.pastehtml.network.PostPaste;
5+
import com.darkprograms.pastehtml.types.PasteHTML;
6+
import com.darkprograms.pastehtml.types.PasteMD;
7+
import com.darkprograms.pastehtml.types.PasteText;
8+
import com.darkprograms.pastehtml.types.PasteType;
9+
10+
import java.io.File;
11+
12+
/**
13+
* PasteObject class. Extends PostPaste for network methods.
14+
*/
15+
public class PasteObject extends PostPaste {
16+
17+
18+
/**
19+
* Type text
20+
*/
21+
private String typeText = null;
22+
23+
/**
24+
* PasteObject constructor
25+
*
26+
* @param type Type you want this object to be
27+
*/
28+
public PasteObject(PasteType type) {
29+
30+
setTypeClass(type);
31+
}
32+
33+
/**
34+
* PasteObject constructor. You must specify the type with setType
35+
*/
36+
public PasteObject() {
37+
38+
}
39+
40+
41+
/**
42+
* Determines if a class is equal to another class
43+
*
44+
* @param classObject Class
45+
* @param type PasteType subclass to test
46+
* @return True if equal, false otherwise
47+
*/
48+
private boolean determineClass(Class classObject, PasteType type) {
49+
return type.getClass().equals(classObject);
50+
}
51+
52+
/**
53+
* Sets the type of this object
54+
*
55+
* @param type Sublcass of PasteType
56+
*/
57+
private void setTypeClass(PasteType type) {
58+
if (determineClass(PasteText.class, type)) {
59+
setTypeText("text");
60+
} else if (determineClass(PasteHTML.class, type)) {
61+
setTypeText("HTML");
62+
} else if (determineClass(PasteMD.class, type)) {
63+
setTypeText("Markdown");
64+
} else {
65+
setTypeText("unknown");
66+
}
67+
}
68+
69+
private String determineTypeForPost() {
70+
if (getTypeText().equals("text")) {
71+
return "txt";
72+
} else if (getTypeText().equals("HTML")) {
73+
return "html";
74+
} else if (getTypeText().equals("Markdown")) {
75+
return "mrk";
76+
} else {
77+
return null;
78+
}
79+
}
80+
81+
/**
82+
* Gets the string representation of the current type of this object
83+
*
84+
* @return Returns a string. "test", "HTML", and "Markdown" are currently returned
85+
*/
86+
public String getTypeText() {
87+
return typeText;
88+
}
89+
90+
/**
91+
* Sets the type text for this object
92+
*
93+
* @param typeText Type text. "test", "HTML", and "Markdown"
94+
*/
95+
private void setTypeText(String typeText) {
96+
this.typeText = typeText;
97+
}
98+
99+
/**
100+
* Sets this objects type. PasteText, PasteMD, and PasteHTML extend of off PasteType
101+
*
102+
* @param type Class that is the type you want this object to be
103+
*/
104+
public void setType(PasteType type) {
105+
setTypeClass(type);
106+
}
107+
108+
/**
109+
* Paste data and return url as response.
110+
*
111+
* @param data Data to post to this object
112+
* @return Returns String representation of the URL of the posted object
113+
*/
114+
public String pasteData(String data) throws NoTypeDefined {
115+
if (getTypeText() == null || getTypeText().equals("unknown")) {
116+
throw new NoTypeDefined("No Type has been defined or an unknown type has been defined for this object");
117+
}
118+
return postPaste(data, determineTypeForPost());
119+
}
120+
121+
/**
122+
* Paste a file and return url as response.
123+
*
124+
* @param file File to post to this object
125+
* @return Returns String representation of the URL of the posted object
126+
*/
127+
public String pasteFile(File file) throws NoTypeDefined {
128+
if (getTypeText() == null || getTypeText().equals("unknown")) {
129+
throw new NoTypeDefined("No Type has been defined or an unknown type has been defined for this object");
130+
}
131+
return postPaste(file, determineTypeForPost());
132+
}
133+
134+
135+
}
Collapse file
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.darkprograms.pastehtml.types;
2+
3+
/**
4+
* PasteHTML class representing the type HTML
5+
*/
6+
public class PasteHTML extends PasteType {
7+
}
Collapse file
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.darkprograms.pastehtml.types;
2+
3+
/**
4+
* PasteMD representing a Markdown paste
5+
*/
6+
public class PasteMD extends PasteType {
7+
8+
9+
}
Collapse file
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.darkprograms.pastehtml.types;
2+
3+
/**
4+
* PasteText class representing a text paste
5+
*/
6+
public class PasteText extends PasteType {
7+
}
Collapse file
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.darkprograms.pastehtml.types;
2+
3+
/**
4+
* PasteType class that all Types extend of off
5+
*/
6+
public class PasteType {
7+
8+
/**
9+
* Protected constructor
10+
*/
11+
protected PasteType() {
12+
13+
}
14+
15+
16+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.