package org.scribe.builder.api;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.scribe.exceptions.OAuthException;
import org.scribe.extractors.AccessTokenExtractor;
import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.scribe.utils.OAuthEncoder;
import org.scribe.utils.Preconditions;

public class EvernoteApi extends DefaultApi10a
{
  private static final String EVERNOTE_URL = "https://www.evernote.com";
  
  @Override
  public Verb getRequestTokenVerb()
  {
    return Verb.GET;
  }

	@Override
	public String getRequestTokenEndpoint()
  {
		return EVERNOTE_URL + "/oauth";
	}

	@Override
	public String getAccessTokenEndpoint()
	{
		return EVERNOTE_URL + "/oauth";
	}
	
	@Override
	public String getAuthorizationUrl(Token requestToken)
	{
	  return String.format(EVERNOTE_URL + "/OAuth.action?oauth_token=%s", requestToken.getToken());
	}
	
	@Override
	public AccessTokenExtractor getAccessTokenExtractor() {
	  return new EvernoteAccessTokenExtractor();
	}

	public static class Sandbox extends EvernoteApi
	{
	  private static final String SANDBOX_URL = "https://sandbox.evernote.com";

	  @Override
	  public String getRequestTokenEndpoint()
	  {
	    return SANDBOX_URL + "/oauth";
	  }

	  @Override
	  public String getAccessTokenEndpoint()
	  {
      return SANDBOX_URL + "/oauth";
	  }

	  @Override
	  public String getAuthorizationUrl(Token requestToken)
	  {
	    return String.format(SANDBOX_URL + "/OAuth.action?oauth_token=%s", requestToken.getToken());
	  }
	}
	
	public static class EvernoteAuthToken extends Token {
    private static final long serialVersionUID = 5913745981744917828L;

    private String noteStoreUrl;
	  private String webApiUrlPrefix;
	  private int userId;

	  public EvernoteAuthToken(String token, String secret,
	      String noteStoreUrl, String webApiUrlPrefix, int userId, String rawResponse) {
	    super(token, secret, rawResponse);
	    this.noteStoreUrl = noteStoreUrl;
	    this.webApiUrlPrefix = webApiUrlPrefix;
	    this.userId = userId;
	  }
	  
	  /**
	   * Get the Evernote web service NoteStore URL from the OAuth access token response.
	   */
	  public String getNoteStoreUrl() {
	    return noteStoreUrl;
	  }

	  /**
	   * Get the Evernote web API URL prefix from the OAuth access token response.
	   */
	  public String getWebApiUrlPrefix() {
	    return webApiUrlPrefix;
	  }

	  /**
	   * Get the numeric Evernote user ID from the OAuth access token response.
	   */
	  public int getUserId() {
	    return userId;
	  }
	}
	
	public static class EvernoteAccessTokenExtractor implements org.scribe.extractors.AccessTokenExtractor {
	  
	  private static final Pattern TOKEN_REGEX = Pattern.compile("oauth_token=([^&]+)");
	  private static final Pattern SECRET_REGEX = Pattern.compile("oauth_token_secret=([^&]*)");
	  private static final Pattern NOTESTORE_REGEX = Pattern.compile("edam_noteStoreUrl=([^&]+)");
	  private static final Pattern WEBAPI_REGEX = Pattern.compile("edam_webApiUrlPrefix=([^&]+)");
	  private static final Pattern USERID_REGEX = Pattern.compile("edam_userId=([^&]+)");
	  
	  /**
	   * {@inheritDoc} 
	   */
	  public Token extract(String response)
	  {
	    Preconditions.checkEmptyString(response, "Response body is incorrect. " +
	        "Can't extract a token from an empty string");
	    return new EvernoteAuthToken(extract(response, TOKEN_REGEX), 
	        extract(response, SECRET_REGEX), 
	        extract(response, NOTESTORE_REGEX), 
	        extract(response, WEBAPI_REGEX), 
	        Integer.parseInt(extract(response, USERID_REGEX)), 
	        response);
	  }
	  
	  private String extract(String response, Pattern p)
	  {
	    Matcher matcher = p.matcher(response);
	    if (matcher.find() && matcher.groupCount() >= 1)
	    {
	      return OAuthEncoder.decode(matcher.group(1));
	    }
	    else
	    {
	      throw new OAuthException("Response body is incorrect. " +
	          "Can't extract token and secret from this: '" + response + "'", null);
	    }
	  }
	}
	
}

@@ -222,38 +222,8 @@
 en;%0A
-import org.scribe.model.Verb;%0A
 impo
@@ -425,86 +425,8 @@
 %0A  %0A
-  @Override%0A  public Verb getRequestTokenVerb()%0A  %7B%0A    return Verb.GET;%0A  %7D%0A%0A
 %09@Ov
@@ -1418,1019 +1418,8 @@
 %0A%09%7D%0A
-%09%0A%09public static class EvernoteAuthToken extends Token %7B%0A    private static final long serialVersionUID = 5913745981744917828L;%0A%0A    private String noteStoreUrl;%0A%09  private String webApiUrlPrefix;%0A%09  private int userId;%0A%0A%09  public EvernoteAuthToken(String token, String secret,%0A%09      String noteStoreUrl, String webApiUrlPrefix, int userId, String rawResponse) %7B%0A%09    super(token, secret, rawResponse);%0A%09    this.noteStoreUrl = noteStoreUrl;%0A%09    this.webApiUrlPrefix = webApiUrlPrefix;%0A%09    this.userId = userId;%0A%09  %7D%0A%09  %0A%09  /**%0A%09   * Get the Evernote web service NoteStore URL from the OAuth access token response.%0A%09   */%0A%09  public String getNoteStoreUrl() %7B%0A%09    return noteStoreUrl;%0A%09  %7D%0A%0A%09  /**%0A%09   * Get the Evernote web API URL prefix from the OAuth access token response.%0A%09   */%0A%09  public String getWebApiUrlPrefix() %7B%0A%09    return webApiUrlPrefix;%0A%09  %7D%0A%0A%09  /**%0A%09   * Get the numeric Evernote user ID from the OAuth access token response.%0A%09   */%0A%09  public int getUserId() %7B%0A%09    return userId;%0A%09  %7D%0A%09%7D%0A%09
 %0A%09pu
@@ -1476,38 +1476,16 @@
 lements 
-org.scribe.extractors.
 AccessTo
@@ -1581,32 +1581,111 @@
 oken=(%5B%5E&%5D+)%22);%0A
+%09  // Evernote access tokens include an empty token secret (the empty string).%0A
 %09  private stati
@@ -1766,287 +1766,8 @@
 %22);%0A
-%09  private static final Pattern NOTESTORE_REGEX = Pattern.compile(%22edam_noteStoreUrl=(%5B%5E&%5D+)%22);%0A%09  private static final Pattern WEBAPI_REGEX = Pattern.compile(%22edam_webApiUrlPrefix=(%5B%5E&%5D+)%22);%0A%09  private static final Pattern USERID_REGEX = Pattern.compile(%22edam_userId=(%5B%5E&%5D+)%22);%0A
 %09  %0A
@@ -2002,20 +2002,8 @@
 new 
-EvernoteAuth
 Toke
@@ -2032,34 +2032,24 @@
 KEN_REGEX), 
-%0A%09        
 extract(resp
@@ -2073,168 +2073,8 @@
 X), 
-%0A%09        extract(response, NOTESTORE_REGEX), %0A%09        extract(response, WEBAPI_REGEX), %0A%09        Integer.parseInt(extract(response, USERID_REGEX)), %0A%09        
 resp

Remove EvernoteAuthToken