Skip to content
This repository has been archived by the owner on Apr 5, 2022. It is now read-only.

Commit

Permalink
Initial steps toward breaking Spring Social into more cohesive submod…
Browse files Browse the repository at this point in the history
…ules
  • Loading branch information
habuma committed Dec 7, 2010
1 parent a00a4c2 commit e1e04bd
Show file tree
Hide file tree
Showing 34 changed files with 2,108 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
build
**/build
src/test/java/exploration
**/src/test/java/exploration
.gradle
spring-social-core/src/test/java/exploration
**/.classpath
Expand Down
48 changes: 45 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,58 @@ project('spring-social-core') {
description = 'Spring Social Core'
dependencies {
compile "org.springframework:spring-web:$springVersion"
compile "org.springframework:spring-jdbc:$springVersion"
}
}

project('spring-social-facebook') {
description = 'Spring Social Facebook'
dependencies {
compile "commons-httpclient:commons-httpclient:$commonsHttpClientVersion"
compile ("javax.servlet:servlet-api:$servletApiVersion") { provided = true }
compile ("org.springframework.security.oauth:spring-security-oauth:$s2OAuthVersion") { optional = true }
compile "org.codehaus.jackson:jackson-mapper-asl:$jacksonVersion"
compile project(':spring-social-core')
compile project(':spring-social-oauth')
}
}

project('spring-social-linkedin') {
description = 'Spring Social LinkedIn'
dependencies {
compile "commons-httpclient:commons-httpclient:$commonsHttpClientVersion"
compile project(':spring-social-core')
compile project(':spring-social-oauth')
}
}

project('spring-social-oauth') {
description = 'Spring Social OAuth'
dependencies {
compile "commons-httpclient:commons-httpclient:$commonsHttpClientVersion"
compile ("org.scribe:scribe:$scribeVersion") { optional = true }
compile "joda-time:joda-time:$jodaVersion"
compile ("org.springframework.security.oauth:spring-security-oauth:$s2OAuthVersion") { optional = true }
compile project(':spring-social-core')
}
}

project('spring-social-tripit') {
description = 'Spring Social TripIt'
dependencies {
compile "org.codehaus.jackson:jackson-mapper-asl:$jacksonVersion"
compile project(':spring-social-core')
compile project(':spring-social-oauth')
}
}

project('spring-social-twitter') {
description = 'Spring Social Twitter'
dependencies {
compile "org.codehaus.jackson:jackson-mapper-asl:$jacksonVersion"
compile project(':spring-social-core')
compile project(':spring-social-oauth')
}
}


// -----------------------------------------------------------------------------
// Configuration for the docs subproject
// -----------------------------------------------------------------------------
Expand Down
5 changes: 5 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ rootProject.name = 'spring-social'

include 'docs'
include 'spring-social-core'
include 'spring-social-facebook'
include 'spring-social-linkedin'
include 'spring-social-oauth'
include 'spring-social-tripit'
include 'spring-social-twitter'
13 changes: 13 additions & 0 deletions spring-social-oauth/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#
#Wed Nov 17 09:24:37 CST 2010
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.compliance=1.5
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.source=1.5
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2010 the original author or authors.
*
* Licensed 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.springframework.social.oauth;

import java.util.Map;

import org.springframework.http.client.ClientHttpRequest;

/**
* Strategy interface for signing a client request with the necessary
* information for it to be OAuth-authenticated. Implementations of this
* interface will vary for different versions of OAuth.
*
* @author Craig Walls
*/
public interface OAuthClientRequestSigner {
void sign(ClientHttpRequest request, Map<String, String> bodyParameters);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright 2010 the original author or authors.
*
* Licensed 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.springframework.social.oauth;

import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.client.AbstractClientHttpRequest;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpResponse;

/**
* Implementation of {@link ClientHttpRequest} that wraps another
* {@link ClientHttpRequest} and signs it with an OAuth
* <code>Authorization</code> header before it is sent.
*
* @author Craig Walls
*/
public class OAuthSigningClientHttpRequest extends AbstractClientHttpRequest {
private final ClientHttpRequest delegate;
private final OAuthClientRequestSigner signer;

/**
* Creates an {@link OAuthSigningClientHttpRequest}.
*
* @param delegate
* The wrapped {@link ClientHttpRequest} that is to be signed
* @param signer
* An OAuth signer
*/
public OAuthSigningClientHttpRequest(ClientHttpRequest delegate, OAuthClientRequestSigner signer) {
this.delegate = delegate;
this.signer = signer;
}

protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException {
Map<String, String> bodyParameters = extractBodyParameters(headers.getContentType(), bufferedOutput);
signer.sign(delegate, bodyParameters);
delegate.getBody().write(bufferedOutput);
delegate.getHeaders().putAll(headers);
return delegate.execute();
}

private Map<String, String> extractBodyParameters(MediaType bodyType, byte[] bodyBytes) {
Map<String, String> params = new HashMap<String, String>();

if (bodyType != null && bodyType.equals(MediaType.APPLICATION_FORM_URLENCODED)) {
String[] paramPairs = new String(bodyBytes).split("&");
for (String pair : paramPairs) {
String[] keyValue = pair.split("=");
if (keyValue.length == 2) {
params.put(keyValue[0], keyValue[1]);
}
}
}
return params;
}

public URI getURI() {
return delegate.getURI();
}

public HttpMethod getMethod() {
return delegate.getMethod();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2010 the original author or authors.
*
* Licensed 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.springframework.social.oauth;

import java.io.IOException;
import java.net.URI;

import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpRequestFactory;

/**
* An implementation of {@link ClientHttpRequestFactory} that creates an
* {@link OAuthSigningClientHttpRequest} so that the request can be signed with
* an OAuth <code>Authorization</code> header.
*
* @author Craig Walls
*/
public class OAuthSigningClientHttpRequestFactory implements ClientHttpRequestFactory {
private final OAuthClientRequestSigner signer;
private final ClientHttpRequestFactory delegate;

public OAuthSigningClientHttpRequestFactory(ClientHttpRequestFactory delegate, OAuthClientRequestSigner signer) {
this.delegate = delegate;
this.signer = signer;
}

public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
return new OAuthSigningClientHttpRequest(delegate.createRequest(uri, httpMethod), signer);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Core classes for signing requests with OAuth
*/
package org.springframework.social.oauth;
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2010 the original author or authors.
*
* Licensed 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.springframework.social.oauth1;

import org.springframework.social.core.SocialException;

/**
* @author Craig Walls
*/
public class MissingOAuthLibraryException extends SocialException {
private static final long serialVersionUID = 1L;

public MissingOAuthLibraryException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2010 the original author or authors.
*
* Licensed 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.springframework.social.oauth1;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLDecoder;
import java.util.Map;

import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.social.oauth.OAuthClientRequestSigner;

/**
* Abstract implementation of {@link OAuthClientRequestSigner} that adds an
* OAuth 1 Authorization header to the request. Concrete implementations will
* generate the Authorization header by implementing the
* buildAuthorizationHeader() method.
*
* @author Craig Walls
*/
public abstract class OAuth1ClientRequestSigner implements OAuthClientRequestSigner {

public void sign(ClientHttpRequest request, Map<String, String> bodyParameters) {
String authorizationHeader = buildAuthorizationHeader(request.getMethod(), request.getURI(), bodyParameters);
if (authorizationHeader != null) {
request.getHeaders().add("Authorization", authorizationHeader);
}
}

protected String decode(String encoded) {
try {
return URLDecoder.decode(encoded, "UTF-8");
} catch (UnsupportedEncodingException shouldntHappen) {
return encoded;
}
}

protected abstract String buildAuthorizationHeader(HttpMethod method, URI url, Map<String, String> parameters);
}
Loading

0 comments on commit e1e04bd

Please sign in to comment.