javax.microedition.io.Connection Java Examples

The following examples show how to use javax.microedition.io.Connection. 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
Source File: BaseConnectionFactory.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 #2
Source File: DatagramConnectionFactory.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 #3
Source File: LocalDeviceImpl.java    From pluotsorbet with 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 #4
Source File: Protocol.java    From pluotsorbet with 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 #5
Source File: BluetoothConnection.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
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 #6
Source File: RemoteDevice.java    From pluotsorbet with 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 #7
Source File: BluetoothConnection.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
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 File: BlackBerryImplementation.java    From CodenameOne with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @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 #9
Source File: GameCanvasImplementation.java    From CodenameOne with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @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 #10
Source File: ConnectorServiceImpl.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 #11
Source File: HttpClientFactory.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Connection createConnection(String name,
                                   int mode,
                                   boolean timeouts)
  throws IOException
{
  return new HttpClientConnection(bc, name, mode, timeouts);
}
 
Example #12
Source File: SessionNotifierImpl.java    From pluotsorbet with GNU General Public License v2.0 5 votes vote down vote up
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 #13
Source File: BluetoothProtocol.java    From pluotsorbet with GNU General Public License v2.0 5 votes vote down vote up
protected Connection openPrimImpl(BluetoothUrl url, int mode)
            throws IOException {
    checkOpenMode(mode);
    checkUrl(url);
    this.url = url;

    return url.isServer?
        serverConnection(mode):
        clientConnection(mode);
}
 
Example #14
Source File: ConnectorServiceImpl.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 #15
Source File: ConnectorServiceImpl.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 #16
Source File: ConnectorServiceImpl.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 #17
Source File: ObexPacketStream.java    From pluotsorbet with 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 #18
Source File: HttpConnectionFactory.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 #19
Source File: RemoteDevice.java    From pluotsorbet with 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 File: RemoteDevice.java    From pluotsorbet with 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 File: ContentReader.java    From pluotsorbet with 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 File: ScanServices.java    From blucat with 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 File: SessionNotifierImpl.java    From pluotsorbet with GNU General Public License v2.0 4 votes vote down vote up
public Connection acceptAndOpen(ServerRequestHandler handler)
    throws IOException {
    return acceptAndOpen(handler, null);
}
 
Example #24
Source File: BluetoothProtocol.java    From pluotsorbet with GNU General Public License v2.0 4 votes vote down vote up
protected abstract Connection serverConnection(int mode)
throws IOException;
 
Example #25
Source File: BluetoothProtocol.java    From pluotsorbet with GNU General Public License v2.0 4 votes vote down vote up
protected abstract Connection clientConnection(int mode)
throws IOException;
 
Example #26
Source File: BTGOEPConnection.java    From pluotsorbet with GNU General Public License v2.0 4 votes vote down vote up
public Connection getUnderlyingConnection() {
    return sock;
}
 
Example #27
Source File: BlucatServer.java    From blucat with GNU General Public License v2.0 4 votes vote down vote up
public static void startServerChannel(String port) throws IOException{
	
	String url = null;
	
	
	if (BlucatState.l2cap){
		url = "btl2cap://localhost:" + port + ";name=BlueCatL2CAP";
	}else{
		url = "btspp://localhost:" + port + ";name=BlueCatRFCOM";
	}
	
	PrintUtil.verbose("#" + "Creating server on channel " + port);
	
       Connection service = Connector.open(url);

       ServiceRecord rec = LocalDevice.getLocalDevice().getRecord(service);
       
       //PrintUtil.out.println(rec.toString());
       String remoteUrl = rec.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
       remoteUrl = remoteUrl.substring(0, remoteUrl.indexOf(";"));

       PrintUtil.verbose("#" + new Date() + " - Listening at " + remoteUrl);
       
       BlucatConnection.handle(service);
}
 
Example #28
Source File: Protocol.java    From pluotsorbet with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Open a client or server socket connection.
 * <p>
 * The name string for this protocol should be:
 * "socket://&lt;name or IP number&gt;:&lt;port number&gt;
 * <p>
 * We allow "socket://:nnnn" to mean an inbound server socket connection.
 *
 * @param token      security token of the calling class
 * @param name       the target for the connection
 * @param mode       I/O access mode
 *
 * @return client or server TCP socket connection
 *
 * @exception  IOException  if an I/O error occurs.
 * @exception  ConnectionNotFoundException  if the host cannot be connected
 *              to
 * @exception  IllegalArgumentException  if the name is malformed
 */
private Connection open(SecurityToken token, String name, int mode)
        throws IOException {
    HttpUrl url;
    ServerSocket serverSocket;

    if (name.charAt(0) != '/' || name.charAt(1) != '/') {
        throw new IllegalArgumentException(
                  "Protocol must start with \"//\"");
    }

    url = new HttpUrl("socket", name); // parse name into host and port

    /*
     * Since we reused the HttpUrl parser, we must make sure that
     * there was nothing past the authority in the URL.
     */
    if (url.path != null || url.query != null || url.fragment != null) {
        throw new IllegalArgumentException("Malformed address");
    }

    NetworkSubsystem.getInstance(classSecurityToken).ensureActive();

    host = url.host;
    port = url.port;
    
    /*
     * If 'host' == null then we are a server endpoint at
     * port 'port'.
     */

    if (host != null) {
        checkForPermission(name, token);
        initStreamConnection(mode);

        // this will call the connect method which uses the host and port
        connect();
        return this;
    }

    // We allow "socket://:nnnn" to mean an inbound TCP server socket.
    try {
        serverSocket = (ServerSocket)Class.forName(
              "com.sun.midp.io.j2me.serversocket.Socket").newInstance();
    } catch (Exception e) {
        throw new ConnectionNotFoundException("Connection not supported");
    }
        
    serverSocket.open(port, token);
    return (Connection)serverSocket;
}
 
Example #29
Source File: Protocol.java    From pluotsorbet with GNU General Public License v2.0 4 votes vote down vote up
public Connection openPrim(String name, int mode, boolean timeouts) throws IOException {
    return new LocalMsgConnection(name, mode, timeouts);
}
 
Example #30
Source File: SessionNotifier.java    From pluotsorbet with GNU General Public License v2.0 4 votes vote down vote up
public Connection acceptAndOpen(ServerRequestHandler handler,
Authenticator auth) throws IOException;