package org.psjava.judgesubmit; import org.puredevteam.httpclient.CanceledException; import org.puredevteam.httpclient.CookieKeepingHttpClient; import org.puredevteam.httpclient.CookieKeepingHttpClientFactory; import org.puredevteam.httpclient.EmptyCanceler; import org.puredevteam.httpclient.HttpClient; import org.puredevteam.httpclient.HttpClientFactory; import org.puredevteam.httpclient.HttpGet; import org.puredevteam.httpclient.HttpPost; import org.puredevteam.httpclient.HttpRequest; import org.puredevteam.httpclient.HttpResponse; import org.puredevteam.httpclient.IndividualConnectionHttpClient; import org.puredevteam.httpclient.KeepAliveHttpClientFactory; import org.puredevteam.httpclient.LocalIOException; import org.puredevteam.httpclient.RemoteIOException; import org.puredevteam.httpclient.SocketConnectionData; import org.puredevteam.httpclient.TransferProgressListener; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.Map; import java.util.TreeMap; public class RealJudgeHttpClient implements JudgeHttpClient { @Deprecated public static JudgeHttpClient wrap(CookieKeepingHttpClient client) { return new RealJudgeHttpClient(client); } public static JudgeHttpClient create(SocketConnectionData conn) { return new RealJudgeHttpClient(conn); } private static final CookieKeepingHttpClientFactory FACTORY = CookieKeepingHttpClientFactory.newInstance(KeepAliveHttpClientFactory.getInstance()); private CookieKeepingHttpClient client; private RealJudgeHttpClient(CookieKeepingHttpClient client) { this.client = client; } private RealJudgeHttpClient(SocketConnectionData site) { this.client = FACTORY.create(site, "1.1", 10 * 1000, 30 * 1000); } @Override public void clearCookie() { client.clearCookie(); } @Override public String receivePostBodyString(String path, Map<String, String> param, String encoding) throws IOException, JudgeServiceException { HttpRequest req = HttpPost.createWithFormContent(path, new TreeMap<String, String>(), param, encoding); HttpResponse res = receive(req); if (isErrorCode(res.code)) throw new JudgeServiceException(res.code + ":POST:" + path); return extractBody(res, encoding); } private HttpResponse receive(HttpRequest req) throws LocalIOException, RemoteIOException { try { return client.receive(req, TransferProgressListener.EMPTY_LISTENER, TransferProgressListener.EMPTY_LISTENER, EmptyCanceler.INSTANCE); } catch (CanceledException e) { throw new RuntimeException(e); } } @Override public String receiveGetBodyString(String path, String encoding) throws IOException, JudgeServiceException { HttpRequest req = HttpGet.create(path, new TreeMap<String, String>(), new TreeMap<String, String>(), encoding); HttpResponse res = receive(req); if (isErrorCode(res.code)) throw new JudgeServiceException(res.code + ":GET:" + path); return extractBody(res, encoding); } private boolean isErrorCode(int code) { return code != 200 && code != 302; } private String extractBody(HttpResponse r, String encoding) throws UnsupportedEncodingException { return new String(r.responseBytes, encoding); } }