3
votes

I am developing OData client with Apache olingo, credentials contains native characters and should be read in UTF-8 for Base64 encoding of "Authorization" header. The first way to go was standard suggested by Olingo:

EdmEnabledODataClient client = ODataClientFactory.getEdmEnabledClient(endpointURI, ContentType.JSON);
client.getConfiguration().setHttpClientFactory(new BasicAuthHttpClientFactory(username, password));

But that didn't not work for me, because Olingo reads "username" and "password" bytes in US-ASCII charset for Base64 encoding, and my username turns to ??? . On HTTP client level, there is a way to pass charset to org.apache.http.impl.auth.BasicSchemeFactory , but I found no way to customize it on Olingo level.

My second attempt was adding a raw header:

URI searchURI = client.newURIBuilder(endpointURI)
            .appendEntitySetSegment(segment)
            .top(10)
            .addQueryOption(QueryOption.FORMAT, "json")         
            .build();
    ODataEntitySetRequest<ClientEntitySet> request = client.getRetrieveRequestFactory().getEntitySetRequest(searchURI);
    String auth = Base64.encodeBase64String(String.format("%s:%s", username, password).getBytes(StandardCharsets.UTF_8));
            request.addCustomHeader("Authorization", "Basic "+ auth);
    ODataRetrieveResponse<ClientEntitySet> response = request.execute();

But it appears that Olingo actually sends 2 HTTP requests under request.execute call . First one is for data, it contains my header, passed authorization and returns data - fine. But then second request is for metadata, without Authorization header, and returns 401 Unauthorized. So the final result is exception. I need a way to add basic auth. that will work through full Olingo request cycle (multiple http requests, and use UTF-8 charset for my credentials. Or, disable metadata call somehow (may be not possible, if Olingo always uses it for response object construction)

1

1 Answers

4
votes

Found a solution, but still interested in recommended way by Olingo

            EdmEnabledODataClient client = ODataClientFactory.getEdmEnabledClient(endpointURI, ContentType.JSON);
            final String auth = Base64.encodeBase64String(String.format("%s:%s", username, password).getBytes(StandardCharsets.UTF_8));
            client.getConfiguration().setHttpClientFactory(new DefaultHttpClientFactory() {
                @SuppressWarnings("deprecation")
                @Override
                public DefaultHttpClient create(HttpMethod method, URI uri) {
                    final DefaultHttpClient client = super.create(method, uri);
                    client.addRequestInterceptor(new HttpRequestInterceptor() {
                       @Override
                       public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
                           request.addHeader("Authorization", "Basic " + auth);
                    }
                });
                return client;
            }
        });