Created
May 4, 2013 02:47
-
-
Save jackie-scholl/5515888 to your computer and use it in GitHub Desktop.
I wrote a server in 90 lines (it only handles OAuth callbacks).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package scholl.both.analyzer.social; | |
import java.io.*; | |
import java.net.*; | |
/** | |
* An HTTP server. | |
* | |
* NOTE: I modified this from http://fragments.turtlemeat.com/javawebserver.php | |
* | |
* @author Jackson | |
*/ | |
public class OAuthCallbackServer { | |
private final int port; // port we are going to listen to | |
/** | |
* Sole constructor. | |
* | |
* @param listenPort port to bind to (default 80) | |
*/ | |
public OAuthCallbackServer(int listenPort) { | |
port = listenPort; | |
} | |
/** | |
* Get a request on the set port and return the verifier as a string. | |
* | |
* @return the OAuth verifier | |
* @throws IOException | |
*/ | |
public String handleRequest() throws IOException { | |
System.out.printf("A simple HTTP server, used for OAuth callback.%n%n"); | |
System.out.printf("Trying to bind to localhost on port %d...", port); | |
ServerSocket serverSocket = new ServerSocket(port); | |
System.out.printf("Ready, Waiting for requests...%n"); | |
try { | |
// this call waits/blocks until someone connects to the port we are listening to | |
Socket connectionSocket = serverSocket.accept(); | |
return getOAuthVerifier(new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())), | |
new DataOutputStream(connectionSocket.getOutputStream())); | |
} finally { | |
serverSocket.close(); | |
} | |
} | |
private String getOAuthVerifier(BufferedReader input, DataOutputStream output) throws IOException { | |
PrintStream print = new PrintStream(new BufferedOutputStream(output)); | |
String tmp = input.readLine(); | |
if (!tmp.toUpperCase().startsWith("GET")) { // Unsupported operation | |
output.writeBytes(constructHeader(501)); // HTTP "Not Implemented" response | |
output.close(); | |
throw new UnsupportedOperationException(); | |
} | |
print.print(constructHeader(200)); | |
String verifier = tmp.replaceAll("GET /callback\\?oauth_token=\\w+&oauth_verifier=(\\w+) HTTP/1.1", "$1"); | |
// print.printf("%nVerifier: %s%n", verifier); | |
print.close(); | |
return verifier; | |
} | |
/** | |
* This method makes the HTTP header for the response. | |
* | |
* @param returnCode HTTP response code | |
* @return HTTP header | |
*/ | |
private String constructHeader(int returnCode) { | |
String responseString = ""; | |
if (returnCode == 200) { | |
responseString = "OK"; | |
} else if (returnCode == 501) { | |
responseString = "Not Implemented"; | |
} else { | |
responseString = "(return code unknown)"; | |
} | |
String s = String.format("HTTP/1.0 %d %s%s", returnCode, responseString, | |
"%nConnection: close%nServer: SimpleHTTPtutorial v0%nContent-Type: text/html%n%n"); | |
return s; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment