IDEA-118070 Clear cookies when error occurred and for test connection

This commit is contained in:
Mikhail Golubev
2014-09-02 18:13:50 +04:00
parent 1b298195ce
commit bafb91855a
@@ -12,7 +12,6 @@ import com.intellij.tasks.Task;
import com.intellij.tasks.TaskBundle;
import com.intellij.tasks.TaskState;
import com.intellij.tasks.impl.BaseRepositoryImpl;
import com.intellij.tasks.impl.TaskUtil;
import com.intellij.tasks.impl.gson.GsonUtil;
import com.intellij.tasks.jira.rest.JiraRestApi;
import com.intellij.tasks.jira.soap.JiraLegacyApi;
@@ -52,6 +51,7 @@ public class JiraRepository extends BaseRepositoryImpl {
private static final boolean REDISCOVER_API = Boolean.getBoolean("tasks.jira.rediscover.api");
public static final Pattern JIRA_ID_PATTERN = Pattern.compile("\\p{javaUpperCase}+-\\d+");
public static final String AUTH_COOKIE_NAME = "JSESSIONID";
/**
* Default JQL query
@@ -145,12 +145,13 @@ public class JiraRepository extends BaseRepositoryImpl {
@Nullable
@Override
public CancellableConnection createCancellableConnection() {
clearCookies();
// TODO cancellable connection for XML_RPC?
return new CancellableConnection() {
@Override
protected void doTest() throws Exception {
ensureApiVersionDiscovered();
myApiVersion.findTasks("", 1);
myApiVersion.findTasks(mySearchQuery, 1);
}
@Override
@@ -231,15 +232,8 @@ public class JiraRepository extends BaseRepositoryImpl {
setUseHttpAuthentication(true);
}
else {
boolean cookieAuthenticated = false;
for (Cookie cookie : client.getState().getCookies()) {
if (cookie.getName().equals("JSESSIONID") && !cookie.isExpired()) {
cookieAuthenticated = true;
break;
}
}
// disable subsequent basic authorization attempts if user already was authenticated
boolean enableBasicAuthentication = !(isRestApiSupported() && cookieAuthenticated);
boolean enableBasicAuthentication = !(isRestApiSupported() && containsCookie(client, AUTH_COOKIE_NAME));
if (enableBasicAuthentication != isUseHttpAuthentication()) {
LOG.info("Basic authentication for subsequent requests was " + (enableBasicAuthentication ? "enabled" : "disabled"));
}
@@ -251,14 +245,15 @@ public class JiraRepository extends BaseRepositoryImpl {
// may be null if 204 No Content received
final InputStream stream = method.getResponseBodyAsStream();
String entityContent = stream == null ? "" : StreamUtil.readText(stream, CharsetToolkit.UTF8);
TaskUtil.prettyFormatJsonToLog(LOG, entityContent);
//TaskUtil.prettyFormatJsonToLog(LOG, entityContent);
// besides SC_OK, can also be SC_NO_CONTENT in issue transition requests
// see: JiraRestApi#setTaskStatus
//if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_NO_CONTENT) {
if (statusCode >= 200 && statusCode < 300) {
return entityContent;
}
else if (method.getResponseHeader("Content-Type") != null) {
clearCookies();
if (method.getResponseHeader("Content-Type") != null) {
Header header = method.getResponseHeader("Content-Type");
if (header.getValue().startsWith("application/json")) {
JsonObject object = GSON.fromJson(entityContent, JsonObject.class);
@@ -284,6 +279,19 @@ public class JiraRepository extends BaseRepositoryImpl {
throw new Exception(TaskBundle.message("failure.http.error", statusCode, statusText));
}
private static boolean containsCookie(@NotNull HttpClient client, @NotNull String cookieName) {
for (Cookie cookie : client.getState().getCookies()) {
if (cookie.getName().equals(cookieName) && !cookie.isExpired()) {
return true;
}
}
return false;
}
private void clearCookies() {
getHttpClient().getState().clearCookies();
}
// Made public for SOAP API compatibility
@Override
public HttpClient getHttpClient() {