Created
December 25, 2010 11:38
-
-
Save shin1ogawa/754839 to your computer and use it in GitHub Desktop.
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 org.sorausagi.lingr; | |
import java.io.IOException; | |
import org.junit.Test; | |
import org.sorausagi.lingr.LingrUtil.LingrSession; | |
public class LingrTest { | |
private static final String USER = ""; | |
private static final String PASSWORD = ""; | |
@Test | |
public void start() throws IOException { | |
LingrSession session = createSessionAndGetRooms(USER, PASSWORD); | |
System.out.println("\"lingrSession\":" + session); | |
String counter = findKeyAndGetValue(LingrUtil.subscribe(session), | |
"counter"); | |
System.out.println("------counter=" + counter); | |
String observe = LingrUtil.observe(session, counter); // blockされる | |
System.out.println(observe); | |
LingrUtil.post(session, "SoraUsagi", "受け取った!" + observe); | |
} | |
public static LingrSession createSessionAndGetRooms(String user, | |
String password) throws IOException { | |
String response1 = LingrUtil | |
.createSessionAndGetResponse(user, password); | |
if (response1.contains("\"status\":\"error\"")) { | |
throw new RuntimeException(response1); | |
} | |
String session = findKeyAndGetValue(response1, "session"); | |
String publicId = findKeyAndGetValue(response1, "public_id"); | |
String nickname = findKeyAndGetValue(response1, "nickname"); | |
String response2 = LingrUtil.getRooms(session); | |
String[] rooms = findKeyAndGetValues(response2, "rooms"); | |
return new LingrSession(session, publicId, nickname, rooms); | |
} | |
LingrSession state; | |
static String[] findKeyAndGetValues(String string, String key) { | |
int indexOf1 = string.indexOf("\"" + key + "\":["); | |
int indexOf2 = string.indexOf(']', indexOf1); | |
String[] _rooms = string.substring( | |
indexOf1 + key.length() + 2/* two quote */+ 1/* semicolon */ | |
+ 1/* open square */, indexOf2 - 1/* close square */) | |
.split("\\,"); | |
String[] rooms = new String[_rooms.length]; | |
for (int i = 0; i < _rooms.length; i++) { | |
rooms[i] = _rooms[i].replaceAll("\"", ""); | |
} | |
return rooms; | |
} | |
static String findKeyAndGetValue(String string, String key) { | |
int indexOf1 = string.indexOf("\"" + key + "\""); | |
int indexOf2 = string.indexOf(',', indexOf1); | |
if (indexOf2 == -1) { | |
indexOf2 = string.indexOf('}', indexOf1); | |
; | |
} | |
String value = string.substring(indexOf1 + key.length() + 3/*two quotes and semicolon*/, indexOf2); | |
return value.replaceAll("\"", ""); | |
} | |
} |
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 org.sorausagi.lingr; | |
import java.io.ByteArrayOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.io.UnsupportedEncodingException; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.net.URLEncoder; | |
public class LingrUtil { | |
public static String createSessionAndGetResponse(String user, | |
String password) throws IOException { | |
StringBuilder b = new StringBuilder(); | |
b.append("user=").append(URLEncoder.encode(user, "utf-8")); | |
b.append("&password=").append(URLEncoder.encode(password, "utf-8")); | |
return new String(postAndGetResponse( | |
"http://lingr.com/api/session/create", b), "utf-8"); | |
} | |
public static String getRooms(String session) | |
throws UnsupportedEncodingException, IOException { | |
HttpURLConnection connection = (HttpURLConnection) new URL( | |
"http://lingr.com/api/user/get_rooms?session=" + session) | |
.openConnection(); | |
return new String(readInputStream(connection.getInputStream()), "utf-8"); | |
} | |
public static String subscribe(LingrSession lingrSession) | |
throws IOException { | |
StringBuilder b = new StringBuilder(); | |
b.append("session=").append( | |
URLEncoder.encode(lingrSession.session, "utf-8")); | |
b.append("&room="); | |
boolean first = true; | |
for (String room : lingrSession.rooms) { | |
if (!first) { | |
b.append(","); | |
} else { | |
first = false; | |
} | |
b.append(room); | |
} | |
b.append("&reset=true"); | |
return new String(postAndGetResponse( | |
"http://lingr.com/api/room/subscribe", b), "utf-8"); | |
} | |
/** | |
* レスポンス待ちでブロックする. | |
* <p> | |
* 以下はレスポンスの例 | |
* </p> | |
* | |
* <pre> | |
* {"status":"ok","counter":2624672,"events":[ | |
* {"message": | |
* {"id":"1382228", | |
* "room":"SoraUsagi", | |
* "public_session_id":"Hfk6PI", | |
* "icon_url":"http://www.gravatar.com/avatar/ae85e5ed67cd0638095099e83548ae20.jpg", | |
* "type":"user", | |
* "speaker_id":"shin1ogawa", | |
* "nickname":"shin1ogawa", | |
* "text":"\u3066\u3059\u3068", | |
* "timestamp":"2010-12-25T11:24:44Z", | |
* "local_id":"pending-Hfk6PI-1" | |
* },"event_id":2624672} | |
* ]} | |
* </pre> | |
* | |
* @param session | |
* @param counter | |
* @return | |
* @throws UnsupportedEncodingException | |
* @throws IOException | |
*/ | |
public static String observe(LingrSession session, String counter) | |
throws UnsupportedEncodingException, IOException { | |
HttpURLConnection connection = (HttpURLConnection) new URL( | |
"http://lingr.com:8080/api/event/observe?session=" | |
+ session.session + "&counter=" + counter) | |
.openConnection(); | |
return new String(readInputStream(connection.getInputStream()), "utf-8"); | |
} | |
public static String post(LingrSession session, String room, String text) | |
throws UnsupportedEncodingException, IOException { | |
StringBuilder b = new StringBuilder(); | |
b.append("session=") | |
.append(URLEncoder.encode(session.session, "utf-8")); | |
b.append("&room=").append(URLEncoder.encode(room, "utf-8")); | |
b.append("&nickname=").append( | |
URLEncoder.encode(session.nickname, "utf-8")); | |
b.append("&text=").append(URLEncoder.encode(text, "utf-8")); | |
return new String( | |
postAndGetResponse("http://lingr.com/api/room/say", b), "utf-8"); | |
} | |
static class LingrSession { | |
final String session; | |
final String publicId; | |
final String nickname; | |
final String[] rooms; | |
/** | |
* @param session | |
* @param publicId | |
* @param nickname | |
*/ | |
public LingrSession(String session, String publicId, String nickname, | |
String[] rooms) { | |
this.session = session; | |
this.publicId = publicId; | |
this.nickname = nickname; | |
this.rooms = rooms; | |
} | |
public String toString() { | |
StringBuilder b = new StringBuilder("{"); | |
b.append("\"session\":\"").append(session).append("\""); | |
b.append(",\"publicId\":\"").append(publicId).append("\""); | |
b.append(",\"nickname\":\"").append(nickname).append("\""); | |
b.append(", \"rooms\":["); | |
boolean first = true; | |
for (String room : rooms) { | |
if (!first) { | |
b.append(","); | |
} else { | |
first = false; | |
} | |
b.append("\"").append(room).append("\""); | |
} | |
b.append("]"); | |
b.append("}"); | |
return b.toString(); | |
} | |
} | |
static byte[] postAndGetResponse(String urlString, StringBuilder payload) | |
throws UnsupportedEncodingException, IOException { | |
URL url = new URL(urlString); | |
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | |
connection.setRequestMethod("POST"); | |
connection.setDoOutput(true); | |
OutputStream os = connection.getOutputStream(); | |
os.write(payload.toString().getBytes("utf-8")); | |
os.flush(); | |
os.close(); | |
connection.connect(); | |
return readInputStream(connection.getInputStream()); | |
} | |
static byte[] readInputStream(InputStream input) throws IOException { | |
ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
try { | |
byte[] buffer = new byte[4096]; | |
long count = 0; | |
int n = 0; | |
while (-1 != (n = input.read(buffer))) { | |
out.write(buffer, 0, n); | |
count += n; | |
} | |
out.flush(); | |
return out.toByteArray(); | |
} finally { | |
out.close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment