Ejemplo n.º 1
0
  public ReaderHandler(LowLevelDbAccess lowLevelDbAccess, String webDir) {

    loginInfoDb = new LoginInfo.DB(lowLevelDbAccess);
    userDb = new User.DB(lowLevelDbAccess);
    feedDb = new Feed.DB(lowLevelDbAccess);
    articleDb = new Article.DB(lowLevelDbAccess);
    readArticlesCollDb = new ReadArticlesColl.DB(lowLevelDbAccess);
    userHelpers = new UserHelpers(loginInfoDb, userDb);

    setContextPath("/");

    File warPath = new File(webDir);
    setWar(warPath.getAbsolutePath());

    if (isInJar) {
      for (Map.Entry<String, String> entry : PATH_MAPPING.entrySet()) {
        addPrebuiltJsp(entry.getKey(), "jsp." + entry.getValue().replaceAll("_", "_005f") + "_jsp");
      }
    } else {
      for (Map.Entry<String, String> entry : PATH_MAPPING.entrySet()) {
        addServlet(
            new ServletHolder(new RedirectServlet("/" + entry.getValue() + ".jsp")),
            entry.getKey());
      }
    }

    setErrorHandler(new ReaderErrorHandler());
  }
Ejemplo n.º 2
0
 // keyboard discovery code
 private void _mapKey(char charCode, int keyindex, boolean shift, boolean altgraph) {
   log("_mapKey: " + charCode);
   // if character is not in map, add it
   if (!charMap.containsKey(new Integer(charCode))) {
     log("Notified: " + (char) charCode);
     KeyEvent event =
         new KeyEvent(
             applet(),
             0,
             0,
             (shift ? KeyEvent.SHIFT_MASK : 0) + (altgraph ? KeyEvent.ALT_GRAPH_MASK : 0),
             ((Integer) vkKeys.get(keyindex)).intValue(),
             (char) charCode);
     charMap.put(new Integer(charCode), event);
     log("Mapped char " + (char) charCode + " to KeyEvent " + event);
     if (((char) charCode) >= 'a' && ((char) charCode) <= 'z') {
       // put shifted version of a-z in automatically
       int uppercharCode = (int) Character.toUpperCase((char) charCode);
       event =
           new KeyEvent(
               applet(),
               0,
               0,
               KeyEvent.SHIFT_MASK + (altgraph ? KeyEvent.ALT_GRAPH_MASK : 0),
               ((Integer) vkKeys.get(keyindex)).intValue(),
               (char) uppercharCode);
       charMap.put(new Integer(uppercharCode), event);
       log("Mapped char " + (char) uppercharCode + " to KeyEvent " + event);
     }
   }
 }
Ejemplo n.º 3
0
  /**
   * Returns true if the user represented by the current request plays the named role.
   *
   * @param role the named role to test.
   * @return true if the user plays the role.
   */
  public boolean isUserInRole(String role) {
    ServletInvocation invocation = getInvocation();

    if (invocation == null) {
      if (getRequest() != null) return getRequest().isUserInRole(role);
      else return false;
    }

    HashMap<String, String> roleMap = invocation.getSecurityRoleMap();

    if (roleMap != null) {
      String linkRole = roleMap.get(role);

      if (linkRole != null) role = linkRole;
    }

    String runAs = getRunAs();

    if (runAs != null) return runAs.equals(role);

    WebApp webApp = getWebApp();

    Principal user = getUserPrincipal();

    if (user == null) {
      if (log.isLoggable(Level.FINE)) log.fine(this + " no user for isUserInRole");

      return false;
    }

    RoleMapManager roleManager = webApp != null ? webApp.getRoleMapManager() : null;

    if (roleManager != null) {
      Boolean result = roleManager.isUserInRole(role, user);

      if (result != null) {
        if (log.isLoggable(Level.FINE)) log.fine(this + " userInRole(" + role + ")->" + result);

        return result;
      }
    }

    Login login = webApp == null ? null : webApp.getLogin();

    boolean inRole = login != null && login.isUserInRole(user, role);

    if (log.isLoggable(Level.FINE)) {
      if (login == null) log.fine(this + " no Login for isUserInRole");
      else if (user == null) log.fine(this + " no user for isUserInRole");
      else if (inRole) log.fine(this + " " + user + " is in role: " + role);
      else log.fine(this + " failed " + user + " in role: " + role);
    }

    return inRole;
  }
Ejemplo n.º 4
0
 static {
   PATH_MAPPING.put("", "home_page");
   PATH_MAPPING.put(PATH_LOGIN, "login");
   PATH_MAPPING.put(PATH_LOGOUT, "login"); // !!! after logout we get redirected to /login
   PATH_MAPPING.put(PATH_SIGNUP, "signup");
   PATH_MAPPING.put(PATH_ERROR, "error");
   PATH_MAPPING.put(PATH_FEED_ADMIN, "feed_admin");
   PATH_MAPPING.put(PATH_SETTINGS, "settings");
   PATH_MAPPING.put(PATH_FEEDS, "feeds");
   PATH_MAPPING.put(PATH_FEED + "/*", "feed");
   PATH_MAPPING.put(PATH_ADMIN, "admin");
 }
Ejemplo n.º 5
0
 public boolean startProxy(long from, long to) {
   // lazy init for proxy support check quickly in isReadable
   if (ProxyConnections == null) {
     ProxyConnections = new HashMap<Long, EventableChannel>();
   }
   EventableChannel target = Connections.get(to);
   if (target != null) {
     ProxyConnections.put(from, target);
     return true;
   } else {
     return false;
   }
 }
Ejemplo n.º 6
0
  void close() {
    try {
      if (mySelector != null) mySelector.close();
    } catch (IOException e) {
    }
    mySelector = null;

    // run down open connections and sockets.
    Iterator<ServerSocketChannel> i = Acceptors.values().iterator();
    while (i.hasNext()) {
      try {
        i.next().close();
      } catch (IOException e) {
      }
    }

    // 29Sep09: We create an ArrayList of the existing connections, then iterate over
    // that to call unbind on them. This is because an unbind can trigger a reconnect,
    // which will add to the Connections HashMap, causing a ConcurrentModificationException.
    // XXX: The correct behavior here would be to latch the various reactor methods to return
    // immediately if the reactor is shutting down.
    ArrayList<EventableChannel> conns = new ArrayList<EventableChannel>();
    Iterator<EventableChannel> i2 = Connections.values().iterator();
    while (i2.hasNext()) {
      EventableChannel ec = i2.next();
      if (ec != null) {
        conns.add(ec);
      }
    }
    Connections.clear();

    ListIterator<EventableChannel> i3 = conns.listIterator(0);
    while (i3.hasNext()) {
      EventableChannel ec = i3.next();
      eventCallback(ec.getBinding(), EM_CONNECTION_UNBOUND, null);
      ec.close();

      EventableSocketChannel sc = (EventableSocketChannel) ec;
      if (sc != null && sc.isAttached()) DetachedConnections.add(sc);
    }

    ListIterator<EventableSocketChannel> i4 = DetachedConnections.listIterator(0);
    while (i4.hasNext()) {
      EventableSocketChannel ec = i4.next();
      ec.cleanup();
    }
    DetachedConnections.clear();
  }
Ejemplo n.º 7
0
  void isReadable(SelectionKey k) {
    EventableChannel ec = (EventableChannel) k.attachment();
    long b = ec.getBinding();

    if (ec.isWatchOnly()) {
      if (ec.isNotifyReadable()) eventCallback(b, EM_CONNECTION_NOTIFY_READABLE, null);
    } else {
      myReadBuffer.clear();

      try {
        ec.readInboundData(myReadBuffer);
        myReadBuffer.flip();
        if (myReadBuffer.limit() > 0) {
          if (ProxyConnections != null) {
            EventableChannel target = ProxyConnections.get(b);
            if (target != null) {
              ByteBuffer myWriteBuffer = ByteBuffer.allocate(myReadBuffer.limit());
              myWriteBuffer.put(myReadBuffer);
              myWriteBuffer.flip();
              target.scheduleOutboundData(myWriteBuffer);
            } else {
              eventCallback(b, EM_CONNECTION_READ, myReadBuffer);
            }
          } else {
            eventCallback(b, EM_CONNECTION_READ, myReadBuffer);
          }
        }
      } catch (IOException e) {
        UnboundConnections.add(b);
      }
    }
  }
Ejemplo n.º 8
0
 public SocketChannel detachChannel(long sig) {
   EventableSocketChannel ec = (EventableSocketChannel) Connections.get(sig);
   if (ec != null) {
     UnboundConnections.add(sig);
     return ec.getChannel();
   } else {
     return null;
   }
 }
Ejemplo n.º 9
0
 public long openUdpSocket(InetSocketAddress address) throws IOException {
   // TODO, don't throw an exception out of here.
   DatagramChannel dg = DatagramChannel.open();
   dg.configureBlocking(false);
   dg.socket().bind(address);
   long b = createBinding();
   EventableChannel ec = new EventableDatagramChannel(dg, b, mySelector);
   dg.register(mySelector, SelectionKey.OP_READ, ec);
   Connections.put(b, ec);
   return b;
 }
Ejemplo n.º 10
0
  void removeUnboundConnections() {
    if (UnboundConnections.size() == 0) {
      return;
    }
    ArrayList<Long> currentUnboundConnections = UnboundConnections;
    // fix concurrent modification exception
    UnboundConnections = new ArrayList<Long>();
    for (long b : currentUnboundConnections) {
      EventableChannel ec = Connections.remove(b);
      if (ec != null) {
        if (ProxyConnections != null) {
          ProxyConnections.remove(b);
        }
        eventCallback(b, EM_CONNECTION_UNBOUND, null);
        ec.close();

        EventableSocketChannel sc = (EventableSocketChannel) ec;
        if (sc != null && sc.isAttached()) DetachedConnections.add(sc);
      }
    }
  }
Ejemplo n.º 11
0
 public long startTcpServer(SocketAddress sa) throws EmReactorException {
   try {
     ServerSocketChannel server = ServerSocketChannel.open();
     server.configureBlocking(false);
     server.socket().bind(sa);
     long s = createBinding();
     Acceptors.put(s, server);
     server.register(mySelector, SelectionKey.OP_ACCEPT, s);
     return s;
   } catch (IOException e) {
     throw new EmReactorException("unable to open socket acceptor: " + e.toString());
   }
 }
Ejemplo n.º 12
0
  public long attachChannel(SocketChannel sc, boolean watch_mode) {
    long b = createBinding();

    EventableSocketChannel ec = new EventableSocketChannel(sc, b, mySelector);

    ec.setAttached();
    if (watch_mode) ec.setWatchOnly();

    Connections.put(b, ec);
    NewConnections.add(b);

    return b;
  }
Ejemplo n.º 13
0
  void isAcceptable(SelectionKey k) {
    ServerSocketChannel ss = (ServerSocketChannel) k.channel();
    SocketChannel sn;
    long b;

    for (int n = 0; n < 10; n++) {
      try {
        sn = ss.accept();
        if (sn == null) break;
      } catch (IOException e) {
        e.printStackTrace();
        k.cancel();

        ServerSocketChannel server = Acceptors.remove(k.attachment());
        if (server != null)
          try {
            server.close();
          } catch (IOException ex) {
          }
        ;
        break;
      }

      try {
        sn.configureBlocking(false);
      } catch (IOException e) {
        e.printStackTrace();
        continue;
      }

      b = createBinding();
      EventableSocketChannel ec = new EventableSocketChannel(sn, b, mySelector);
      Connections.put(b, ec);
      NewConnections.add(b);

      eventCallback(((Long) k.attachment()).longValue(), EM_CONNECTION_ACCEPTED, null, b);
    }
  }
Ejemplo n.º 14
0
	// Recursive method to walk the tree below a node and set
        // a new parent logger.
        void walkAndSetParent(Logger parent) {
	    if (children == null) {
	        return;
	    }
	    Iterator values = children.values().iterator();
	    while (values.hasNext()) {
	        LogNode node = (LogNode) values.next();
	        if (node.logger == null) {
	    	    node.walkAndSetParent(parent);
	        } else {
	            doSetParent(node.logger, parent);
		}
	    }
	}
  /**
   * Construct a "simplified name" based on the subject DN from the certificate. The purpose is to
   * have something shorter to display in the list. The name used is one of the following DN parts,
   * if available, otherwise the complete DN: 'CN', 'OU' or else 'O'.
   *
   * @param cert to read subject DN from
   * @return the simplified name
   */
  private static String getSimplifiedName(X509Certificate cert) {
    final HashMap<String, String> parts = new HashMap<String, String>();
    try {
      for (Rdn name : new LdapName(cert.getSubjectX500Principal().getName()).getRdns()) {
        if (name.getType() != null && name.getValue() != null) {
          parts.put(name.getType(), name.getValue().toString());
        }
      }
    } catch (InvalidNameException ignored) // NOPMD
    {
    }

    String result = parts.get("CN");
    if (result == null) {
      result = parts.get("OU");
    }
    if (result == null) {
      result = parts.get("O");
    }
    if (result == null) {
      result = cert.getSubjectX500Principal().getName();
    }
    return result;
  }
Ejemplo n.º 16
0
 // Recursive method to walk the tree below a node and set
 // a new parent logger.
 void walkAndSetParent(Logger parent) {
   if (children == null) {
     return;
   }
   Iterator<LogNode> values = children.values().iterator();
   while (values.hasNext()) {
     LogNode node = values.next();
     LoggerWeakRef ref = node.loggerRef;
     Logger logger = (ref == null) ? null : ref.get();
     if (logger == null) {
       node.walkAndSetParent(parent);
     } else {
       doSetParent(logger, parent);
     }
   }
 }
Ejemplo n.º 17
0
  void removeUnboundConnections() {
    ListIterator<Long> iter = UnboundConnections.listIterator(0);
    while (iter.hasNext()) {
      long b = iter.next();

      EventableChannel ec = Connections.remove(b);
      if (ec != null) {
        eventCallback(b, EM_CONNECTION_UNBOUND, null);
        ec.close();

        EventableSocketChannel sc = (EventableSocketChannel) ec;
        if (sc != null && sc.isAttached()) DetachedConnections.add(sc);
      }
    }
    UnboundConnections.clear();
  }
Ejemplo n.º 18
0
 /** Get a class loader. Create in a restricted context */
 synchronized AppletClassLoader getClassLoader(final URL codebase, final String key) {
   AppletClassLoader c = classloaders.get(key);
   if (c == null) {
     AccessControlContext acc = getAccessControlContext(codebase);
     c =
         AccessController.doPrivileged(
             new PrivilegedAction<AppletClassLoader>() {
               @Override
               public AppletClassLoader run() {
                 AppletClassLoader ac = createClassLoader(codebase);
                 /* Should the creation of the classloader be
                  * within the class synchronized block?  Since
                  * this class is used by the plugin, take care
                  * to avoid deadlocks, or specialize
                  * AppletPanel within the plugin.  It may take
                  * an arbitrary amount of time to create a
                  * class loader (involving getting Jar files
                  * etc.) and may block unrelated applets from
                  * finishing createAppletThread (due to the
                  * class synchronization). If
                  * createAppletThread does not finish quickly,
                  * the applet cannot process other messages,
                  * particularly messages such as destroy
                  * (which timeout when called from the browser).
                  */
                 synchronized (getClass()) {
                   AppletClassLoader res = classloaders.get(key);
                   if (res == null) {
                     classloaders.put(key, ac);
                     return ac;
                   } else {
                     return res;
                   }
                 }
               }
             },
             acc);
   }
   return c;
 }
Ejemplo n.º 19
0
  void addNewConnections() {
    ListIterator<EventableSocketChannel> iter = DetachedConnections.listIterator(0);
    while (iter.hasNext()) {
      EventableSocketChannel ec = iter.next();
      ec.cleanup();
    }
    DetachedConnections.clear();

    ListIterator<Long> iter2 = NewConnections.listIterator(0);
    while (iter2.hasNext()) {
      long b = iter2.next();

      EventableChannel ec = Connections.get(b);
      if (ec != null) {
        try {
          ec.register();
        } catch (ClosedChannelException e) {
          UnboundConnections.add(ec.getBinding());
        }
      }
    }
    NewConnections.clear();
  }
Ejemplo n.º 20
0
  public long connectTcpServer(String bindAddr, int bindPort, String address, int port) {
    long b = createBinding();

    try {
      SocketChannel sc = SocketChannel.open();
      sc.configureBlocking(false);
      if (bindAddr != null) sc.socket().bind(new InetSocketAddress(bindAddr, bindPort));

      EventableSocketChannel ec = new EventableSocketChannel(sc, b, mySelector);

      if (sc.connect(new InetSocketAddress(address, port))) {
        // Connection returned immediately. Can happen with localhost connections.
        // WARNING, this code is untested due to lack of available test conditions.
        // Ought to be be able to come here from a localhost connection, but that
        // doesn't happen on Linux. (Maybe on FreeBSD?)
        // The reason for not handling this until we can test it is that we
        // really need to return from this function WITHOUT triggering any EM events.
        // That's because until the user code has seen the signature we generated here,
        // it won't be able to properly dispatch them. The C++ EM deals with this
        // by setting pending mode as a flag in ALL eventable descriptors and making
        // the descriptor select for writable. Then, it can send UNBOUND and
        // CONNECTION_COMPLETED on the next pass through the loop, because writable will
        // fire.
        throw new RuntimeException("immediate-connect unimplemented");
      } else {
        ec.setConnectPending();
        Connections.put(b, ec);
        NewConnections.add(b);
      }
    } catch (IOException e) {
      // Can theoretically come here if a connect failure can be determined immediately.
      // I don't know how to make that happen for testing purposes.
      throw new RuntimeException("immediate-connect unimplemented: " + e.toString());
    }
    return b;
  }
Ejemplo n.º 21
0
 public void setCommInactivityTimeout(long sig, long mills) {
   Connections.get(sig).setCommInactivityTimeout(mills);
 }
Ejemplo n.º 22
0
 public void stopProxy(long from) {
   ProxyConnections.remove(from);
 }
Ejemplo n.º 23
0
 public int getConnectionCount() {
   return Connections.size() + Acceptors.size();
 }
Ejemplo n.º 24
0
 public boolean isNotifyWritable(long sig) {
   return Connections.get(sig).isNotifyWritable();
 }
Ejemplo n.º 25
0
 public void setNotifyWritable(long sig, boolean mode) {
   ((EventableSocketChannel) Connections.get(sig)).setNotifyWritable(mode);
 }
Ejemplo n.º 26
0
 public Object[] getPeerName(long sig) {
   return Connections.get(sig).getPeerName();
 }
Ejemplo n.º 27
0
 public void startTls(long sig) throws NoSuchAlgorithmException, KeyManagementException {
   Connections.get(sig).startTls();
 }
Ejemplo n.º 28
0
 public void closeConnection(long sig, boolean afterWriting) {
   EventableChannel ec = Connections.get(sig);
   if (ec != null) if (ec.scheduleClose(afterWriting)) UnboundConnections.add(sig);
 }
Ejemplo n.º 29
0
  private int getVKCode(int charCode, int keyCode) {
    int keyboardCode = 0;
    if (charCode >= 32) {
      // if it is printable, then it lives in our hashmap
      KeyEvent event = (KeyEvent) charMap.get(new Integer(charCode));
      keyboardCode = event.getKeyCode();
    } else {
      switch (keyCode) {
        case 13:
          keyboardCode = KeyEvent.VK_ENTER;
          break;
        case 8:
          keyboardCode = KeyEvent.VK_BACK_SPACE;
          break;
        case 25: // shift tab for Safari
        case 9:
          keyboardCode = KeyEvent.VK_TAB;
          break;
        case 12:
          keyboardCode = KeyEvent.VK_CLEAR;
          break;
        case 16:
          keyboardCode = KeyEvent.VK_SHIFT;
          break;
        case 17:
          keyboardCode = KeyEvent.VK_CONTROL;
          break;
        case 18:
          keyboardCode = KeyEvent.VK_ALT;
          break;
        case 63250:
        case 19:
          keyboardCode = KeyEvent.VK_PAUSE;
          break;
        case 20:
          keyboardCode = KeyEvent.VK_CAPS_LOCK;
          break;
        case 27:
          keyboardCode = KeyEvent.VK_ESCAPE;
          break;
        case 32:
          log("it's a space");
          keyboardCode = KeyEvent.VK_SPACE;
          break;
        case 63276:
        case 33:
          keyboardCode = KeyEvent.VK_PAGE_UP;
          break;
        case 63277:
        case 34:
          keyboardCode = KeyEvent.VK_PAGE_DOWN;
          break;
        case 63275:
        case 35:
          keyboardCode = KeyEvent.VK_END;
          break;
        case 63273:
        case 36:
          keyboardCode = KeyEvent.VK_HOME;
          break;

          /** Constant for the <b>left</b> arrow key. */
        case 63234:
        case 37:
          keyboardCode = KeyEvent.VK_LEFT;
          break;

          /** Constant for the <b>up</b> arrow key. */
        case 63232:
        case 38:
          keyboardCode = KeyEvent.VK_UP;
          break;

          /** Constant for the <b>right</b> arrow key. */
        case 63235:
        case 39:
          keyboardCode = KeyEvent.VK_RIGHT;
          break;

          /** Constant for the <b>down</b> arrow key. */
        case 63233:
        case 40:
          keyboardCode = KeyEvent.VK_DOWN;
          break;
        case 63272:
        case 46:
          keyboardCode = KeyEvent.VK_DELETE;
          break;
        case 63289:
        case 144:
          keyboardCode = KeyEvent.VK_NUM_LOCK;
          break;
        case 63249:
        case 145:
          keyboardCode = KeyEvent.VK_SCROLL_LOCK;
          break;

          /** Constant for the F1 function key. */
        case 63236:
        case 112:
          keyboardCode = KeyEvent.VK_F1;
          break;

          /** Constant for the F2 function key. */
        case 63237:
        case 113:
          keyboardCode = KeyEvent.VK_F2;
          break;

          /** Constant for the F3 function key. */
        case 63238:
        case 114:
          keyboardCode = KeyEvent.VK_F3;
          break;

          /** Constant for the F4 function key. */
        case 63239:
        case 115:
          keyboardCode = KeyEvent.VK_F4;
          break;

          /** Constant for the F5 function key. */
        case 63240:
        case 116:
          keyboardCode = KeyEvent.VK_F5;
          break;

          /** Constant for the F6 function key. */
        case 63241:
        case 117:
          keyboardCode = KeyEvent.VK_F6;
          break;

          /** Constant for the F7 function key. */
        case 63242:
        case 118:
          keyboardCode = KeyEvent.VK_F7;
          break;

          /** Constant for the F8 function key. */
        case 63243:
        case 119:
          keyboardCode = KeyEvent.VK_F8;
          break;

          /** Constant for the F9 function key. */
        case 63244:
        case 120:
          keyboardCode = KeyEvent.VK_F9;
          break;

          /** Constant for the F10 function key. */
        case 63245:
        case 121:
          keyboardCode = KeyEvent.VK_F10;
          break;

          /** Constant for the F11 function key. */
        case 63246:
        case 122:
          keyboardCode = KeyEvent.VK_F11;
          break;

          /** Constant for the F12 function key. */
        case 63247:
        case 123:
          keyboardCode = KeyEvent.VK_F12;
          break;

          /**
           * Constant for the F13 function key.
           *
           * @since 1.2
           */
          /*
           * F13 - F24 are used on IBM 3270 keyboard; break; use
           * random range for constants.
           */
        case 124:
          keyboardCode = KeyEvent.VK_F13;
          break;

          /**
           * Constant for the F14 function key.
           *
           * @since 1.2
           */
        case 125:
          keyboardCode = KeyEvent.VK_F14;
          break;

          /**
           * Constant for the F15 function key.
           *
           * @since 1.2
           */
        case 126:
          keyboardCode = KeyEvent.VK_F15;
          break;

        case 63302:
        case 45:
          keyboardCode = KeyEvent.VK_INSERT;
          break;
        case 47:
          keyboardCode = KeyEvent.VK_HELP;
          break;
      }
    }
    log("Attempting to type " + (char) charCode + ":" + charCode + " " + keyCode);
    log("Converted to " + keyboardCode);
    return keyboardCode;
  }
Ejemplo n.º 30
0
 public void sendDatagram(long sig, ByteBuffer bb, String recipAddress, int recipPort) {
   (Connections.get(sig)).scheduleOutboundDatagram(bb, recipAddress, recipPort);
 }