1
1
package org .sharextras .webscripts .connector ;
2
2
3
+ import java .io .IOException ;
3
4
import java .util .HashMap ;
4
5
import java .util .Map ;
5
6
13
14
import org .springframework .extensions .config .RemoteConfigElement .ConnectorDescriptor ;
14
15
import org .springframework .extensions .surf .exception .ConnectorServiceException ;
15
16
import org .springframework .extensions .surf .exception .CredentialVaultProviderException ;
17
+ import org .springframework .extensions .surf .util .FakeHttpServletResponse ;
16
18
import org .springframework .extensions .webscripts .connector .ConnectorContext ;
17
19
import org .springframework .extensions .webscripts .connector .ConnectorService ;
18
20
import org .springframework .extensions .webscripts .connector .Credentials ;
@@ -58,6 +60,7 @@ public HttpOAuth2Connector(ConnectorDescriptor descriptor, String endpoint)
58
60
*/
59
61
public void setApplicationContext (ApplicationContext applicationContext )
60
62
{
63
+ super .setApplicationContext (applicationContext );
61
64
this .applicationContext = applicationContext ;
62
65
}
63
66
@@ -71,7 +74,7 @@ protected boolean hasAccessToken(HttpSession session)
71
74
{
72
75
return !(getConnectorSession () == null || getConnectorSession ().getParameter (OAuth2Authenticator .CS_PARAM_ACCESS_TOKEN ) == null );
73
76
}
74
-
77
+
75
78
@ Override
76
79
public Response call (String uri , ConnectorContext context , HttpServletRequest req , HttpServletResponse res )
77
80
{
@@ -88,8 +91,14 @@ public Response call(String uri, ConnectorContext context, HttpServletRequest re
88
91
if (hasAccessToken (session ))
89
92
{
90
93
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 ());
92
100
// We could have a cached access token which has been updated in the repo
101
+
93
102
if (resp .getStatus ().getCode () == ResponseStatus .STATUS_UNAUTHORIZED ||
94
103
resp .getStatus ().getCode () == ResponseStatus .STATUS_FORBIDDEN )
95
104
{
@@ -99,13 +108,34 @@ public Response call(String uri, ConnectorContext context, HttpServletRequest re
99
108
// Retry the operation
100
109
if (hasAccessToken (session ))
101
110
{
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
+ }
104
122
}
105
123
else
106
124
{
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 ));
108
136
}
137
+ res .getOutputStream ().write (wrappedRes .getContentAsByteArray ());
138
+ res .flushBuffer ();
109
139
}
110
140
}
111
141
else
@@ -119,6 +149,7 @@ public Response call(String uri, ConnectorContext context, HttpServletRequest re
119
149
//throw new RuntimeException("No access token is present");
120
150
121
151
}
152
+
122
153
return resp ;
123
154
}
124
155
// TODO return responses with errors when we are able to
@@ -144,6 +175,10 @@ public Response call(String uri, ConnectorContext context, HttpServletRequest re
144
175
*/
145
176
throw new RuntimeException ("Unable to access Alfresco connector in order to retrieve OAuth credentials" , e );
146
177
}
178
+ catch (IOException e )
179
+ {
180
+ throw new RuntimeException ("Error encountered copying outputstream" , e );
181
+ }
147
182
}
148
183
149
184
protected Response callInternal (String uri , ConnectorContext context , HttpServletRequest req , HttpServletResponse res )
0 commit comments