Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIMOB-19163] Android: Removal of Apache HTTP Client #7036

Merged
merged 11 commits into from
Aug 25, 2015
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@

package ti.modules.titanium.network;

import java.net.HttpCookie;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import org.apache.http.cookie.Cookie;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.annotations.Kroll;
Expand Down Expand Up @@ -41,29 +39,28 @@ public class CookieProxy extends KrollProxy
systemExpiryDateFormatter.setTimeZone(timezone);
}

private BasicClientCookie basicClientCookie;
private HttpCookie httpCookie;

public CookieProxy()
{
super();
}

public CookieProxy(Cookie cookie)
public CookieProxy(HttpCookie cookie)
{
super();
if (cookie instanceof BasicClientCookie) {
basicClientCookie = (BasicClientCookie) cookie;
setProperty(TiC.PROPERTY_NAME, basicClientCookie.getName());
setProperty(TiC.PROPERTY_VALUE, basicClientCookie.getValue());
setProperty(TiC.PROPERTY_DOMAIN, basicClientCookie.getDomain());
Date expiryDate = basicClientCookie.getExpiryDate();
if (expiryDate != null) {
setProperty(TiC.PROPERTY_EXPIRY_DATE, httpExpiryDateFormatter.format(expiryDate));
}
setProperty(TiC.PROPERTY_COMMENT, basicClientCookie.getComment());
setProperty(TiC.PROPERTY_PATH, basicClientCookie.getPath());
setProperty(TiC.PROPERTY_SECURE, basicClientCookie.isSecure());
setProperty(TiC.PROPERTY_VERSION, basicClientCookie.getVersion());
if (cookie instanceof HttpCookie) {
httpCookie = (HttpCookie) cookie;
setProperty(TiC.PROPERTY_NAME, httpCookie.getName());
setProperty(TiC.PROPERTY_VALUE, httpCookie.getValue());
setProperty(TiC.PROPERTY_DOMAIN, httpCookie.getDomain());
// PROPERTY_EXPIRY_DATE not used instead, PROPERTY_MAX_AGE is used
// See http://developer.android.com/reference/java/net/HttpCookie.html for more info
setProperty(TiC.PROPERTY_MAX_AGE, httpCookie.getMaxAge());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you modify the doc for this replacement property?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will modify the docs.

setProperty(TiC.PROPERTY_COMMENT, httpCookie.getComment());
setProperty(TiC.PROPERTY_PATH, httpCookie.getPath());
setProperty(TiC.PROPERTY_SECURE, httpCookie.getSecure());
setProperty(TiC.PROPERTY_VERSION, httpCookie.getVersion());
} else {
Log.e(TAG, "Unable to create CookieProxy. Invalid cookie type.");
}
Expand Down Expand Up @@ -95,70 +92,65 @@ public void handleCreationDict(KrollDict dict)
String name = TiConvert.toString(getProperty(TiC.PROPERTY_NAME));
String value = TiConvert.toString(getProperty(TiC.PROPERTY_VALUE));
if (name != null) {
this.basicClientCookie = new BasicClientCookie(name, value);
this.httpCookie = new HttpCookie(name, value);
} else {
Log.w(TAG, "Unable to create the http client cookie. Need to provide a valid name.");
return;
}

if (dict.containsKey(TiC.PROPERTY_DOMAIN)) {
basicClientCookie.setDomain(TiConvert.toString(getProperty(TiC.PROPERTY_DOMAIN)));
}
if (dict.containsKey(TiC.PROPERTY_EXPIRY_DATE)) {
try {
basicClientCookie.setExpiryDate(httpExpiryDateFormatter.parse(TiConvert
.toString(getProperty(TiC.PROPERTY_EXPIRY_DATE))));
} catch (Exception e) {
Log.e(TAG, "Unable to set expiry date to the cookie.", e);
}
httpCookie.setDomain(TiConvert.toString(getProperty(TiC.PROPERTY_DOMAIN)));
}
if (dict.containsKey(TiC.PROPERTY_MAX_AGE)) {
// PROPERTY_EXPIRY_DATE not used instead, PROPERTY_MAX_AGE is used
// See http://developer.android.com/reference/java/net/HttpCookie.html for more info
httpCookie.setMaxAge(TiConvert.toInt(getProperty(TiC.PROPERTY_MAX_AGE)));
}
if (dict.containsKey(TiC.PROPERTY_COMMENT)) {
basicClientCookie.setComment(TiConvert.toString(getProperty(TiC.PROPERTY_COMMENT)));
httpCookie.setComment(TiConvert.toString(getProperty(TiC.PROPERTY_COMMENT)));
}
if (dict.containsKey(TiC.PROPERTY_PATH)) {
basicClientCookie.setPath(TiConvert.toString(getProperty(TiC.PROPERTY_PATH)));
httpCookie.setPath(TiConvert.toString(getProperty(TiC.PROPERTY_PATH)));
}
if (dict.containsKey(TiC.PROPERTY_SECURE)) {
basicClientCookie.setSecure(TiConvert.toBoolean(getProperty(TiC.PROPERTY_SECURE)));
httpCookie.setSecure(TiConvert.toBoolean(getProperty(TiC.PROPERTY_SECURE)));
}
if (dict.containsKey(TiC.PROPERTY_VERSION)) {
basicClientCookie.setVersion(TiConvert.toInt(getProperty(TiC.PROPERTY_VERSION)));
httpCookie.setVersion(TiConvert.toInt(getProperty(TiC.PROPERTY_VERSION)));
}
}

@Override
public void onPropertyChanged(String name, Object value)
{
if (basicClientCookie == null) {
if (httpCookie == null) {
return;
}

super.onPropertyChanged(name, value);

if (TiC.PROPERTY_VALUE.equals(name)) {
basicClientCookie.setValue(TiConvert.toString(value));
httpCookie.setValue(TiConvert.toString(value));
} else if (TiC.PROPERTY_DOMAIN.equals(name)) {
basicClientCookie.setDomain(TiConvert.toString(value));
} else if (TiC.PROPERTY_EXPIRY_DATE.equals(name)) {
try {
basicClientCookie.setExpiryDate(httpExpiryDateFormatter.parse(TiConvert.toString(value)));
} catch (Exception e) {
Log.e(TAG, "Unable to set expiry date to the cookie.", e);
}
httpCookie.setDomain(TiConvert.toString(value));
} else if (TiC.PROPERTY_MAX_AGE.equals(name)) {
// PROPERTY_EXPIRY_DATE not used instead, PROPERTY_MAX_AGE is used
// See http://developer.android.com/reference/java/net/HttpCookie.html for more info
httpCookie.setMaxAge(TiConvert.toInt(value));
} else if (TiC.PROPERTY_COMMENT.equals(name)) {
basicClientCookie.setComment(TiConvert.toString(value));
httpCookie.setComment(TiConvert.toString(value));
} else if (TiC.PROPERTY_PATH.equals(name)) {
basicClientCookie.setPath(TiConvert.toString(value));
httpCookie.setPath(TiConvert.toString(value));
} else if (TiC.PROPERTY_SECURE.equals(name)) {
basicClientCookie.setSecure(TiConvert.toBoolean(value));
httpCookie.setSecure(TiConvert.toBoolean(value));
} else if (TiC.PROPERTY_VERSION.equals(name)) {
basicClientCookie.setVersion(TiConvert.toInt(value));
httpCookie.setVersion(TiConvert.toInt(value));
}
}

public BasicClientCookie getHTTPCookie()
public HttpCookie getHTTPCookie()
{
return basicClientCookie;
return httpCookie;
}

@Kroll.getProperty @Kroll.method
Expand All @@ -170,6 +162,6 @@ public String getName()
@Override
public String getApiName()
{
return "Ti.Network.Cookie";
return "Ti.Network.CookieProxy";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason for this change? A typo?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A Typo. A very bad typo. Thanks for catching this.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
*/
package ti.modules.titanium.network;

import java.io.UnsupportedEncodingException;

import javax.net.ssl.X509KeyManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.MethodNotSupportedException;
import org.apache.http.auth.AuthSchemeFactory;
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.annotations.Kroll;
Expand Down Expand Up @@ -57,6 +57,7 @@ public HTTPClientProxy(TiContext tiContext)
public void handleCreationDict(KrollDict dict)
{
super.handleCreationDict(dict);

if (hasProperty(TiC.PROPERTY_TIMEOUT)) {
client.setTimeout(TiConvert.toInt(getProperty(TiC.PROPERTY_TIMEOUT),0));
}
Expand All @@ -68,7 +69,7 @@ public void handleCreationDict(KrollDict dict)
if (hasProperty(TiC.PROPERTY_AUTO_ENCODE_URL)) {
client.setAutoEncodeUrl(TiConvert.toBoolean((getProperty(TiC.PROPERTY_AUTO_ENCODE_URL)),true));
}

//Set the securityManager on the client if it is defined as a valid value
if (hasProperty(PROPERTY_SECURITY_MANAGER)) {
Object prop = getProperty(PROPERTY_SECURITY_MANAGER);
Expand All @@ -80,10 +81,12 @@ public void handleCreationDict(KrollDict dict)
}
}
}

client.setTlsVersion(TiConvert.toInt(getProperty(TiC.PROPERTY_TLS_VERSION), NetworkModule.TLS_DEFAULT));


}

@Kroll.method
public void abort()
{
Expand Down Expand Up @@ -137,7 +140,7 @@ public String getStatusText()
{
return client.getStatusText();
}

@Kroll.method
public void open(String method, String url)
{
Expand All @@ -146,7 +149,7 @@ public void open(String method, String url)

@Kroll.method
public void send(@Kroll.argument(optional=true) Object data)
throws MethodNotSupportedException
throws UnsupportedEncodingException
{
client.send(data);
}
Expand Down Expand Up @@ -268,6 +271,8 @@ public String getDomain()
return null;
}

// This uses Apache
/*
@Kroll.method
public void addAuthFactory(String scheme, Object factory)
{
Expand All @@ -278,6 +283,7 @@ public void addAuthFactory(String scheme, Object factory)

client.addAuthFactory(scheme, (AuthSchemeFactory)factory);
}
*/

@Kroll.method
public void addTrustManager(Object manager)
Expand Down