Skip to content

Commit 7626f9c

Browse files
committed
OAuth 2.0 connector fixes
- Retrying requests after an initial 401/403 response now works - Application context is now always set correctly on the connector instance
1 parent 7d93705 commit 7626f9c

File tree

1 file changed

+40
-5
lines changed

1 file changed

+40
-5
lines changed

src/main/java/org/sharextras/webscripts/connector/HttpOAuth2Connector.java

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.sharextras.webscripts.connector;
22

3+
import java.io.IOException;
34
import java.util.HashMap;
45
import java.util.Map;
56

@@ -13,6 +14,7 @@
1314
import org.springframework.extensions.config.RemoteConfigElement.ConnectorDescriptor;
1415
import org.springframework.extensions.surf.exception.ConnectorServiceException;
1516
import org.springframework.extensions.surf.exception.CredentialVaultProviderException;
17+
import org.springframework.extensions.surf.util.FakeHttpServletResponse;
1618
import org.springframework.extensions.webscripts.connector.ConnectorContext;
1719
import org.springframework.extensions.webscripts.connector.ConnectorService;
1820
import org.springframework.extensions.webscripts.connector.Credentials;
@@ -58,6 +60,7 @@ public HttpOAuth2Connector(ConnectorDescriptor descriptor, String endpoint)
5860
*/
5961
public void setApplicationContext(ApplicationContext applicationContext)
6062
{
63+
super.setApplicationContext(applicationContext);
6164
this.applicationContext = applicationContext;
6265
}
6366

@@ -71,7 +74,7 @@ protected boolean hasAccessToken(HttpSession session)
7174
{
7275
return !(getConnectorSession() == null || getConnectorSession().getParameter(OAuth2Authenticator.CS_PARAM_ACCESS_TOKEN) == null);
7376
}
74-
77+
7578
@Override
7679
public Response call(String uri, ConnectorContext context, HttpServletRequest req, HttpServletResponse res)
7780
{
@@ -88,8 +91,14 @@ public Response call(String uri, ConnectorContext context, HttpServletRequest re
8891
if (hasAccessToken(session))
8992
{
9093
context.setCommitResponseOnAuthenticationError(false);
91-
resp = callInternal(uri, context, req, res);
94+
95+
// Wrap the response object, since it gets committed straight away
96+
FakeHttpServletResponse wrappedRes = new FakeHttpServletResponse(res);
97+
resp = callInternal(uri, context, req, wrappedRes);
98+
if (logger.isDebugEnabled())
99+
logger.debug("Response status " + resp.getStatus().getCode() + " " + resp.getStatus().getCodeName());
92100
// We could have a cached access token which has been updated in the repo
101+
93102
if (resp.getStatus().getCode() == ResponseStatus.STATUS_UNAUTHORIZED ||
94103
resp.getStatus().getCode() == ResponseStatus.STATUS_FORBIDDEN)
95104
{
@@ -99,13 +108,34 @@ public Response call(String uri, ConnectorContext context, HttpServletRequest re
99108
// Retry the operation
100109
if (hasAccessToken(session))
101110
{
102-
context.setCommitResponseOnAuthenticationError(false);
103-
resp = callInternal(uri, context, req, res);
111+
context.setCommitResponseOnAuthenticationError(true);
112+
try
113+
{
114+
resp = callInternal(uri, context, req, res);
115+
if (logger.isDebugEnabled())
116+
logger.debug("Response status " + resp.getStatus().getCode() + " " + resp.getStatus().getCodeName());
117+
}
118+
catch (Throwable t)
119+
{
120+
logger.error("Encountered error when attempting to reload", t);
121+
}
104122
}
105123
else
106124
{
107-
// TODO What to do here?
125+
throw new RuntimeException("No access token is present");
126+
}
127+
}
128+
else
129+
{
130+
res.setStatus(wrappedRes.getStatus());
131+
res.setCharacterEncoding(wrappedRes.getCharacterEncoding());
132+
// Copy headers over
133+
for (Object hdrname : wrappedRes.getHeaderNames())
134+
{
135+
res.setHeader((String) hdrname, (String) wrappedRes.getHeader((String) hdrname));
108136
}
137+
res.getOutputStream().write(wrappedRes.getContentAsByteArray());
138+
res.flushBuffer();
109139
}
110140
}
111141
else
@@ -119,6 +149,7 @@ public Response call(String uri, ConnectorContext context, HttpServletRequest re
119149
//throw new RuntimeException("No access token is present");
120150

121151
}
152+
122153
return resp;
123154
}
124155
// TODO return responses with errors when we are able to
@@ -144,6 +175,10 @@ public Response call(String uri, ConnectorContext context, HttpServletRequest re
144175
*/
145176
throw new RuntimeException("Unable to access Alfresco connector in order to retrieve OAuth credentials", e);
146177
}
178+
catch (IOException e)
179+
{
180+
throw new RuntimeException("Error encountered copying outputstream", e);
181+
}
147182
}
148183

149184
protected Response callInternal(String uri, ConnectorContext context, HttpServletRequest req, HttpServletResponse res)

0 commit comments

Comments
 (0)