From 16f35e90bd40f2115adb3eff0736b85cd56b5f05 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Francesco=20Chicchiricc=C3=B2?= <--global>
Date: Mon, 28 Jul 2014 12:31:26 +0200
Subject: [PATCH] [OLINGO-381] provided samples for advanced HTTP client-side
handling; the submodule is not included in olingo-parent, being not meant for
any distribution nor artifact, but only as reference
---
samples/client/pom.xml | 46 ++++
.../http/AzureADOAuth2HttpClientFactory.java | 217 ++++++++++++++++++
.../core/http/CookieHttpClientFactory.java | 54 +++++
.../CustomConnectionsHttpClientFactory.java | 123 ++++++++++
.../http/ParametersHttpClientFactory.java | 53 +++++
.../http/ParametersHttpUriRequestFactory.java | 53 +++++
.../ProtocolInterceptorHttpClientFactory.java | 75 ++++++
.../http/RequestRetryHttpClientFactory.java | 86 +++++++
.../http/SocketFactoryHttpClientFactory.java | 77 +++++++
.../core/http/StatefulHttpClientFactory.java | 57 +++++
10 files changed, 841 insertions(+)
create mode 100644 samples/client/pom.xml
create mode 100644 samples/client/src/main/java/org/apache/olingo/samples/client/core/http/AzureADOAuth2HttpClientFactory.java
create mode 100644 samples/client/src/main/java/org/apache/olingo/samples/client/core/http/CookieHttpClientFactory.java
create mode 100644 samples/client/src/main/java/org/apache/olingo/samples/client/core/http/CustomConnectionsHttpClientFactory.java
create mode 100644 samples/client/src/main/java/org/apache/olingo/samples/client/core/http/ParametersHttpClientFactory.java
create mode 100644 samples/client/src/main/java/org/apache/olingo/samples/client/core/http/ParametersHttpUriRequestFactory.java
create mode 100644 samples/client/src/main/java/org/apache/olingo/samples/client/core/http/ProtocolInterceptorHttpClientFactory.java
create mode 100644 samples/client/src/main/java/org/apache/olingo/samples/client/core/http/RequestRetryHttpClientFactory.java
create mode 100644 samples/client/src/main/java/org/apache/olingo/samples/client/core/http/SocketFactoryHttpClientFactory.java
create mode 100644 samples/client/src/main/java/org/apache/olingo/samples/client/core/http/StatefulHttpClientFactory.java
diff --git a/samples/client/pom.xml b/samples/client/pom.xml
new file mode 100644
index 000000000..87d64fe83
--- /dev/null
+++ b/samples/client/pom.xml
@@ -0,0 +1,46 @@
+
+
+
+
+ 4.0.0
+
+ olingo-client-samples
+ jar
+ ${project.artifactId}
+ Olingo client customization samples.
+
+
+ org.apache.olingo
+ olingo-parent
+ 0.1.0-SNAPSHOT
+ ../..
+
+
+
+
+ org.apache.olingo
+ olingo-client-proxy
+ ${project.version}
+
+
+
+
diff --git a/samples/client/src/main/java/org/apache/olingo/samples/client/core/http/AzureADOAuth2HttpClientFactory.java b/samples/client/src/main/java/org/apache/olingo/samples/client/core/http/AzureADOAuth2HttpClientFactory.java
new file mode 100644
index 000000000..e281bd9be
--- /dev/null
+++ b/samples/client/src/main/java/org/apache/olingo/samples/client/core/http/AzureADOAuth2HttpClientFactory.java
@@ -0,0 +1,217 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.olingo.samples.client.core.http;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.http.Header;
+import org.apache.http.HttpException;
+import org.apache.http.HttpHeaders;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpRequestInterceptor;
+import org.apache.http.HttpResponse;
+import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.client.entity.UrlEncodedFormEntity;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.utils.URIBuilder;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.message.BasicNameValuePair;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.util.EntityUtils;
+import org.apache.olingo.client.core.http.AbstractOAuth2HttpClientFactory;
+import org.apache.olingo.client.core.http.OAuth2Exception;
+
+/**
+ * Shows how to work with OAuth 2.0 native applications protected by Azure Active Directory.
+ * More information.
+ */
+public class AzureADOAuth2HttpClientFactory extends AbstractOAuth2HttpClientFactory {
+
+ private final String clientId;
+
+ private final String redirectURI;
+
+ private final String resourceURI;
+
+ private final UsernamePasswordCredentials creds;
+
+ private ObjectNode token;
+
+ public AzureADOAuth2HttpClientFactory(final String authority, final String clientId,
+ final String redirectURI, final String resourceURI, final UsernamePasswordCredentials creds) {
+
+ super(URI.create(authority + "/oauth2/authorize"), URI.create(authority + "/oauth2/token"));
+ this.clientId = clientId;
+ this.redirectURI = redirectURI;
+ this.resourceURI = resourceURI;
+ this.creds = creds;
+ }
+
+ @Override
+ protected boolean isInited() throws OAuth2Exception {
+ return token != null;
+ }
+
+ private void fetchAccessToken(final DefaultHttpClient httpClient, final List data) {
+ token = null;
+
+ InputStream tokenResponse = null;
+ try {
+ final HttpPost post = new HttpPost(oauth2TokenServiceURI);
+ post.setEntity(new UrlEncodedFormEntity(data, "UTF-8"));
+
+ final HttpResponse response = httpClient.execute(post);
+
+ tokenResponse = response.getEntity().getContent();
+ token = (ObjectNode) new ObjectMapper().readTree(tokenResponse);
+ } catch (Exception e) {
+ throw new OAuth2Exception(e);
+ } finally {
+ IOUtils.closeQuietly(tokenResponse);
+ }
+ }
+
+ @Override
+ protected void init() throws OAuth2Exception {
+ final DefaultHttpClient httpClient = wrapped.create(null, null);
+
+ // 1. access the OAuth2 grant service (with authentication)
+ String code = null;
+ try {
+ final URIBuilder builder = new URIBuilder(oauth2GrantServiceURI).
+ addParameter("response_type", "code").
+ addParameter("client_id", clientId).
+ addParameter("redirect_uri", redirectURI);
+
+ HttpResponse response = httpClient.execute(new HttpGet(builder.build()));
+
+ final String loginPage = EntityUtils.toString(response.getEntity());
+
+ String postURL = StringUtils.substringBefore(
+ StringUtils.substringAfter(loginPage, "