use of com.codename1.rad.models.ContentType in project CodeRAD by shannah.
the class ResultParser method createGetter.
/**
* Creates a getter for a given property.
* @param property
* @return
*/
private static Getter createGetter(Property property) {
Getter getter = null;
ContentType propType = property.getContentType();
if (propType == ContentType.BooleanType) {
getter = (rs, sel) -> {
return rs.getAsBoolean(sel);
};
} else if (propType == ContentType.DateType) {
getter = (rs, sel) -> {
return rs.getAsString(sel);
};
} else if (propType == ContentType.DoubleType || propType == ContentType.FloatType) {
getter = (rs, sel) -> {
return rs.getAsDouble(sel);
};
} else if (propType == ContentType.IntegerType) {
getter = (rs, sel) -> {
return rs.getAsInteger(sel);
};
} else if (propType == ContentType.LongType) {
getter = (rs, sel) -> {
return rs.getAsLong(sel);
};
} else if (propType == ContentType.Text) {
getter = (rs, sel) -> {
return rs.getAsString(sel);
};
} else if (propType.isEntityList()) {
getter = (rs, sel) -> {
try {
return rs.getAsArray(sel);
} catch (Throwable t) {
Log.p("Failed to get selector " + sel + " from result " + rs);
Log.e(t);
return null;
}
};
} else if (propType.isEntity()) {
getter = (rs, sel) -> {
return rs.get(sel);
};
}
return getter;
}
use of com.codename1.rad.models.ContentType in project CodeRAD by shannah.
the class ContentType method createObjectType.
public static <V> ContentType<V> createObjectType(Class<V> representationClass) {
if (representationClass == Entity.class) {
return (ContentType<V>) EntityType;
}
if (representationClass == EntityList.class) {
return (ContentType<V>) EntityListType;
}
return new ContentType<V>(new Name(representationClass.getName()), representationClass) {
private boolean isEntity;
private boolean isEntityList;
{
isEntity = Entity.class.isAssignableFrom(representationClass);
isEntityList = EntityList.class.isAssignableFrom(representationClass);
;
}
@Override
public boolean equals(Object obj) {
return obj.getClass() == this.getClass() && ((ContentType) obj).getRepresentationClass() == representationClass;
}
@Override
public boolean isEntity() {
return isEntity;
}
@Override
public boolean isEntityList() {
return isEntityList;
}
@Override
public int hashCode() {
return representationClass.hashCode();
}
@Override
public boolean canConvertFrom(ContentType otherType) {
return representationClass.isAssignableFrom(otherType.representationClass);
}
@Override
public <U> V from(ContentType<U> otherType, U data) {
if (representationClass.isAssignableFrom(otherType.representationClass)) {
return (V) data;
}
return super.from(otherType, data);
}
@Override
public <U> U to(ContentType<U> otherType, V data) {
if (otherType.representationClass.isAssignableFrom(representationClass)) {
return (U) data;
}
return super.to(otherType, data);
}
};
}
use of com.codename1.rad.models.ContentType in project CodenameOne by codenameone.
the class RequestBuilder method createRequest.
private Connection createRequest(boolean parseJson) {
Connection req = new Connection(parseJson);
for (String key : pathParams.keySet()) {
url = com.codename1.util.StringUtil.replaceAll(url, "{" + key + "}", pathParams.get(key));
}
if (contentType != null) {
req.setContentType(contentType);
}
req.setFailSilently(false);
if (cache != null) {
req.setCacheMode(cache);
}
req.setReadResponseForErrors(true);
req.setDuplicateSupported(true);
req.setUrl(url);
req.setHttpMethod(method);
if (postParameters == null) {
req.setPost(method.equalsIgnoreCase("POST") || method.equalsIgnoreCase("PUT") || method.equalsIgnoreCase("PATCH"));
} else {
req.setPost(postParameters);
}
if (body != null) {
req.setRequestBody(body);
req.setWriteRequest(true);
}
if (timeout != null) {
req.setTimeout(timeout);
}
if (readTimeout != null) {
req.setReadTimeout(readTimeout);
}
for (String key : queryParams.keySet()) {
Object value = queryParams.get(key);
if (value instanceof String[]) {
req.addArgument(key, (String[]) value);
} else {
req.addArgument(key, (String) value);
}
}
for (String key : headers.keySet()) {
req.addRequestHeader(key, headers.get(key));
}
for (ActionListener<NetworkEvent> l : errorCallbacks) {
req.addExceptionListener(l);
}
req.setInsecure(insecure);
if (cookiesEnabled != null) {
req.setCookiesEnabled(cookiesEnabled);
}
if (priority != null) {
req.setPriority(priority);
}
return req;
}
use of com.codename1.rad.models.ContentType in project CodenameOne by codenameone.
the class VServAds method createAdRequest.
/**
* {@inheritDoc}
*/
protected ConnectionRequest createAdRequest() {
ConnectionRequest con = new ConnectionRequest() {
protected void handleErrorResponseCode(int code, String message) {
failed = true;
}
protected void handleException(Exception err) {
failed = true;
Log.e(err);
}
private String getString(Hashtable h, String n) {
Object v = h.get(n);
if (v == null) {
return null;
}
if (v instanceof Vector) {
return (String) ((Vector) v).elementAt(0);
}
return (String) v;
}
protected void readResponse(InputStream input) throws IOException {
JSONParser parser = new JSONParser();
Hashtable h = parser.parse(new InputStreamReader(input, "UTF-8"));
if (h.size() == 0) {
return;
}
backgroundColor = Integer.parseInt((String) ((Hashtable) ((Vector) h.get("style")).elementAt(0)).get("background-color"), 16);
Hashtable actionHash = ((Hashtable) ((Vector) h.get("action")).elementAt(0));
actionNotify = getString(actionHash, "notify");
if (actionNotify == null) {
actionNotify = getString(actionHash, "notify-once");
}
destination = (String) actionHash.get("data");
Hashtable renderHash = ((Hashtable) ((Vector) h.get("render")).elementAt(0));
contentType = (String) renderHash.get("type");
renderNotify = getString(renderHash, "notify");
if (renderNotify == null) {
renderNotify = getString(renderHash, "notify-once");
}
imageURL = (String) renderHash.get("data");
}
};
con.setUrl(URL);
con.setPost(false);
con.addArgument("zoneid", getZoneId());
con.addArgument("ua", Display.getInstance().getProperty("User-Agent", ""));
con.addArgument("app", "1");
con.addArgument("aid", Display.getInstance().getProperty("androidId", ""));
con.addArgument("uuid", Display.getInstance().getProperty("uuid", ""));
con.addArgument("im", Display.getInstance().getProperty("imei", ""));
con.addArgument("sw", "" + Display.getInstance().getDisplayWidth());
con.addArgument("sh", "" + Display.getInstance().getDisplayHeight());
con.addArgument("mn", Display.getInstance().getProperty("AppName", ""));
con.addArgument("vs3", "1");
con.addArgument("partnerid", "1");
con.addArgument("zc", "" + category);
if (countryCode != null) {
con.addArgument("cc", countryCode);
}
if (networkCode != null) {
con.addArgument("nc", networkCode);
}
if (locale != null) {
con.addArgument("lc", locale);
}
return con;
}
use of com.codename1.rad.models.ContentType in project CodenameOne by codenameone.
the class VServAds method getPendingAd.
/**
* {@inheritDoc}
*/
protected Component getPendingAd() {
if (imageURL == null) {
return null;
}
if (renderNotify != null && renderNotify.length() > 0) {
ConnectionRequest c = new ConnectionRequest();
c.setFailSilently(true);
c.setUrl(renderNotify);
c.setPost(false);
NetworkManager.getInstance().addToQueue(c);
}
if ("image".equalsIgnoreCase(contentType)) {
Button adComponent = new Button() {
public void setIcon(Image icon) {
if (icon != null && isScaleMode()) {
icon = icon.scaledWidth(Display.getInstance().getDisplayWidth());
}
super.setIcon(icon);
}
};
adComponent.setUIID("Container");
adComponent.getStyle().setBgColor(backgroundColor);
adComponent.getStyle().setOpacity(0xff);
ImageDownloadService imd = new ImageDownloadService(imageURL, adComponent);
NetworkManager.getInstance().addToQueueAndWait(imd);
/*adComponent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Display.getInstance().execute(getAdDestination());
}
});*/
return adComponent;
} else {
WebBrowser wb = new WebBrowser();
if (wb.getInternal() instanceof BrowserComponent) {
BrowserComponent bc = (BrowserComponent) wb.getInternal();
bc.setBrowserNavigationCallback(new BrowserNavigationCallback() {
public boolean shouldNavigate(final String url) {
unlock(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Display.getInstance().execute(url);
}
});
return false;
}
});
}
wb.setURL(imageURL);
return wb;
}
}
Aggregations