Skip to content

Instantly share code, notes, and snippets.

@shin1ogawa
Created February 16, 2011 07:31
Show Gist options
  • Save shin1ogawa/828996 to your computer and use it in GitHub Desktop.
Save shin1ogawa/828996 to your computer and use it in GitHub Desktop.
package apps.activity.util;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.IOUtils;
import apps.activity.MakeSyncCallServlet;
import com.google.apphosting.api.ApiProxy;
import com.google.apphosting.api.ApiProxy.ApiConfig;
import com.google.apphosting.api.ApiProxy.ApiProxyException;
import com.google.apphosting.api.ApiProxy.Environment;
import com.google.apphosting.api.ApiProxy.LogRecord;
/**
* @author shin1ogawa
*/
public class MakeSyncCallServletDelegate implements ApiProxy.Delegate<Environment> {
@SuppressWarnings("unchecked")
private final ApiProxy.Delegate<Environment> parent = ApiProxy.getDelegate();
final URL url;
/**
* the constructor.
* @param url for {@link MakeSyncCallServlet}
* @throws MalformedURLException
* @category constructor
*/
public MakeSyncCallServletDelegate(URL url) throws MalformedURLException {
this.url = url;
}
@Override
public void log(Environment environment, LogRecord logRecord) {
parent.log(environment, logRecord);
}
@Override
public byte[] makeSyncCall(Environment environment, String serviceName, String methodName,
byte[] request) throws ApiProxyException {
if (serviceName.equals("urlfetch")) {
return parent.makeSyncCall(environment, serviceName, methodName, request);
}
try {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("serviceName", serviceName);
connection.setRequestProperty("methodName", methodName);
connection.setRequestProperty("applicationId", environment.getAppId());
IOUtils.write(request, connection.getOutputStream());
connection.connect();
int responseCode = connection.getResponseCode();
if (responseCode == 500) {
System.out.println(new String(connection.getResponseMessage()));
return new byte[0];
}
return IOUtils.toByteArray(connection.getInputStream());
} catch (IOException e) {
e.printStackTrace();
return new byte[0];
}
}
@Override
public Future<byte[]> makeAsyncCall(Environment environment, String serviceName,
String methodName, byte[] request, ApiConfig apiConfig) {
if (serviceName.equals("urlfetch")) {
return parent.makeAsyncCall(environment, serviceName, methodName, request, apiConfig);
}
try {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("serviceName", serviceName);
connection.setRequestProperty("methodName", methodName);
connection.setRequestProperty("applicationId", environment.getAppId());
IOUtils.write(request, connection.getOutputStream());
connection.connect();
int responseCode = connection.getResponseCode();
if (responseCode == 500) {
System.out.println(new String(connection.getResponseMessage()));
return new FutureImpl(new byte[0]);
}
return new FutureImpl(IOUtils.toByteArray(connection.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
return new FutureImpl(new byte[0]);
}
}
/**
* @param appId
* @return {@link Environment} that was overwriten appId.
*/
public static ApiProxy.Environment wrappedEnvironment(String appId) {
return new EnvironementImpl(appId, ApiProxy.getCurrentEnvironment());
}
/**
* @return the parent
*/
public ApiProxy.Delegate<Environment> getParent() {
return parent;
}
static class EnvironementImpl implements ApiProxy.Environment {
final String appId;
final ApiProxy.Environment parent;
/**
* the constructor.
* @param appId
* @param parent
* @category constructor
*/
public EnvironementImpl(String appId, Environment parent) {
this.appId = appId;
this.parent = parent;
}
@Override
public String getAppId() {
return appId;
}
@Override
public Map<String, Object> getAttributes() {
return parent.getAttributes();
}
@Override
public String getAuthDomain() {
return parent.getAuthDomain();
}
@Override
public String getEmail() {
return parent.getEmail();
}
@SuppressWarnings("deprecation")
@Override
public String getRequestNamespace() {
return parent.getRequestNamespace();
}
@Override
public String getVersionId() {
return parent.getVersionId();
}
@Override
public boolean isAdmin() {
return parent.isAdmin();
}
@Override
public boolean isLoggedIn() {
return parent.isLoggedIn();
}
}
static class FutureImpl implements Future<byte[]> {
final byte[] bytes;
boolean canceled = false;
FutureImpl(byte[] bytes) {
this.bytes = bytes;
}
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
canceled = true;
return true;
}
@Override
public byte[] get() {
return bytes;
}
@Override
public byte[] get(long timeout, TimeUnit unit) {
return bytes;
}
@Override
public boolean isCancelled() {
return canceled;
}
@Override
public boolean isDone() {
return false;
}
}
}
@shin1ogawa
Copy link
Author

Tools for Google App Engine/JavaのMakeSyncCallが動作しなくなっている問題への一時的な対応です。サーバ側モジュールはそのままで動作します。
現在は下記の対応を行なっている真っ最中でリリースできない状態なのですすいません。

  • 外部ライブラリへの依存を全て外す
  • 認証の仕組みを変更する
  • ClientLoginだけではなくOAuthに対応する

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment