Example usage for java.util Enumeration Enumeration

List of usage examples for java.util Enumeration Enumeration

Introduction

In this page you can find the example usage for java.util Enumeration Enumeration.

Prototype

Enumeration

Source Link

Usage

From source file:com.netflix.zuul.http.HttpServletRequestWrapper.java

/**
 * This method is safe to execute multiple times.
 *
 * @see javax.servlet.ServletRequest#getParameterNames()
 *///w  w  w.  ja  va 2s.  c  o m
@SuppressWarnings("unchecked")
public Enumeration getParameterNames() {
    try {
        parseRequest();
    } catch (IOException e) {
        throw new IllegalStateException("Cannot parse the request!", e);
    }
    return new Enumeration<String>() {
        private String[] arr = getParameters().keySet().toArray(new String[0]);
        private int idx = 0;

        public boolean hasMoreElements() {
            return idx < arr.length;
        }

        public String nextElement() {
            return arr[idx++];
        }

    };
}

From source file:io.apiman.common.plugin.PluginClassLoader.java

/**
 * @see java.lang.ClassLoader#findResources(java.lang.String)
 *//*from   w w  w  .j  a v a2  s. c  om*/
@Override
protected Enumeration<URL> findResources(String name) throws IOException {
    List<URL> resources = new ArrayList<>();

    // Search for the artifact in the plugin WAR itself
    Enumeration<? extends ZipEntry> entries = this.pluginArtifactZip.entries();
    while (entries.hasMoreElements()) {
        ZipEntry zipEntry = entries.nextElement();
        if (zipEntry.getName().equalsIgnoreCase(name)) {
            try {
                resources.add(extractResource(zipEntry));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    // Now also add any resources found in dependencies
    ZipEntry entry;
    File file;
    for (ZipFile zipFile : this.dependencyZips) {
        entry = zipFile.getEntry(name);
        if (entry != null) {
            try {
                file = new File(zipFile.getName());
                URL zipUrl = file.toURI().toURL();
                URL entryUrl = new URL("jar:" + zipUrl + "!/" + name);
                resources.add(entryUrl);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
    }

    // Return the discovered resources as an Enumeration
    final Iterator<URL> iterator = resources.iterator();
    return new Enumeration<URL>() {
        @Override
        public boolean hasMoreElements() {
            return iterator.hasNext();
        }

        @Override
        public URL nextElement() {
            return iterator.next();
        }
    };
}

From source file:net.wastl.webmail.xml.XMLSystemData.java

public WebMailVirtualDomain getVirtualDomain(String domname) {
    // Check if virtual domains are disabled
    if (getVirtuals() == false) {
        // No, default to localhost
        return new WebMailVirtualDomain() {
            public String getDomainName() {
                return "localhost";
            }//from  w w  w .j  ava2 s  .co  m

            public void setDomainName(String name) throws Exception {
                log.error("Ignoring DefaultDomain.setDomainName().  " + "Should not call this method.");
            }

            public String getDefaultServer() {
                return "localhost";
            }

            public void setDefaultServer(String name) {
                log.error("Ignoring DefaultDomain.setDomainServer().  " + "Should not call this method.");
            }

            public String getAuthenticationHost() {
                return "localhost";
            }

            public void setAuthenticationHost(String name) {
                log.error("Ignoring DefaultDomain.setAuthenticationHost().  " + "Should not call this method.");
            }

            public boolean isAllowedHost(String host) {
                return true;
            }

            public void setAllowedHosts(String hosts) {
                log.error("Ignoring DefaultDomain.setAllowedHosts().  " + "Should not call this method.");
            }

            public Enumeration<String> getAllowedHosts() {
                return new Enumeration<String>() {
                    int i = 0;

                    public boolean hasMoreElements() {
                        return i < 1;
                    }

                    public String nextElement() {
                        i++;
                        return "localhost";
                    }
                };
            }

            public void setHostsRestricted(boolean b) {
                log.error("Ignoring DefaultDomain.setHostsRestricted().  " + "Should not call this method.");
            }

            public boolean getHostsRestricted() {
                return false;
            }

            public String getImapBasedir() {
                return null;
            }
        };
    }
    // Virtual domains are allowed, get that wanted one
    NodeList nodel = sysdata.getElementsByTagName("DOMAIN");
    Element elem = null;
    int j;
    for (j = 0; j < nodel.getLength(); j++) {
        elem = (Element) nodel.item(j);
        elem.normalize();
        NodeList namel = elem.getElementsByTagName("NAME");
        if (namel.getLength() > 0) {
            if (XMLCommon.getElementTextValue((Element) namel.item(0)).equals(domname)) {
                break;
            }
        }
    }
    if (j < nodel.getLength() && elem != null) {
        final Element domain = elem;
        return new WebMailVirtualDomain() {
            public String getDomainName() {
                String value = XMLCommon.getTagValue(domain, "NAME");
                return value == null ? "unknown" : value;
            }

            public void setDomainName(String name) throws Exception {
                XMLCommon.setTagValue(domain, "NAME", name, true, "Virtual Domain names must be unique!");
            }

            public String getDefaultServer() {
                String value = XMLCommon.getTagValue(domain, "DEFAULT_HOST");
                return value == null ? "unknown" : value;
            }

            /* Override the IMAP base directory for this domain,
             * for imap and imaps protocols */
            public String getImapBaseDir() {
                String value = XMLCommon.getTagValue(domain, "IMAP_BASEDIR");
                return value == null ? "unknown" : value;
            }

            public void setDefaultServer(String name) {
                XMLCommon.setTagValue(domain, "DEFAULT_HOST", name);
            }

            public String getAuthenticationHost() {
                String value = XMLCommon.getTagValue(domain, "AUTHENTICATION_HOST");
                return value == null ? "unknown" : value;
            }

            public void setAuthenticationHost(String name) {
                XMLCommon.setTagValue(domain, "AUTHENTICATION_HOST", name);
            }

            public boolean isAllowedHost(String host) {
                if (getHostsRestricted()) {
                    Vector<String> v = new Vector<String>();
                    v.addElement(getDefaultServer());
                    Enumeration<String> e = getAllowedHosts();
                    while (e.hasMoreElements()) {
                        v.addElement(e.nextElement());
                    }
                    Enumeration<String> enumVar = v.elements();
                    while (enumVar.hasMoreElements()) {
                        String next = enumVar.nextElement();
                        if (host.toUpperCase().endsWith(next.toUpperCase())) {
                            return true;
                        }
                    }
                    return false;
                } else {
                    return true;
                }
            }

            public void setAllowedHosts(String hosts) {
                NodeList nl = domain.getElementsByTagName("ALLOWED_HOST");
                for (int i = 0; i < nl.getLength(); i++) {
                    domain.removeChild(nl.item(i));
                }
                StringTokenizer tok = new StringTokenizer(hosts, ", ");
                while (tok.hasMoreElements()) {
                    Element ahost = root.createElement("ALLOWED_HOST");
                    XMLCommon.setElementTextValue(ahost, tok.nextToken());
                    domain.appendChild(ahost);
                }
            }

            public Enumeration<String> getAllowedHosts() {
                final NodeList nl = domain.getElementsByTagName("ALLOWED_HOST");
                return new Enumeration<String>() {
                    int i = 0;

                    public boolean hasMoreElements() {
                        return i < nl.getLength();
                    }

                    public String nextElement() {
                        String value = XMLCommon.getElementTextValue((Element) nl.item(i++));
                        return value == null ? "error" : value;
                    }
                };
            }

            public void setHostsRestricted(boolean b) {
                NodeList nl = domain.getElementsByTagName("RESTRICTED");
                for (int i = 0; i < nl.getLength(); i++) {
                    domain.removeChild(nl.item(i));
                }
                if (b) {
                    domain.appendChild(root.createElement("RESTRICTED"));
                }
            }

            public boolean getHostsRestricted() {
                NodeList nl = domain.getElementsByTagName("RESTRICTED");
                return nl.getLength() > 0;
            }

            public String getImapBasedir() {
                NodeList nl = domain.getElementsByTagName("IMAP_BASEDIR");
                return ((nl.getLength() > 0) ? XMLCommon.getElementTextValue((Element) nl.item(0)) : null);
            }
        };
    } else {
        return null;
    }
}

From source file:net.wastl.webmail.xml.XMLUserData.java

public Enumeration<String> mailHosts() {
    final NodeList nl = getNodeListXPath("//MAILHOST");
    return new Enumeration<String>() {
        int i = 0;

        public boolean hasMoreElements() {
            return i < nl.getLength();
        }//from   w  ww.  j  ava  2s.  co m

        public String nextElement() {
            Element e = (Element) nl.item(i++);
            return e.getAttribute("id");
        }
    };
}

From source file:org.grails.gsp.jsp.GroovyPagesPageContext.java

@SuppressWarnings("rawtypes")
@Override/*from   w  ww  . j  a v  a 2  s  .  com*/
public Enumeration getAttributeNamesInScope(int scope) {
    switch (scope) {
    case PAGE_SCOPE:
        final Iterator i = pageScope.getVariables().keySet().iterator();
        return new Enumeration() {
            @Override
            public boolean hasMoreElements() {
                return i.hasNext();
            }

            @Override
            public Object nextElement() {
                return i.next();
            }
        };
    case REQUEST_SCOPE:
        return request.getAttributeNames();
    case SESSION_SCOPE:
        HttpSession httpSession = request.getSession(false);
        if (httpSession != null) {
            return httpSession.getAttributeNames();
        } else {
            return EMPTY_ENUMERATION;
        }
    case APPLICATION_SCOPE:
        return servletContext.getAttributeNames();
    }
    return EMPTY_ENUMERATION;
}

From source file:org.apache.rocketmq.jms.domain.message.JmsBaseMessage.java

@Override
public Enumeration<?> getPropertyNames() throws JMSException {
    final Object[] keys = this.properties.keySet().toArray();
    return new Enumeration<Object>() {
        int i;//  ww w  .j av  a 2 s.  c  o m

        @Override
        public boolean hasMoreElements() {
            return i < keys.length;
        }

        @Override
        public Object nextElement() {
            return keys[i++];
        }
    };
}

From source file:org.apache.geode.internal.util.CollectionUtilsJUnitTest.java

@Test
public void testAddAllCollectionEnumerationWithEmpty() {
    final ArrayList<String> list = new ArrayList<>(2);

    list.add("one");
    list.add("two");

    boolean modified = CollectionUtils.addAll(list, new Enumeration<String>() {
        @Override//ww w.j  a  v a 2 s .co  m
        public boolean hasMoreElements() {
            return false;
        }

        @Override
        public String nextElement() {
            throw new NoSuchElementException();
        }
    });

    assertTrue(!modified);
    assertEquals(2, list.size());
}

From source file:net.wastl.webmail.server.WebMailServlet.java

@Override
public Enumeration getServers() {
    return new Enumeration() {
        public boolean hasMoreElements() {
            return false;
        }/*ww w. ja va2  s.  c  om*/

        public Object nextElement() {
            return null;
        }
    };
}

From source file:net.lightbody.bmp.proxy.jetty.http.HttpFields.java

/** Get enumeration of header _names.
 * Returns an enumeration of strings representing the header _names
 * for this request. /*from  ww w . j a  v a 2  s.c  o m*/
 */
public Enumeration getFieldNames() {
    return new Enumeration() {
        int i = 0;
        Field field = null;

        public boolean hasMoreElements() {
            if (field != null)
                return true;
            while (i < _fields.size()) {
                Field f = (Field) _fields.get(i++);
                if (f != null && f._version == _version && f._prev == null) {
                    field = f;
                    return true;
                }
            }
            return false;
        }

        public Object nextElement() throws NoSuchElementException {
            if (field != null || hasMoreElements()) {
                String n = field._info._name;
                field = null;
                return n;
            }
            throw new NoSuchElementException();
        }
    };
}

From source file:net.lightbody.bmp.proxy.jetty.http.HttpFields.java

/** Get multi headers
 * @return Enumeration of the values, or null if no such header.
 * @param name the case-insensitive field name
 *///from  w  ww. j  a  v  a2  s.co  m
public Enumeration getValues(String name) {
    FieldInfo info = getFieldInfo(name);
    final Field field = getField(info, true);

    if (field != null) {
        return new Enumeration() {
            Field f = field;

            public boolean hasMoreElements() {
                while (f != null && f._version != _version)
                    f = f._next;
                return f != null;
            }

            public Object nextElement() throws NoSuchElementException {
                if (f == null)
                    throw new NoSuchElementException();
                Field n = f;
                do
                    f = f._next;
                while (f != null && f._version != _version);
                return n._value;
            }
        };
    }
    return null;
}