Commits

Eric Schlenz  committed 40c9bc2

Added SSL enabled calls for login/registration.

  • Participants
  • Parent commits 8730561
  • Branches release-1.4.2.1

Comments (0)

Files changed (9)

File MediaGetStrategyTest.launch

 <listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
 <listEntry value="1"/>
 </listAttribute>
+<mapAttribute key="org.eclipse.debug.core.preferred_launchers">
+<mapEntry key="[debug]" value="org.eclipse.jdt.junit.launchconfig"/>
+</mapAttribute>
 <stringAttribute key="org.eclipse.jdt.junit.CONTAINER" value=""/>
 <booleanAttribute key="org.eclipse.jdt.junit.KEEPRUNNING_ATTR" value="false"/>
 <stringAttribute key="org.eclipse.jdt.junit.TESTNAME" value=""/>

File core/src/com/photobucket/api/core/PhotobucketAPI.java

 	private String oauthConsumerSecret;
 	private Long oauthTimestamp;
     private String subdomain = "api.photobucket.com";
+    private boolean ssl;
     private String requestPath;
 	private Map<String, String> parameters = new HashMap<String, String>();
 	private String method;
 	    request.setMethod(getHttpMethod());
 	    request.setParameters(parameters);
 	    request.setSubdomain(getSubdomain());
+	    request.setSsl(isSsl());
 	    request.setRequestPath(getRequestPath());
 	    
 	    if ( includeFormat ) {
 	public void setSubdomain(String subdomain) {
 		this.subdomain = subdomain;
 	}
+	public boolean isSsl() {
+        return ssl;
+    }
+	public void setSsl(boolean ssl) {
+        this.ssl = ssl;
+    }
 	public String getRequestPath() {
 		return requestPath;
 	}

File core/src/com/photobucket/api/oauth/PhotobucketHttpOAuthConsumer.java

     		time = this.getOauthTimestamp().toString();
     	} else {
 			try {
-				URL timeUrl = new URL("http://api.photobucket.com/time");
+				URL timeUrl = new URL("https://api.photobucket.com:443/time");
 				BufferedReader reader = new BufferedReader(new InputStreamReader(timeUrl.openStream()));
 				time = reader.readLine();
 				reader.close();

File core/src/com/photobucket/api/rest/RESTfulRequest.java

     private String uploadFileName;
     private Integer uploadFileSize;
     private String subdomain = new String(PHOTOBUCKET_BASE_DOMAIN);
+    private boolean ssl = false;
    
     // Photobucket specific
     // TODO:  refactor so this class is more generic to REST
 	public void setSubdomain(String subdomain) {
 		this.subdomain = subdomain;
 	}
+	public boolean isSsl() {
+        return ssl;
+    }
+    public void setSsl(boolean ssl) {
+        this.ssl = ssl;
+    }
 	public String getRequestPath() {
 		return requestPath;
 	}
 	}
 	
 	public String getRequestUrl() {
-		return "http://"+getSubdomain()+getRequestPath();
+	    String sslPart = isSsl() ? "s" : "";
+		return "http" + sslPart + "://"+getSubdomain()+getRequestPath();
 	}
 	
 	public String getBaseDomainRequestUrl() {
-		return "http://"+PHOTOBUCKET_BASE_DOMAIN+getRequestPath();
+	    String sslPart = isSsl() ? "s" : "";
+		return "http" + sslPart + "://"+PHOTOBUCKET_BASE_DOMAIN+getRequestPath();
 	}
 
 	public void setFormat(String format) {

File service/src/com/photobucket/api/service/DirectLoginStrategy.java

 import com.photobucket.api.service.model.User;
 
 @SuppressWarnings("serial")
-public class DirectLoginStrategy extends SimpleStrategy implements Serializable {
+public class DirectLoginStrategy extends SimpleSslStrategy implements Serializable {
     private static final Logger logger = LoggerFactory.getLogger(DirectLoginStrategy.class);
 
     private User user;
     private String uniqueDeviceId;
     
 	public DirectLoginStrategy(User user) {
+	    super();
 		this.user = user;
 	}
 	

File service/src/com/photobucket/api/service/SecureStrategy.java

 
 import com.photobucket.api.service.model.User;
 
+/**
+ * SecureStrategy should be used for API requests that require an authenticated user. 
+ * 
+ * @see SimpleSslStrategy For SSL (encrypted) API requests.
+ * 
+ */
 public abstract class SecureStrategy extends SimpleStrategy {
 	private static final long serialVersionUID = 5036774638231334584L;
 	

File service/src/com/photobucket/api/service/SimpleSslStrategy.java

+package com.photobucket.api.service;
+
+
+public abstract class SimpleSslStrategy extends SimpleStrategy {
+    private static final long serialVersionUID = 1504770560301707370L;
+
+    public SimpleSslStrategy() {
+        super();
+        setSsl(true);
+    }
+}

File service/src/com/photobucket/api/service/Strategy.java

 		return api.getSubdomain();
 	}
 	
+	public void setSsl(boolean ssl) {
+        api.setSsl(ssl);
+    }
+    
+    public boolean isSsl() {
+        return api.isSsl();
+    }
+	
 	public RESTfulResponse getApiResponse() {
 		return response;
 	}

File service/src/com/photobucket/api/service/UserRegistrationStrategy.java

 package com.photobucket.api.service;
 
 import java.io.Serializable;
+import java.io.UnsupportedEncodingException;
 import java.net.URLEncoder;
 import java.util.Calendar;
 import java.util.Map;
 import org.apache.commons.lang.builder.ToStringBuilder;
 import org.json.JSONException;
 import org.json.JSONObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import com.photobucket.api.core.HTTPResponseCode;
 import com.photobucket.api.service.exception.APIException;
 import com.photobucket.validator.routines.EmailValidator;
 
 @SuppressWarnings("serial")
-public class UserRegistrationStrategy extends Strategy implements Serializable {
+public class UserRegistrationStrategy extends SimpleSslStrategy implements Serializable {
+    private static final Logger logger = LoggerFactory.getLogger(UserRegistrationStrategy.class);
+    
 	public enum Gender {
 	    MALE("M"), FEMALE("F");
 		private String gender;
     private Integer cellPhone;
     private String cobrand;
 	
+    @Override
+    protected Logger getLogger() {
+        return logger;
+    }
+    
+    @Override
+    protected String getMethod() {
+        return Strategy.METHOD_POST;
+    }
+    
+    @Override
+    protected String getRequestPath() {
+        String encodedUsername;  
+        
+        try {
+          encodedUsername = URLEncoder.encode(username,"UTF-8");
+        } catch (UnsupportedEncodingException e) {
+            // Should not happen since all JVMs are required to support UTF-8
+            throw new RuntimeException("Unsupported encoding", e);
+        }
+        
+        return "/user/"+encodedUsername;
+    }
+
 
 	@Override
 	public void deserializeJSON() throws SerializationException, APIException {
 		// TODO Auto-generated method stub
 		
 	}
-
+	
+	@Override
+	protected void preExecute() {
+	    super.preExecute();
+	    
+	    Map<String,String> params = api.getParameters();           
+        params.put("password", password);
+        params.put("email", email);
+        params.put("birthyear", String.valueOf(birthday.get(Calendar.YEAR)));
+        params.put("birthmonth", String.valueOf(birthday.get(Calendar.MONTH)+1));
+        params.put("birthday", String.valueOf(birthday.get(Calendar.DAY_OF_MONTH)));
+        params.put("gender", gender.toString());
+        params.put("country", countryCode);
+        params.put("tosaccepted", termsOfService.toString());
+        if ( StringUtils.isNotBlank(firstName)) {
+            params.put("firstname", firstName);
+        }
+        if ( StringUtils.isNotBlank(lastName)) {
+            params.put("lastname", lastName);
+        }
+        if ( zipCode != null ) {
+            params.put("zipcode", String.format("%05d", zipCode));
+        }
+        if ( cellPhone != null ) {
+            params.put("cellphone", cellPhone.toString());
+        }
+        if ( StringUtils.isNotBlank(cobrand)) {
+            params.put("cobrand", cobrand);
+        }
+	}
+	
 	@Override
 	protected void executeInternal() throws RegistrationException, Exception {
-		if (validateRequestFields()) {
-		    api.setRequestPath("/user/"+URLEncoder.encode(username, "UTF-8"));
-		    api.setMethod("post");
-			Map<String,String> params = api.getParameters();		    
-			params.put("password", password);
-			params.put("email", email);
-			params.put("birthyear", String.valueOf(birthday.get(Calendar.YEAR)));
-			params.put("birthmonth", String.valueOf(birthday.get(Calendar.MONTH)+1));
-			params.put("birthday", String.valueOf(birthday.get(Calendar.DAY_OF_MONTH)));
-			params.put("gender", gender.toString());
-			params.put("country", countryCode);
-			params.put("tosaccepted", termsOfService.toString());
-			if ( StringUtils.isNotBlank(firstName)) {
-				params.put("firstname", firstName);
-			}
-			if ( StringUtils.isNotBlank(lastName)) {
-				params.put("lastname", lastName);
-			}
-			if ( zipCode != null ) {
-				params.put("zipcode", String.format("%05d", zipCode));
-			}
-			if ( cellPhone != null ) {
-				params.put("cellphone", cellPhone.toString());
-			}
-			if ( StringUtils.isNotBlank(cobrand)) {
-				params.put("cobrand", cobrand);
-			}
-		
-		    response = api.execute();
-		}
-			
+	    validateRequestFields();
+	    super.executeInternal();
 	}
 	
 	private boolean validateRequestFields() throws RegistrationException {