Search in sources :

Example 6 with ContentType

use of com.codename1.rad.models.ContentType in project CodenameOne by codenameone.

the class JavaSEPort method openInputStream.

/**
 * @inheritDoc
 */
public InputStream openInputStream(Object connection) throws IOException {
    if (connection instanceof String) {
        FileInputStream fc = new FileInputStream(unfile((String) connection));
        BufferedInputStream o = new BufferedInputStream(fc, (String) connection);
        return o;
    }
    if (netMonitor != null || slowConnectionMode || disconnectedMode) {
        final NetworkRequestObject nr = getByConnection((URLConnection) connection);
        if (nr != null || slowConnectionMode || disconnectedMode) {
            if (slowConnectionMode) {
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                }
            }
            if (nr != null) {
                nr.setTimeServerResponse(System.currentTimeMillis());
            }
            if (disconnectedMode) {
                throw new IOException("Unreachable");
            }
            HttpURLConnection con = (HttpURLConnection) connection;
            String headers = "";
            Map<String, List<String>> map = con.getHeaderFields();
            for (String header : map.keySet()) {
                headers += header + "=" + map.get(header) + "\n";
            }
            if (nr != null) {
                nr.setResponseHeaders(headers);
                nr.setResponseBody("");
            }
            InputStream is;
            if (con.getResponseCode() >= 200 && con.getResponseCode() < 300) {
                is = con.getInputStream();
            } else {
                is = con.getErrorStream();
            }
            boolean isText = false;
            String contentType = con.getContentType();
            if (contentType != null) {
                if (contentType.startsWith("text/") || contentType.contains("json") || contentType.contains("css") || contentType.contains("javascript")) {
                    isText = true;
                }
            }
            final boolean fIsText = isText;
            InputStream i = new BufferedInputStream(is) {

                public synchronized int read(byte[] b, int off, int len) throws IOException {
                    int s = super.read(b, off, len);
                    if (nr != null) {
                        if (fIsText && s > -1) {
                            nr.setResponseBody(nr.getResponseBody() + new String(b, off, len));
                        }
                    }
                    if (slowConnectionMode) {
                        try {
                            Thread.sleep(len);
                        } catch (Exception e) {
                        }
                    }
                    if (disconnectedMode) {
                        throw new IOException("Unreachable");
                    }
                    return s;
                }

                @Override
                public void close() throws IOException {
                    super.close();
                    if (nr != null) {
                        nr.setTimeComplete(System.currentTimeMillis());
                    }
                }
            };
            return i;
        }
    }
    if (connection instanceof HttpURLConnection) {
        HttpURLConnection ht = (HttpURLConnection) connection;
        if (ht.getResponseCode() < 400) {
            return new BufferedInputStream(ht.getInputStream());
        }
        return new BufferedInputStream(ht.getErrorStream());
    } else {
        return new BufferedInputStream(((URLConnection) connection).getInputStream());
    }
}
Also used : BufferedInputStream(com.codename1.io.BufferedInputStream) BufferedInputStream(com.codename1.io.BufferedInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) AudioInputStream(javax.sound.sampled.AudioInputStream) ZipInputStream(java.util.zip.ZipInputStream) List(java.util.List) NodeList(org.w3c.dom.NodeList) AttributedString(java.text.AttributedString) InvocationTargetException(java.lang.reflect.InvocationTargetException) SQLException(java.sql.SQLException) ParseException(java.text.ParseException) EOFException(java.io.EOFException) FontFormatException(java.awt.FontFormatException) LineUnavailableException(javax.sound.sampled.LineUnavailableException) Point(java.awt.Point)

Example 7 with ContentType

use of com.codename1.rad.models.ContentType in project CodeRAD by shannah.

the class DefaultTableCellEditor method getTableCellEditorComponent.

@Override
public Component getTableCellEditorComponent(Table table, Object value, boolean isSelected, int row, int column) {
    TextField tf = new TextField();
    TableModel model = table.getModel();
    ContentType contentType = model.getCellContentType(row, column);
    String strVal = value == null ? "" : String.valueOf(value);
    tf.setText(strVal);
    tf.addDataChangedListener((type, index) -> {
        table.getModel().setValueAt(ContentType.convert(Text, tf.getText(), contentType), row, column);
    });
    return tf;
}
Also used : ContentType(com.codename1.rad.models.ContentType) TextField(com.codename1.ui.TextField)

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