Java Code Examples for javax.microedition.io.Connection

The following examples show how to use javax.microedition.io.Connection. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
public Connection createConnection(String address, int mode, boolean timeouts) throws IOException {
  if (mode != ConnectorService.READ &&
      mode != ConnectorService.WRITE &&
      mode != ConnectorService.READ_WRITE) 
    throw new IOException("Invalid mode");
      
  try {
          
    Connection con = 
      new DatagramConnectionAdapter(this, address, timeouts);

    return con;

  } catch (Exception e) {
    // if anything goes wrong it *must* throw IOException
    throw new IOException(e.getMessage());
  }
}
 
Example 2
public void unregisterFactory(BundleContext bc) {
  Iterator copyIterator;
  synchronized (list) {
    // Copy list to avoid race condition
    copyIterator = new ArrayList(list).iterator();

    // Just a precaution, if this factory is used again.
    list.clear();
  }

  while (copyIterator.hasNext()) {
    try {
      ((Connection) copyIterator.next()).close();
    } catch (IOException e) { /* ignore */ }
  }
}
 
Example 3
/**
 * @inheritDoc
 */
public void cleanup(Object o) {
    try {
        if(o != null) {
            if(o instanceof Connection) {
                ((Connection) o).close();
                return;
            } 
            if(o instanceof RecordEnumeration) {
                ((RecordEnumeration) o).destroy();
                return;
            }
            if(o instanceof RecordStore) {
                ((RecordStore) o).closeRecordStore();
                return;
            }
            super.cleanup(o);
        }
    } catch (Throwable ex) {
        ex.printStackTrace();
    }
}
 
Example 4
/**
 * @inheritDoc
 */
public void cleanup(Object o) {
    try {
        if (o != null) {
            if (o instanceof Connection) {
                ((Connection) o).close();
                return;
            }
            if (o instanceof RecordEnumeration) {
                ((RecordEnumeration) o).destroy();
                return;
            }
            if (o instanceof RecordStore) {
                ((RecordStore) o).closeRecordStore();
                return;
            }
            super.cleanup(o);
        }
    } catch (Throwable ex) {
        ex.printStackTrace();
    }
}
 
Example 5
Source Project: pluotsorbet   Source File: LocalDeviceImpl.java    License: GNU General Public License v2.0 6 votes vote down vote up
public ServiceRecord getRecord(Connection notifier) {
    if (notifier == null) {
        throw new NullPointerException("Null notifier specified.");
    }
    if (!(notifier instanceof BluetoothNotifier)) {
        if (!(notifier instanceof SessionNotifierImpl)) {
            throw new IllegalArgumentException("Invalid notifier class.");
        }
        Connection transport =
            ((SessionNotifierImpl)notifier).getTransport();
        if (!(transport instanceof BluetoothNotifier)) {
            throw new IllegalArgumentException("Invalid notifier class.");
        }
        return ((BluetoothNotifier)transport).getServiceRecord();
    }
    return ((BluetoothNotifier)notifier).getServiceRecord();
}
 
Example 6
public static BluetoothConnection getConnection(Connection conn)
        throws IOException {
    if (conn == null) {
        throw new NullPointerException("Null connection specified.");
    }

    if (conn instanceof ObexPacketStream) {
        conn = ((ObexPacketStream)conn).getTransport();
    }

    if (!(conn instanceof BluetoothConnection)) {
        throw new IllegalArgumentException("The specified connection " +
                "is not a Bluetooth connection.");
    }

    BluetoothConnection btConn = (BluetoothConnection)conn;
    btConn.checkOpen();
    return btConn;
}
 
Example 7
protected void checkSecurity()
        throws BluetoothConnectionException, IOException {
    if (url.authenticate) {
        if (!remoteDevice.authenticate()) {
            throw new BluetoothConnectionException(
                    BluetoothConnectionException.SECURITY_BLOCK,
                    "Authentication failed.");
        }
    }
    if (url.authorize) {
        if (!remoteDevice.authorize((Connection)this)) {
            throw new BluetoothConnectionException(
                    BluetoothConnectionException.SECURITY_BLOCK,
                    "Authorization failed.");
        }
    }
    if (url.encrypt) {
        if (!remoteDevice.encrypt((Connection)this, true)) {
            throw new BluetoothConnectionException(
                    BluetoothConnectionException.SECURITY_BLOCK,
                    "Encryption failed.");
        }
    }
}
 
Example 8
Source Project: pluotsorbet   Source File: RemoteDevice.java    License: GNU General Public License v2.0 6 votes vote down vote up
public boolean isAuthorized(Connection conn) throws IOException {
    BluetoothConnection btconn = BluetoothConnection.getConnection(conn);
    RemoteDevice device;

    try {
        device = btconn.getRemoteDevice();
    } catch (IllegalArgumentException e) {
        return false;
    }
    if (!equals(device)) {
        throw new IllegalArgumentException("The specified connection " +
                "is not a connection to this RemoteDevice.");
    }

    return btconn.isServerSide() && btconn.isAuthorized();
}
 
Example 9
Source Project: pluotsorbet   Source File: Protocol.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Sets up the state of the connection, but
 * does not actually connect to the server until there's something
 * to do.
 *
 * @param theUrl           URL object
 * @param mode             The access mode, ignored
 *                         timeout exceptions, ignored
 *
 * @return reference to this connection
 *
 * @exception IllegalArgumentException If a parameter is invalid.
 * @exception ConnectionNotFoundException If the connection cannot be
 *             found.
 * @exception IOException  If some other kind of I/O error occurs.
 */
private Connection open(HttpUrl theUrl, int mode)
    throws IOException, IllegalArgumentException,
    ConnectionNotFoundException {

    url = theUrl;

    initStreamConnection(mode);

    if (url.port == -1) {
        url.port = default_port;
    }

    if (url.host == null) {
        throw new IllegalArgumentException("missing host in URL");
    }

    hostAndPort = url.host + ":" + url.port;

    return this;
}
 
Example 10
public Connection createConnection(String name, int mode, boolean timeouts) throws IOException {
  HttpURLConnection connection = (HttpURLConnection) new URL(name).openConnection();
  
  if (mode == ConnectorService.READ_WRITE) {
    connection.setDoInput(true);
    connection.setDoOutput(true);
  } else {
    connection.setDoInput(mode == ConnectorService.READ);
    connection.setDoOutput(mode == ConnectorService.WRITE);
  }
  
  Connection con = new HttpConnectionAdapter(this, connection);
  return con;
}
 
Example 11
public DataInputStream openDataInputStream(String name) throws IOException {
  Connection con = open(name, ConnectorService.READ, false);

  if (con instanceof InputConnection) {
    DataInputStream stream = ((InputConnection)con).openDataInputStream();
    con.close();
    return stream;
  }
  con.close();
  throw new IOException("Given scheme does not support input");
}
 
Example 12
public DataOutputStream openDataOutputStream(String name) throws IOException {
  Connection con = open(name, ConnectorService.WRITE, false);

  if (con instanceof OutputConnection) {
    DataOutputStream stream = ((OutputConnection)con).openDataOutputStream();
    con.close();
    return stream;
  }

  con.close();
  throw new IOException("Given scheme does not support output");
}
 
Example 13
public InputStream openInputStream(String name) throws IOException {

    Connection con = open(name, ConnectorService.READ, false);

    if (con instanceof InputConnection) {
      InputStream stream = ((InputConnection)con).openInputStream();
      con.close();
      return stream;
    }

    con.close();
    throw new IOException("Given scheme does not support input");
  }
 
Example 14
public OutputStream openOutputStream(String name) throws IOException {
  Connection con = open(name, ConnectorService.WRITE, false);

  if (con instanceof OutputConnection) {
    OutputStream stream = ((OutputConnection)con).openOutputStream();
    con.close();
    return stream;
  }

  con.close();
  throw new IOException("Given scheme does not support output");
}
 
Example 15
public Connection createConnection(String name,
                                   int mode,
                                   boolean timeouts)
  throws IOException
{
  return new HttpClientConnection(bc, name, mode, timeouts);
}
 
Example 16
protected Connection openPrimImpl(BluetoothUrl url, int mode)
            throws IOException {
    checkOpenMode(mode);
    checkUrl(url);
    this.url = url;

    return url.isServer?
        serverConnection(mode):
        clientConnection(mode);
}
 
Example 17
public Connection acceptAndOpen(ServerRequestHandler handler,
    Authenticator auth) throws IOException {
    if (notifier == null) {
        throw new IOException("session closed");
    }
    if (handler == null) {
        throw new NullPointerException("null handler");
    }
    ObexTransport transport = notifier.acceptAndOpen();
    return new ServerConnectionImpl(transport, handler, auth);
}
 
Example 18
Source Project: pluotsorbet   Source File: ObexPacketStream.java    License: GNU General Public License v2.0 5 votes vote down vote up
public Connection getTransport()
    throws IOException {
    if (transport == null) {
        throw new IOException("connection error");
    }
    return transport.getUnderlyingConnection();
}
 
Example 19
Source Project: pluotsorbet   Source File: RemoteDevice.java    License: GNU General Public License v2.0 5 votes vote down vote up
public boolean authorize(Connection conn) throws IOException {
    BluetoothConnection btconn = BluetoothConnection.getConnection(conn);

    if (!equals(btconn.getRemoteDevice())) {
        throw new IllegalArgumentException("The specified connection " +
                "is not a connection to this RemoteDevice.");
    }
    if (!btconn.isServerSide()) {
        throw new IllegalArgumentException("The local device is client " +
                "rather than the server for the specified connection.");
    }

    return authenticate() && (isTrustedDevice() || btconn.isAuthorized() ||
            btconn.authorize());
}
 
Example 20
Source Project: pluotsorbet   Source File: RemoteDevice.java    License: GNU General Public License v2.0 5 votes vote down vote up
public boolean encrypt(Connection conn, boolean on) throws IOException {
    BluetoothConnection btconn = BluetoothConnection.getConnection(conn);
    if (!equals(btconn.getRemoteDevice())) {
        throw new IllegalArgumentException("The specified connection " +
                "is not a connection to this RemoteDevice.");
    }
    if (on && !authenticate()) {
        return false;
    }
    return btconn.encrypt(on);
}
 
Example 21
Source Project: pluotsorbet   Source File: ContentReader.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Finds the type of the content in this Invocation.
 * <p>
 * The calling thread blocks while the type is being determined.
 * If a network access is needed there may be an associated delay.
 *
 * @return the content type.
 *          May be <code>null</code> if the type can not be determined.
 *
 * @exception IOException if access to the content fails
 * @exception IllegalArgumentException if the content is accessed via
 *  the URL and the URL is invalid
 * @exception SecurityException is thrown if access to the content
 *  is required and is not permitted
 */
String findType() throws IOException, SecurityException {
    String type = null;
    Connection conn = openPrim(true);
    if (conn instanceof ContentConnection) {
    	if( conn instanceof HttpConnection ){
    		HttpConnection hc = (HttpConnection)conn;
         hc.setRequestMethod(HttpConnection.HEAD);
	
         // actual connection performed, some delay...
         if (hc.getResponseCode() != HttpConnection.HTTP_OK)
         	return null;
    	}
    	
        type = ((ContentConnection)conn).getType();
        conn.close();

        if (type != null) {
            // Check for and remove any parameters (rfc2616)
            int ndx = type.indexOf(';');
            if (ndx >= 0) {
                type = type.substring(0, ndx);
            }
            type = type.trim();
            if (type.length() == 0) {
                type = null;
            }
        }
    }

    return type;
}
 
Example 22
Source Project: blucat   Source File: ScanServices.java    License: GNU General Public License v2.0 5 votes vote down vote up
public static boolean testUrl(String server, String protocol, int channel) throws IOException{

	String url = protocol + server + ":" + channel;
	String fullurl = url + ";authenticate=false;encrypt=false";
	try{
		
		Connection con = Connector.open(fullurl, Connector.READ_WRITE, true);
		PrintUtil.out.println(url + " -> Open Channel!!! " + con.getClass().getSimpleName());
		con.close();
		return true;
	}catch(IllegalArgumentException a){  
	
	}catch(Exception e){
		
		String msg = e.getMessage();
		
		if (!msg.contains("0xe00002cd") && !msg.contains("timeout")){
		
			PrintUtil.out.println(url + " -\\> " + msg);
			//e.printStackTrace();
		}
		
		return false;
	}
	
	return true;
}
 
Example 23
Source Project: J2ME-Loader   Source File: ConnectorImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Connection open(final String name, final int mode, final boolean timeouts) throws IOException {
	return openSecure(name, mode, timeouts);
}
 
Example 24
Source Project: J2ME-Loader   Source File: ConnectorAdapter.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public abstract Connection open(String name, int mode, boolean timeouts) throws IOException;
 
Example 25
Source Project: J2ME-Loader   Source File: ConnectorAdapter.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Connection open(String name) throws IOException {
	return open(name, Connector.READ_WRITE, false);
}
 
Example 26
Source Project: J2ME-Loader   Source File: ConnectorAdapter.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Connection open(String name, int mode) throws IOException {
	return open(name, mode, false);
}
 
Example 27
Source Project: J2ME-Loader   Source File: RemoteDevice.java    License: Apache License 2.0 4 votes vote down vote up
public boolean authorize(javax.microedition.io.Connection conn) throws IOException {
	return false;
}
 
Example 28
Source Project: J2ME-Loader   Source File: RemoteDevice.java    License: Apache License 2.0 4 votes vote down vote up
public boolean encrypt(javax.microedition.io.Connection conn, boolean on) throws IOException {
	return false;
}
 
Example 29
Source Project: J2ME-Loader   Source File: RemoteDevice.java    License: Apache License 2.0 4 votes vote down vote up
public boolean isAuthorized(javax.microedition.io.Connection conn) throws IOException {
	return false;
}
 
Example 30
public void registerConnection(Connection con) {
  synchronized (list) {
    list.add(con);
  }
}