java/com.sap.sailing.aiagent/src/com/sap/sailing/aiagent/impl/AIAgentImpl.java
... ...
@@ -138,6 +138,10 @@ public class AIAgentImpl implements AIAgent {
138 138
chatSession = createChatSession();
139 139
} catch (UnsupportedOperationException | URISyntaxException | IOException | ParseException e) {
140 140
throw new RuntimeException(e);
141
+ } catch (SecurityException e) {
142
+ aiCore.setCredentials(null);
143
+ logger.warning("Invalid credentials; clearing (setting to null).");
144
+ throw e;
141 145
}
142 146
} else {
143 147
chatSession = null;
java/com.sap.sse.aicore.test/src/com/sap/sse/aicore/impl/ReadCredentialsTest.java
... ...
@@ -22,8 +22,8 @@ public class ReadCredentialsTest {
22 22
try {
23 23
((CredentialsImpl) c).fetchToken();
24 24
fail("Expected an unauthorized (401) error code");
25
- } catch (IOException e) {
26
- assertTrue(e.getMessage().contains("401")); // expected
25
+ } catch (SecurityException e) {
26
+ assertTrue(e.getMessage().contains("Authentication failed: Unauthorized")); // expected
27 27
}
28 28
}
29 29
}
java/com.sap.sse.aicore/src/com/sap/sse/aicore/impl/AICoreImpl.java
... ...
@@ -4,6 +4,7 @@ import java.io.IOException;
4 4
import java.io.InputStreamReader;
5 5
import java.net.URISyntaxException;
6 6
import java.net.URL;
7
+import java.security.AccessControlException;
7 8
import java.util.ArrayList;
8 9
import java.util.List;
9 10
import java.util.Optional;
... ...
@@ -123,8 +124,14 @@ public class AICoreImpl implements AICore {
123 124
public JSONObject getJSONResponse(HttpUriRequest request) throws UnsupportedOperationException, ClientProtocolException, URISyntaxException, IOException, ParseException {
124 125
final CloseableHttpClient client = getHttpClient();
125 126
final HttpResponse response = client.execute(request);
126
- if (response.getStatusLine().getStatusCode() >= 400) {
127
- throw new IOException("Error fetching "+request.getRequestLine()+": ("+response.getStatusLine().getStatusCode()+") "+response.getStatusLine().getReasonPhrase());
127
+ final int statusCode = response.getStatusLine().getStatusCode();
128
+ if (statusCode == 401) {
129
+ throw new SecurityException("Authentication failed: "+response.getStatusLine().getReasonPhrase());
130
+ } else if (statusCode == 403) {
131
+ throw new AccessControlException("Authorization failed: " + response.getStatusLine().getReasonPhrase());
132
+ }
133
+ if (statusCode >= 400) {
134
+ throw new IOException("Error fetching "+request.getRequestLine()+": ("+statusCode+") "+response.getStatusLine().getReasonPhrase());
128 135
}
129 136
final JSONObject configurationsJson = (JSONObject) new JSONParser().parse(new InputStreamReader(response.getEntity().getContent()));
130 137
return configurationsJson;
java/com.sap.sse.aicore/src/com/sap/sse/aicore/impl/CredentialsImpl.java
... ...
@@ -6,6 +6,7 @@ import java.net.MalformedURLException;
6 6
import java.net.URI;
7 7
import java.net.URISyntaxException;
8 8
import java.net.URL;
9
+import java.security.AccessControlException;
9 10
import java.util.ArrayList;
10 11
import java.util.List;
11 12
... ...
@@ -119,8 +120,14 @@ public class CredentialsImpl implements Credentials {
119 120
.build();
120 121
final JSONParser jsonParser = new JSONParser();
121 122
final HttpResponse response = client.execute(postRequest);
122
- if (response.getStatusLine().getStatusCode() >= 400) {
123
- throw new IOException("Error obtaining client token: "+response.getStatusLine().getReasonPhrase()+" ("+response.getStatusLine().getStatusCode()+")");
123
+ final int statusCode = response.getStatusLine().getStatusCode();
124
+ if (statusCode == 401) {
125
+ throw new SecurityException("Authentication failed: "+response.getStatusLine().getReasonPhrase());
126
+ } else if (statusCode == 403) {
127
+ throw new AccessControlException("Authorization failed: " + response.getStatusLine().getReasonPhrase());
128
+ }
129
+ if (statusCode >= 400) {
130
+ throw new IOException("Error obtaining client token: "+response.getStatusLine().getReasonPhrase()+" ("+statusCode+")");
124 131
}
125 132
final JSONObject tokenJson = (JSONObject) jsonParser.parse(new InputStreamReader(response.getEntity().getContent()));
126 133
return (String) tokenJson.get(ACCESS_TOKEN);