2727import javax .ws .rs .WebApplicationException ;
2828import javax .ws .rs .core .Context ;
2929import javax .ws .rs .core .Cookie ;
30+ import javax .ws .rs .core .HttpHeaders ;
3031import javax .ws .rs .core .MediaType ;
3132import javax .ws .rs .core .MultivaluedHashMap ;
3233import javax .ws .rs .core .MultivaluedMap ;
@@ -144,16 +145,16 @@ public Response create(@CookieParam(value = "user") Cookie userc,
144145
145146 @ GET
146147 @ Path ("{sessionToken}" )
147- public Response get (@ PathParam ("sessionToken" ) String sessionToken , @ Context UriInfo uriInfo )
148- throws Exception {
148+ public Response get (@ PathParam ("sessionToken" ) String sessionToken , @ Context UriInfo uriInfo ,
149+ @ Context HttpHeaders headers ) throws Exception {
149150 MultivaluedMap <String , String > formParams = new MultivaluedHashMap <String , String >();
150- return post (sessionToken , uriInfo , formParams );
151+ return post (sessionToken , uriInfo , formParams , headers );
151152 }
152153
153154 @ POST
154155 @ Path ("{sessionToken}" )
155156 public Response post (@ PathParam ("sessionToken" ) String sessionToken , @ Context UriInfo uriInfo ,
156- MultivaluedMap <String , String > formParams ) throws Exception {
157+ MultivaluedMap <String , String > formParams , @ Context HttpHeaders headers ) throws Exception {
157158
158159 String username = "" ;
159160 try {
@@ -164,17 +165,11 @@ public Response post(@PathParam("sessionToken") String sessionToken, @Context Ur
164165 return Response .status (Response .Status .INTERNAL_SERVER_ERROR ).entity (e .getMessage ()).build ();
165166 }
166167
168+
167169 log .debug ("user:" + username + "-" );
168170 if (username .equals ("" ))
169171 return Response .status (Response .Status .NOT_FOUND ).build ();
170172
171- // ObjectPair<String, String> rdfStoreUser =
172- // frameworkUserManager.getRdfStoreUser(username);
173-
174- // create a context with credentials
175- // UsernamePasswordCredentials credentials = new
176- // UsernamePasswordCredentials(rdfStoreUser
177- // .getFirst(), rdfStoreUser.getSecond());
178173 UsernamePasswordCredentials credentials =
179174 new UsernamePasswordCredentials (FrameworkConfiguration .getInstance ()
180175 .getWorkbenchSystemAdmin (), FrameworkConfiguration .getInstance ()
@@ -185,6 +180,17 @@ public Response post(@PathParam("sessionToken") String sessionToken, @Context Ur
185180 context .setCredentialsProvider (credsProvider );
186181 // create post method and set parameters
187182 HttpPost proxyMethod = new HttpPost (endpoint );
183+
184+ MultivaluedMap <String , String > requestHeaders = headers .getRequestHeaders ();
185+
186+ log .debug ("REQUEST HEADERS" );
187+ for (Entry <String , List <String >> entity : requestHeaders .entrySet ()) {
188+ log .debug (entity .getKey () + " -> " + entity .getValue ());
189+ if (entity .getKey ().equals ("cookie" ) || entity .getKey ().equals ("content-length" ))
190+ continue ;
191+ proxyMethod .addHeader (entity .getKey (), entity .getValue ().get (0 ));
192+ }
193+
188194 ArrayList <NameValuePair > postParameters = new ArrayList <NameValuePair >();
189195 // when is GET we extract query params using uriInfo
190196 for (Entry <String , List <String >> entity : uriInfo .getQueryParameters ().entrySet ())
@@ -196,21 +202,28 @@ public Response post(@PathParam("sessionToken") String sessionToken, @Context Ur
196202 proxyMethod .setEntity (new UrlEncodedFormEntity (postParameters , "UTF-8" ));
197203 // create the httpclient and reads/wirtes repsponse
198204 CloseableHttpClient httpClient = HttpClients .createDefault ();
199- final CloseableHttpResponse response = httpClient .execute (proxyMethod , context );
200- StreamingOutput stream = new StreamingOutput () {
201- @ Override
202- public void write (OutputStream os ) throws IOException , WebApplicationException {
203- // TODO Auto-generated method stub
204- Writer writer = new BufferedWriter (new OutputStreamWriter (os ));
205- int b ;
206- while ((b = response .getEntity ().getContent ().read ()) != -1 ) {
207- writer .write (b );
205+
206+ try {
207+ final CloseableHttpResponse response = httpClient .execute (proxyMethod , context );
208+ StreamingOutput stream = new StreamingOutput () {
209+ @ Override
210+ public void write (OutputStream os ) throws IOException , WebApplicationException {
211+ // TODO Auto-generated method stub
212+ Writer writer = new BufferedWriter (new OutputStreamWriter (os ));
213+ int b ;
214+ while ((b = response .getEntity ().getContent ().read ()) != -1 ) {
215+ writer .write (b );
216+ }
217+ writer .flush ();
208218 }
209- writer .flush ();
210- }
211- };
219+ };
220+ return Response .ok (stream ).build ();
221+ } catch (Exception e ) {
222+ e .printStackTrace ();
223+ return Response .status (Response .Status .INTERNAL_SERVER_ERROR ).entity (e .getMessage ()).build ();
224+ }
212225 // retuns response
213- return Response . ok ( stream ). build ();
226+
214227
215228 }
216229
0 commit comments