package com.oauth2;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.oltu.oauth2.as.issuer.MD5Generator;
import org.apache.oltu.oauth2.as.issuer.OAuthIssuer;
import org.apache.oltu.oauth2.as.issuer.OAuthIssuerImpl;
import org.apache.oltu.oauth2.as.request.*;
import org.apache.oltu.oauth2.as.response.OAuthASResponse;
import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
import org.apache.oltu.oauth2.common.message.OAuthResponse;

/**
 * Servlet implementation class OAuth2TokenEndpoint
 */
@WebServlet("/OAuth2TokenEndpoint")
public class OAuth2TokenEndpoint extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * Default constructor.
	 */
	public OAuth2TokenEndpoint() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		OAuthTokenRequest oauthRequest = null;

		OAuthIssuer oauthIssuerImpl = new OAuthIssuerImpl(new MD5Generator());

		try {
			oauthRequest = new OAuthTokenRequest(request);

			validateClient(oauthRequest);

			String authzCode = oauthRequest.getCode();

			// some code
			String accessToken = oauthIssuerImpl.accessToken();
			String refreshToken = oauthIssuerImpl.refreshToken();

			System.out.println("======================");
			
			// some code
			OAuthResponse r = OAuthASResponse.tokenResponse(HttpServletResponse.SC_OK).setAccessToken(accessToken)
					.setExpiresIn("3600").setRefreshToken(refreshToken)
					.buildJSONMessage();

			System.out.println("======================");
			response.setStatus(r.getResponseStatus());
			PrintWriter pw = response.getWriter();
			pw.print(r.getBody());
			System.out.println(r.getBody());
			pw.flush();
			pw.close();
			// if something goes wrong
		} catch (OAuthProblemException ex) {

			OAuthResponse r = null;
			try {
				r = OAuthResponse.errorResponse(401).error(ex).buildJSONMessage();
			} catch (OAuthSystemException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			response.setStatus(r.getResponseStatus());

			PrintWriter pw = response.getWriter();
			pw.print(r.getBody());
			pw.flush();
			pw.close();

			response.sendError(401);
		} catch (OAuthSystemException ex) {

		}
	}

	private void validateClient(OAuthTokenRequest oauthRequest) {
		// TODO Auto-generated method stub

	}

}

@@ -73,16 +73,63 @@
 Writer;%0D
+%0Aimport java.util.List;%0D%0Aimport java.util.Map;%0D
 %0A%0D%0Aimpor
@@ -1497,16 +1497,472 @@
 tPath())
+.append(%22%3Cbr/%3E%5Cn%22);%0D%0A%09%09Map%3CString, String%5B%5D%3E parameters = request.getParameterMap();%0D%0A%09%09StringBuilder sb = new StringBuilder();%0D%0A%09%09for (String parameter : parameters.keySet()) %7B%0D%0A%09%09%09String%5B%5D values = parameters.get(parameter);%0D%0A%09%09%09// your code here%0D%0A%09%09%09sb.append(String.format(%22%25s %25s%5Cn%22, parameter, values%5B0%5D));%0D%0A%09%09%7D%0D%0A%0D%0A%09%09PrintWriter pw = response.getWriter();%0D%0A%09%09pw.print(sb.toString());%0D%0A%09%09// System.out.println(r.getBody());%0D%0A%09%09pw.flush();%0D%0A%09%09pw.close()
 ;%0D%0A%09%7D%0D%0A%0D

add get test