Spring3 with Servlets

Spring3 with Servlets

Actually, it is not Servlets. It is simple servlet. It is org.springframework.web.HttpRequestHandler.

The sample project is easyoauthprovider. I will put some core codes and configuration here.

Spring configuration files:
servlet-context.xml
<bean id="RequestTokenServlet"  class="com.sillycat.easyoauthprovider.servlets.RequestTokenHttpRequestHandler" >
<property name="oauthProvider" ref="oauthProvider" />
<property name="oauthValidator" ref="oauthValidator" />
</bean>

The related oauthProvider and oauthValidator is configured in spring bean xml files.

The web configuration file web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:main-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
        <servlet-name>RequestTokenServlet</servlet-name>
        <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>RequestTokenServlet</servlet-name>
<url-pattern>/request_token</url-pattern>
</servlet-mapping>


The JAVA class RequestTokenHttpRequestHandler.java is as follow:
package com.sillycat.easyoauthprovider.servlets;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.oauth.OAuth;
import net.oauth.OAuthAccessor;
import net.oauth.OAuthConsumer;
import net.oauth.OAuthMessage;
import net.oauth.OAuthValidator;
import net.oauth.server.OAuthServlet;

import org.springframework.web.HttpRequestHandler;

import com.sillycat.easyoauthprovider.plugins.oauth.OAuthProvider;

public class RequestTokenHttpRequestHandler implements HttpRequestHandler {

private OAuthProvider oauthProvider;

private OAuthValidator oauthValidator;

public void setOauthProvider(OAuthProvider oauthProvider) {
this.oauthProvider = oauthProvider;
}

public void setOauthValidator(OAuthValidator oauthValidator) {
this.oauthValidator = oauthValidator;
}

public void handleRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}

public void processRequest(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {

try {
OAuthMessage requestMessage = OAuthServlet
.getMessage(request, null);

OAuthConsumer consumer = oauthProvider.getConsumer(requestMessage);

OAuthAccessor accessor = new OAuthAccessor(consumer);
oauthValidator.validateMessage(requestMessage, accessor);
{
// Support the 'Variable ACCESSOR Secret' extension
// described in http://oauth.pbwiki.com/AccessorSecret
String secret = requestMessage
.getParameter("oauth_accessor_secret");
if (secret != null) {
accessor.setProperty(OAuthConsumer.ACCESSOR_SECRET, secret);
}
}
// generate request_token and secret
oauthProvider.generateRequestToken(accessor);

response.setContentType("text/plain");
OutputStream out = response.getOutputStream();
OAuth.formEncode(OAuth.newList("oauth_token",
accessor.requestToken, "oauth_token_secret",
accessor.tokenSecret), out);
out.close();

} catch (Exception e) {
e.printStackTrace();
}

}

}

Frankly speaking, it is part of the oauth sample project. I am still working on this.

references:
http://andykayley.blogspot.com/2007/11/how-to-inject-spring-beans-into.html
http://andykayley.blogspot.com/2008/06/how-to-inject-spring-beans-into.html

你可能感兴趣的:(servlet)