Search in sources :

Example 1 with ContentType

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;
}
Also used : EntityType(com.codename1.rad.models.EntityType) SimpleDateFormat(com.codename1.l10n.SimpleDateFormat) CharArrayReader(com.codename1.io.CharArrayReader) Date(java.util.Date) EntityList(com.codename1.rad.models.EntityList) HashMap(java.util.HashMap) Element(com.codename1.xml.Element) JSONParser(com.codename1.io.JSONParser) Log(com.codename1.io.Log) ParseException(com.codename1.l10n.ParseException) ArrayList(java.util.ArrayList) List(java.util.List) java.io(java.io) Map(java.util.Map) EntityProperty(com.codename1.rad.models.EntityProperty) Result(com.codename1.rad.processing.Result) Entity(com.codename1.rad.models.Entity) EntityFactory(com.codename1.rad.models.EntityFactory) Tag(com.codename1.rad.models.Tag) ContentType(com.codename1.rad.models.ContentType) Property(com.codename1.rad.models.Property) ContentType(com.codename1.rad.models.ContentType)

Example 2 with ContentType

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);
        }
    };
}
Also used : Name(com.codename1.rad.models.Property.Name)

Example 3 with ContentType

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;
}
Also used : NetworkEvent(com.codename1.io.NetworkEvent) PropertyBusinessObject(com.codename1.properties.PropertyBusinessObject)

Example 4 with ContentType

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;
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest) InputStreamReader(java.io.InputStreamReader) Hashtable(java.util.Hashtable) InputStream(java.io.InputStream) JSONParser(com.codename1.io.JSONParser) Vector(java.util.Vector) IOException(java.io.IOException)

Example 5 with ContentType

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;
    }
}
Also used : ImageDownloadService(com.codename1.io.services.ImageDownloadService) ConnectionRequest(com.codename1.io.ConnectionRequest) WebBrowser(com.codename1.components.WebBrowser) ActionListener(com.codename1.ui.events.ActionListener) ActionEvent(com.codename1.ui.events.ActionEvent) BrowserNavigationCallback(com.codename1.ui.events.BrowserNavigationCallback)

Aggregations

ConnectionRequest (com.codename1.io.ConnectionRequest)2 JSONParser (com.codename1.io.JSONParser)2 ContentType (com.codename1.rad.models.ContentType)2 List (java.util.List)2 WebBrowser (com.codename1.components.WebBrowser)1 BufferedInputStream (com.codename1.io.BufferedInputStream)1 CharArrayReader (com.codename1.io.CharArrayReader)1 Log (com.codename1.io.Log)1 NetworkEvent (com.codename1.io.NetworkEvent)1 ImageDownloadService (com.codename1.io.services.ImageDownloadService)1 ParseException (com.codename1.l10n.ParseException)1 SimpleDateFormat (com.codename1.l10n.SimpleDateFormat)1 PropertyBusinessObject (com.codename1.properties.PropertyBusinessObject)1 Entity (com.codename1.rad.models.Entity)1 EntityFactory (com.codename1.rad.models.EntityFactory)1 EntityList (com.codename1.rad.models.EntityList)1 EntityProperty (com.codename1.rad.models.EntityProperty)1 EntityType (com.codename1.rad.models.EntityType)1 Property (com.codename1.rad.models.Property)1 Name (com.codename1.rad.models.Property.Name)1