Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import com.googlecode.androidannotations.annotations.FragmentByTag;
import com.googlecode.androidannotations.annotations.FromHtml;
import com.googlecode.androidannotations.annotations.Fullscreen;
import com.googlecode.androidannotations.annotations.HttpsClient;
import com.googlecode.androidannotations.annotations.InstanceState;
import com.googlecode.androidannotations.annotations.ItemClick;
import com.googlecode.androidannotations.annotations.ItemLongClick;
Expand Down Expand Up @@ -133,6 +134,7 @@
import com.googlecode.androidannotations.processing.FragmentByTagProcessor;
import com.googlecode.androidannotations.processing.FromHtmlProcessor;
import com.googlecode.androidannotations.processing.FullscreenProcessor;
import com.googlecode.androidannotations.processing.HttpsClientProcessor;
import com.googlecode.androidannotations.processing.InstanceStateProcessor;
import com.googlecode.androidannotations.processing.ItemClickProcessor;
import com.googlecode.androidannotations.processing.ItemLongClickProcessor;
Expand Down Expand Up @@ -190,6 +192,7 @@
import com.googlecode.androidannotations.validation.FragmentByTagValidator;
import com.googlecode.androidannotations.validation.FromHtmlValidator;
import com.googlecode.androidannotations.validation.FullscreenValidator;
import com.googlecode.androidannotations.validation.HttpsClientValidator;
import com.googlecode.androidannotations.validation.InstanceStateValidator;
import com.googlecode.androidannotations.validation.ItemClickValidator;
import com.googlecode.androidannotations.validation.ItemLongClickValidator;
Expand Down Expand Up @@ -291,8 +294,8 @@
FragmentByTag.class, //
BeforeTextChange.class, //
TextChange.class, //
AfterTextChange.class //
})
AfterTextChange.class, //
HttpsClient.class })
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class AndroidAnnotationProcessor extends AnnotatedAbstractProcessor {

Expand Down Expand Up @@ -460,6 +463,7 @@ private ModelValidator buildModelValidator(IRClass rClass, AndroidSystemServices
modelValidator.register(new BeforeTextChangeValidator(processingEnv, rClass));
modelValidator.register(new TextChangeValidator(processingEnv, rClass));
modelValidator.register(new AfterTextChangeValidator(processingEnv, rClass));
modelValidator.register(new HttpsClientValidator(processingEnv, rClass));
return modelValidator;
}

Expand Down Expand Up @@ -538,6 +542,7 @@ private ModelProcessor buildModelProcessor(IRClass rClass, AndroidSystemServices
modelProcessor.register(new TextChangeProcessor(processingEnv, rClass));
modelProcessor.register(new BeforeTextChangeProcessor(processingEnv, rClass));
modelProcessor.register(new AfterTextChangeProcessor(processingEnv, rClass));
modelProcessor.register(new HttpsClientProcessor());
return modelProcessor;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* Copyright (C) 2010-2011 eBusiness Information, Excilys Group
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed To in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.googlecode.androidannotations.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Use this annotation to get an HttpClient instance with the specified KeyStore
* and TrustStore configured to perform an <b>HTTPS</b> request <br/>
* <br/>
*
* All the parameters are optional <br/>
* <br/>
*
* <i>trustStore</i>: int, Resource id of your trust store file ex
* <code>R.raw.cacerts.bks</code> Typically your servers trusted certificates
* (public key, Root Chain Authority etc) <br/>
* <br/>
*
* <i>trustStorePwd</i>: String, Your trust store password (default is
* <code>changeit</code>) <br/>
* <br/>
*
* <i>keyStore</i>: int, Resource id of your keystore Usually your private key
* (client certificate) <br/>
* <br/>
*
* <i>keyStorePwd</i>: String, Your KeyStore password (default is
* <code>changeit</code>) <br/>
* <br/>
*
* <i>hostnameVerif</i>: boolean, Turn on or off strict hostname verification
* (default <code>false</code>) Hostname in certificate (DN) must match the URL. <br/>
* <br/>
*
* <b>Note</b>:
* <tt>Until ICS, Android accept [Key|Trust]store only in BKS format
* (Bouncycastle Key Store)</tt>
*
* @author Nabil Hachicha
*/
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.FIELD)
public @interface HttpsClient {
public static final String DEFAULT_PASSWD = "changeit";

int trustStore() default Id.DEFAULT_VALUE;

String trustStorePwd() default DEFAULT_PASSWD;

int keyStore() default Id.DEFAULT_VALUE;

String keyStorePwd() default DEFAULT_PASSWD;

boolean hostnameVerif() default false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ public void idExists(Element element, Res res, boolean defaultUseName, boolean a
idExists(element, res, defaultUseName, allowDefault, valid, idValue);
}

public void idExists(Element element, Res res, boolean defaultUseName, boolean allowDefault, IsValid valid, String methodName) {

Integer idValue = annotationHelper.extractAnnotationValue(element, methodName);

idExists(element, res, defaultUseName, allowDefault, valid, idValue);
}

public void idsExists(Element element, Res res, IsValid valid) {

int[] idsValues = annotationHelper.extractAnnotationValue(element);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ public <T> T extractAnnotationValue(Element element) {
}
}

@SuppressWarnings("unchecked")
public <T> T extractAnnotationValue(Element element, String methodName) {
Annotation annotation = element.getAnnotation(target);

Method method;
try {
method = annotation.getClass().getMethod(methodName);
return (T) method.invoke(annotation);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public DeclaredType extractAnnotationClassValue(Element element) {

AnnotationMirror annotationMirror = findAnnotationMirror(element, target);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
/**
* Copyright (C) 2010-2011 eBusiness Information, Excilys Group
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed To in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
* @author Nabil Hachicha
*/
package com.googlecode.androidannotations.processing;

import java.lang.annotation.Annotation;

import javax.lang.model.element.Element;

import com.googlecode.androidannotations.annotations.HttpsClient;
import com.googlecode.androidannotations.annotations.Id;
import com.sun.codemodel.JBlock;
import com.sun.codemodel.JCatchBlock;
import com.sun.codemodel.JClass;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JExpression;
import com.sun.codemodel.JInvocation;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JMod;
import com.sun.codemodel.JTryBlock;
import com.sun.codemodel.JVar;

public class HttpsClientProcessor implements ElementProcessor {

public HttpsClientProcessor() {
}

@Override
public Class<? extends Annotation> getTarget() {
return HttpsClient.class;
}

@Override
public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHolder) {
EBeanHolder holder = eBeansHolder.getEnclosingEBeanHolder(element);

int trustStoreFile = element.getAnnotation(HttpsClient.class).trustStore();
String trustStorePwd = element.getAnnotation(HttpsClient.class).trustStorePwd();

int keyStoreFile = element.getAnnotation(HttpsClient.class).keyStore();
String keyStorePwd = element.getAnnotation(HttpsClient.class).keyStorePwd();

boolean hostnameVerif = element.getAnnotation(HttpsClient.class).hostnameVerif();

boolean useCustomTrustStore = Id.DEFAULT_VALUE != trustStoreFile ? true : false;
boolean useCustomKeyStore = Id.DEFAULT_VALUE != keyStoreFile ? true : false;

String fieldName = element.getSimpleName().toString();
JBlock methodBody = holder.init.body();

JClass jTypeRetMethod = holder.refClass("org.apache.http.conn.ClientConnectionManager");
JClass jTypeDefHttp = holder.refClass("org.apache.http.impl.client.DefaultHttpClient");
JClass jTypeKeyStore = holder.refClass("java.security.KeyStore");
JClass jTypeRes = holder.refClass("android.content.res.Resources");
JClass jTypeInputStream = holder.refClass("java.io.InputStream");

JDefinedClass jAnonClass = codeModel.anonymousClass(jTypeDefHttp);

JMethod method = jAnonClass.method(JMod.PROTECTED, jTypeRetMethod, "createClientConnectionManager");
method.annotate(Override.class);

JTryBlock jTryBlock = method.body()._try();
JVar jVarTrusted = null;
JVar jVarKeystore = null;

if (useCustomKeyStore) {
jVarKeystore = jTryBlock.body().decl(jTypeKeyStore, "keystore");
jVarKeystore.init(jTypeKeyStore.staticInvoke("getInstance").arg("BKS"));
}

if (useCustomTrustStore || !useCustomTrustStore && useCustomKeyStore/*
* use
* default
* trust
* store
*/) {
jVarTrusted = jTryBlock.body().decl(jTypeKeyStore, "trusted");
jVarTrusted.init(jTypeKeyStore.staticInvoke("getInstance").arg("BKS"));

}

JVar jVarRes = null;
JVar jVarTrstFile = null;
JVar jVarKeyFile = null;

if (useCustomKeyStore || useCustomTrustStore) {
jVarRes = jTryBlock.body().decl(jTypeRes, "res", JExpr.invoke("getResources"));
}

if (useCustomKeyStore) {
JInvocation jInvRawKey = jVarRes.invoke("openRawResource").arg(JExpr.lit(keyStoreFile));
jVarKeyFile = jTryBlock.body().decl(jTypeInputStream, "inKeystore", jInvRawKey);
}

if (useCustomTrustStore) {
JInvocation jInvRawTrust = jVarRes.invoke("openRawResource").arg(JExpr.lit(trustStoreFile));
jVarTrstFile = jTryBlock.body().decl(jTypeInputStream, "inTrustStore", jInvRawTrust);

} else if (useCustomKeyStore) {
JClass jTypeFileInputStream = holder.refClass("java.io.FileInputStream");
jVarTrstFile = jTryBlock.body().decl(jTypeInputStream, "inTrustStore", JExpr._new(jTypeFileInputStream).arg("/system/etc/security/cacerts.bks"));
}

// try load
if (useCustomKeyStore || useCustomTrustStore) {
JTryBlock jTryLoad = jTryBlock.body()._try();

if (useCustomKeyStore) {
jTryLoad.body().add(JExpr.invoke(jVarKeystore, "load").arg(jVarKeyFile).arg(JExpr.invoke(JExpr.lit(keyStorePwd), "toCharArray")));
}

if (useCustomTrustStore || !useCustomTrustStore && useCustomKeyStore) {
jTryLoad.body().add(JExpr.invoke(jVarTrusted, "load").arg(jVarTrstFile).arg(JExpr.invoke(JExpr.lit(trustStorePwd), "toCharArray")));
}
// finally load
JBlock jFinally = jTryLoad._finally();
if (useCustomKeyStore) {
jFinally.add(JExpr.invoke(jVarKeyFile, "close"));
}

if (useCustomTrustStore || !useCustomTrustStore && useCustomKeyStore) {
jFinally.add(JExpr.invoke(jVarTrstFile, "close"));
}
}

if (null == jVarKeystore && null == jVarTrusted) {
JVar jVarCcm = jTryBlock.body().decl(jTypeRetMethod, "ccm");
jVarCcm.init(JExpr._super().invoke("createClientConnectionManager"));

if (!hostnameVerif) {
JClass jTypeSocketFact = holder.refClass("org.apache.http.conn.ssl.SSLSocketFactory");
JExpression jCast = JExpr.cast(jTypeSocketFact, jVarCcm.invoke("getSchemeRegistry").invoke("getScheme").arg("https").invoke("getSocketFactory"));
JClass jTypeSslFact = holder.refClass("org.apache.http.conn.ssl.SSLSocketFactory");
jTryBlock.body().add(jCast.invoke("setHostnameVerifier").arg(jTypeSslFact.staticRef("ALLOW_ALL_HOSTNAME_VERIFIER")));
}

jTryBlock.body()._return(jVarCcm);

} else {
JClass jTypeSslFact = holder.refClass("org.apache.http.conn.ssl.SSLSocketFactory");
JClass jTypeScheme = holder.refClass("org.apache.http.conn.scheme.Scheme");
JClass jTypeSchemeReg = holder.refClass("org.apache.http.conn.scheme.SchemeRegistry");
JClass jTypeSingleConnMgr = holder.refClass("org.apache.http.impl.conn.SingleClientConnManager");

JVar jVarSslFact = jTryBlock.body().decl(jTypeSslFact, "newSslSocketFactory");
jVarSslFact.init(JExpr._new(jTypeSslFact).arg(null == jVarKeystore ? JExpr._null() : jVarKeystore).arg(keyStorePwd).arg(null == jVarTrusted ? JExpr._null() : jVarTrusted));

if (!hostnameVerif) {
jTryBlock.body().add(JExpr.invoke(jVarSslFact, "setHostnameVerifier").arg(jTypeSslFact.staticRef("ALLOW_ALL_HOSTNAME_VERIFIER")));
}

JVar jVarSchemeReg = jTryBlock.body().decl(jTypeSchemeReg, "registry");
jVarSchemeReg.init(JExpr._new(jTypeSchemeReg));
jTryBlock.body().add(JExpr.invoke(jVarSchemeReg, "register").arg(JExpr._new(jTypeScheme).arg("https").arg(jVarSslFact).arg(JExpr.lit(443))));

JVar jVarCcm = jTryBlock.body().decl(jTypeRetMethod, "ccm");
jVarCcm.init(JExpr._new(jTypeSingleConnMgr).arg(JExpr.invoke("getParams")).arg(jVarSchemeReg));
jTryBlock.body()._return(jVarCcm);
}

// catch block
JClass jTypeException = holder.refClass("java.lang.Exception");
JCatchBlock jCatchBlock = jTryBlock._catch(jTypeException);
JVar jVarExceptionParam = jCatchBlock.param("e");
jCatchBlock.body().add(jVarExceptionParam.invoke("printStackTrace"));
jCatchBlock.body()._return(JExpr._super().invoke("createClientConnectionManager"));

methodBody.assign(JExpr.ref(fieldName), JExpr._new(jAnonClass));

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
public interface IRClass {

public enum Res {
LAYOUT, ID, STRING, ARRAY, COLOR, ANIM, BOOL, DIMEN, DRAWABLE, INTEGER, MOVIE, MENU;
LAYOUT, ID, STRING, ARRAY, COLOR, ANIM, BOOL, DIMEN, DRAWABLE, INTEGER, MOVIE, MENU, RAW;
public String rName() {
return toString().toLowerCase();
}
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.