Example usage for org.w3c.dom Element getTagName

List of usage examples for org.w3c.dom Element getTagName

Introduction

In this page you can find the example usage for org.w3c.dom Element getTagName.

Prototype

public String getTagName();

Source Link

Document

The name of the element.

Usage

From source file:org.apache.impala.yarn.server.resourcemanager.scheduler.fair.AllocationFileLoaderService.java

/**
 * Updates the allocation list from the allocation config file. This file is
 * expected to be in the XML format specified in the design doc.
 *
 * @throws IOException if the config file cannot be read.
 * @throws AllocationConfigurationException if allocations are invalid.
 * @throws ParserConfigurationException if XML parser is misconfigured.
 * @throws SAXException if config file is malformed.
 *//*  ww  w  . j av a 2s  .c  o  m*/
public synchronized void reloadAllocations()
        throws IOException, ParserConfigurationException, SAXException, AllocationConfigurationException {
    if (allocFile == null) {
        return;
    }
    LOG.info("Loading allocation file " + allocFile);
    // Create some temporary hashmaps to hold the new allocs, and we only save
    // them in our fields if we have parsed the entire allocs file successfully.
    Map<String, Resource> minQueueResources = new HashMap<>();
    Map<String, Resource> maxQueueResources = new HashMap<>();
    Map<String, Resource> maxChildQueueResources = new HashMap<>();
    Map<String, Integer> queueMaxApps = new HashMap<>();
    Map<String, Integer> userMaxApps = new HashMap<>();
    Map<String, Float> queueMaxAMShares = new HashMap<>();
    Map<String, ResourceWeights> queueWeights = new HashMap<>();
    Map<String, SchedulingPolicy> queuePolicies = new HashMap<>();
    Map<String, Long> minSharePreemptionTimeouts = new HashMap<>();
    Map<String, Long> fairSharePreemptionTimeouts = new HashMap<>();
    Map<String, Float> fairSharePreemptionThresholds = new HashMap<>();
    Map<String, Map<QueueACL, AccessControlList>> queueAcls = new HashMap<>();
    Set<String> nonPreemptableQueues = new HashSet<>();
    int userMaxAppsDefault = Integer.MAX_VALUE;
    int queueMaxAppsDefault = Integer.MAX_VALUE;
    Resource queueMaxResourcesDefault = Resources.unbounded();
    float queueMaxAMShareDefault = 0.5f;
    long defaultFairSharePreemptionTimeout = Long.MAX_VALUE;
    long defaultMinSharePreemptionTimeout = Long.MAX_VALUE;
    float defaultFairSharePreemptionThreshold = 0.5f;
    SchedulingPolicy defaultSchedPolicy = SchedulingPolicy.DEFAULT_POLICY;

    QueuePlacementPolicy newPlacementPolicy = null;

    // Remember all queue names so we can display them on web UI, etc.
    // configuredQueues is segregated based on whether it is a leaf queue
    // or a parent queue. This information is used for creating queues
    // and also for making queue placement decisions(QueuePlacementRule.java).
    Map<FSQueueType, Set<String>> configuredQueues = new HashMap<>();

    for (FSQueueType queueType : FSQueueType.values()) {
        configuredQueues.put(queueType, new HashSet<String>());
    }

    // Read and parse the allocations file.
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    docBuilderFactory.setIgnoringComments(true);
    DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
    Document doc = builder.parse(allocFile);
    Element root = doc.getDocumentElement();
    if (!"allocations".equals(root.getTagName()))
        throw new AllocationConfigurationException(
                "Bad fair scheduler config " + "file: top-level element not <allocations>");
    NodeList elements = root.getChildNodes();
    List<Element> queueElements = new ArrayList<Element>();
    Element placementPolicyElement = null;
    for (int i = 0; i < elements.getLength(); i++) {
        Node node = elements.item(i);
        if (node instanceof Element) {
            Element element = (Element) node;
            if ("queue".equals(element.getTagName()) || "pool".equals(element.getTagName())) {
                queueElements.add(element);
            } else if ("user".equals(element.getTagName())) {
                String userName = element.getAttribute("name");
                NodeList fields = element.getChildNodes();
                for (int j = 0; j < fields.getLength(); j++) {
                    Node fieldNode = fields.item(j);
                    if (!(fieldNode instanceof Element))
                        continue;
                    Element field = (Element) fieldNode;
                    if ("maxRunningApps".equals(field.getTagName())) {
                        String text = ((Text) field.getFirstChild()).getData().trim();
                        int val = Integer.parseInt(text);
                        userMaxApps.put(userName, val);
                    }
                }
            } else if ("queueMaxResourcesDefault".equals(element.getTagName())) {
                String text = ((Text) element.getFirstChild()).getData().trim();
                Resource val = FairSchedulerConfiguration.parseResourceConfigValue(text);
                queueMaxResourcesDefault = val;
            } else if ("userMaxAppsDefault".equals(element.getTagName())) {
                String text = ((Text) element.getFirstChild()).getData().trim();
                int val = Integer.parseInt(text);
                userMaxAppsDefault = val;
            } else if ("defaultFairSharePreemptionTimeout".equals(element.getTagName())) {
                String text = ((Text) element.getFirstChild()).getData().trim();
                long val = Long.parseLong(text) * 1000L;
                defaultFairSharePreemptionTimeout = val;
            } else if ("fairSharePreemptionTimeout".equals(element.getTagName())) {
                if (defaultFairSharePreemptionTimeout == Long.MAX_VALUE) {
                    String text = ((Text) element.getFirstChild()).getData().trim();
                    long val = Long.parseLong(text) * 1000L;
                    defaultFairSharePreemptionTimeout = val;
                }
            } else if ("defaultMinSharePreemptionTimeout".equals(element.getTagName())) {
                String text = ((Text) element.getFirstChild()).getData().trim();
                long val = Long.parseLong(text) * 1000L;
                defaultMinSharePreemptionTimeout = val;
            } else if ("defaultFairSharePreemptionThreshold".equals(element.getTagName())) {
                String text = ((Text) element.getFirstChild()).getData().trim();
                float val = Float.parseFloat(text);
                val = Math.max(Math.min(val, 1.0f), 0.0f);
                defaultFairSharePreemptionThreshold = val;
            } else if ("queueMaxAppsDefault".equals(element.getTagName())) {
                String text = ((Text) element.getFirstChild()).getData().trim();
                int val = Integer.parseInt(text);
                queueMaxAppsDefault = val;
            } else if ("queueMaxAMShareDefault".equals(element.getTagName())) {
                String text = ((Text) element.getFirstChild()).getData().trim();
                float val = Float.parseFloat(text);
                val = Math.min(val, 1.0f);
                queueMaxAMShareDefault = val;
            } else if ("defaultQueueSchedulingPolicy".equals(element.getTagName())
                    || "defaultQueueSchedulingMode".equals(element.getTagName())) {
                String text = ((Text) element.getFirstChild()).getData().trim();
                if (text.equalsIgnoreCase("FIFO")) {
                    throw new AllocationConfigurationException(
                            "Bad fair scheduler " + "config file: defaultQueueSchedulingPolicy or "
                                    + "defaultQueueSchedulingMode can't be FIFO.");
                }
                defaultSchedPolicy = SchedulingPolicy.parse(text);
            } else if ("queuePlacementPolicy".equals(element.getTagName())) {
                placementPolicyElement = element;
            } else {
                LOG.warn("Bad element in allocations file: " + element.getTagName());
            }
        }
    }

    // Load queue elements.  A root queue can either be included or omitted.  If
    // it's included, all other queues must be inside it.
    for (Element element : queueElements) {
        String parent = "root";
        if (element.getAttribute("name").equalsIgnoreCase("root")) {
            if (queueElements.size() > 1) {
                throw new AllocationConfigurationException(
                        "If configuring root queue," + " no other queues can be placed alongside it.");
            }
            parent = null;
        }
        loadQueue(parent, element, minQueueResources, maxQueueResources, maxChildQueueResources, queueMaxApps,
                userMaxApps, queueMaxAMShares, queueWeights, queuePolicies, minSharePreemptionTimeouts,
                fairSharePreemptionTimeouts, fairSharePreemptionThresholds, queueAcls, configuredQueues,
                nonPreemptableQueues);
    }

    // Load placement policy and pass it configured queues
    Configuration conf = getConfig();
    if (placementPolicyElement != null) {
        newPlacementPolicy = QueuePlacementPolicy.fromXml(placementPolicyElement, configuredQueues, conf);
    } else {
        newPlacementPolicy = QueuePlacementPolicy.fromConfiguration(conf, configuredQueues);
    }

    // Set the min/fair share preemption timeout for the root queue
    if (!minSharePreemptionTimeouts.containsKey("root")) {
        minSharePreemptionTimeouts.put("root", defaultMinSharePreemptionTimeout);
    }
    if (!fairSharePreemptionTimeouts.containsKey("root")) {
        fairSharePreemptionTimeouts.put("root", defaultFairSharePreemptionTimeout);
    }

    // Set the fair share preemption threshold for the root queue
    if (!fairSharePreemptionThresholds.containsKey("root")) {
        fairSharePreemptionThresholds.put("root", defaultFairSharePreemptionThreshold);
    }

    AllocationConfiguration info = new AllocationConfiguration(minQueueResources, maxQueueResources,
            maxChildQueueResources, queueMaxApps, userMaxApps, queueWeights, queueMaxAMShares,
            userMaxAppsDefault, queueMaxAppsDefault, queueMaxResourcesDefault, queueMaxAMShareDefault,
            queuePolicies, defaultSchedPolicy, minSharePreemptionTimeouts, fairSharePreemptionTimeouts,
            fairSharePreemptionThresholds, queueAcls, newPlacementPolicy, configuredQueues,
            nonPreemptableQueues);
    lastSuccessfulReload = clock.getTime();
    lastReloadAttemptFailed = false;

    reloadListener.onReload(info);
}

From source file:org.apache.impala.yarn.server.resourcemanager.scheduler.fair.AllocationFileLoaderService.java

/**
 * Loads a queue from a queue element in the configuration file
 *///ww w. j  a  v a 2 s  .c om
private void loadQueue(String parentName, Element element, Map<String, Resource> minQueueResources,
        Map<String, Resource> maxQueueResources, Map<String, Resource> maxChildQueueResources,
        Map<String, Integer> queueMaxApps, Map<String, Integer> userMaxApps,
        Map<String, Float> queueMaxAMShares, Map<String, ResourceWeights> queueWeights,
        Map<String, SchedulingPolicy> queuePolicies, Map<String, Long> minSharePreemptionTimeouts,
        Map<String, Long> fairSharePreemptionTimeouts, Map<String, Float> fairSharePreemptionThresholds,
        Map<String, Map<QueueACL, AccessControlList>> queueAcls, Map<FSQueueType, Set<String>> configuredQueues,
        Set<String> nonPreemptableQueues) throws AllocationConfigurationException {
    String queueName = CharMatcher.WHITESPACE.trimFrom(element.getAttribute("name"));

    if (queueName.contains(".")) {
        throw new AllocationConfigurationException("Bad fair scheduler config " + "file: queue name ("
                + queueName + ") shouldn't contain period.");
    }

    if (queueName.isEmpty()) {
        throw new AllocationConfigurationException("Bad fair scheduler config "
                + "file: queue name shouldn't be empty or " + "consist only of whitespace.");
    }

    if (parentName != null) {
        queueName = parentName + "." + queueName;
    }

    Map<QueueACL, AccessControlList> acls = new HashMap<>();
    NodeList fields = element.getChildNodes();
    boolean isLeaf = true;

    for (int j = 0; j < fields.getLength(); j++) {
        Node fieldNode = fields.item(j);
        if (!(fieldNode instanceof Element))
            continue;
        Element field = (Element) fieldNode;
        if ("minResources".equals(field.getTagName())) {
            String text = ((Text) field.getFirstChild()).getData().trim();
            Resource val = FairSchedulerConfiguration.parseResourceConfigValue(text);
            minQueueResources.put(queueName, val);
        } else if ("maxResources".equals(field.getTagName())) {
            String text = ((Text) field.getFirstChild()).getData().trim();
            Resource val = FairSchedulerConfiguration.parseResourceConfigValue(text);
            maxQueueResources.put(queueName, val);
        } else if ("maxChildResources".equals(field.getTagName())) {
            String text = ((Text) field.getFirstChild()).getData().trim();
            Resource val = FairSchedulerConfiguration.parseResourceConfigValue(text);
            maxChildQueueResources.put(queueName, val);
        } else if ("maxRunningApps".equals(field.getTagName())) {
            String text = ((Text) field.getFirstChild()).getData().trim();
            int val = Integer.parseInt(text);
            queueMaxApps.put(queueName, val);
        } else if ("maxAMShare".equals(field.getTagName())) {
            String text = ((Text) field.getFirstChild()).getData().trim();
            float val = Float.parseFloat(text);
            val = Math.min(val, 1.0f);
            queueMaxAMShares.put(queueName, val);
        } else if ("weight".equals(field.getTagName())) {
            String text = ((Text) field.getFirstChild()).getData().trim();
            double val = Double.parseDouble(text);
            queueWeights.put(queueName, new ResourceWeights((float) val));
        } else if ("minSharePreemptionTimeout".equals(field.getTagName())) {
            String text = ((Text) field.getFirstChild()).getData().trim();
            long val = Long.parseLong(text) * 1000L;
            minSharePreemptionTimeouts.put(queueName, val);
        } else if ("fairSharePreemptionTimeout".equals(field.getTagName())) {
            String text = ((Text) field.getFirstChild()).getData().trim();
            long val = Long.parseLong(text) * 1000L;
            fairSharePreemptionTimeouts.put(queueName, val);
        } else if ("fairSharePreemptionThreshold".equals(field.getTagName())) {
            String text = ((Text) field.getFirstChild()).getData().trim();
            float val = Float.parseFloat(text);
            val = Math.max(Math.min(val, 1.0f), 0.0f);
            fairSharePreemptionThresholds.put(queueName, val);
        } else if ("schedulingPolicy".equals(field.getTagName())
                || "schedulingMode".equals(field.getTagName())) {
            String text = ((Text) field.getFirstChild()).getData().trim();
            SchedulingPolicy policy = SchedulingPolicy.parse(text);
            queuePolicies.put(queueName, policy);
        } else if ("aclSubmitApps".equals(field.getTagName())) {
            String text = ((Text) field.getFirstChild()).getData();
            acls.put(QueueACL.SUBMIT_APPLICATIONS, new AccessControlList(text));
        } else if ("aclAdministerApps".equals(field.getTagName())) {
            String text = ((Text) field.getFirstChild()).getData();
            acls.put(QueueACL.ADMINISTER_QUEUE, new AccessControlList(text));
        } else if ("allowPreemptionFrom".equals(field.getTagName())) {
            String text = ((Text) field.getFirstChild()).getData().trim();
            if (!Boolean.parseBoolean(text)) {
                nonPreemptableQueues.add(queueName);
            }
        } else if ("queue".endsWith(field.getTagName()) || "pool".equals(field.getTagName())) {
            loadQueue(queueName, field, minQueueResources, maxQueueResources, maxChildQueueResources,
                    queueMaxApps, userMaxApps, queueMaxAMShares, queueWeights, queuePolicies,
                    minSharePreemptionTimeouts, fairSharePreemptionTimeouts, fairSharePreemptionThresholds,
                    queueAcls, configuredQueues, nonPreemptableQueues);
            configuredQueues.get(FSQueueType.PARENT).add(queueName);
            isLeaf = false;
        }
    }
    if (isLeaf) {
        // if a leaf in the alloc file is marked as type='parent'
        // then store it under 'parent'
        if ("parent".equals(element.getAttribute("type"))) {
            configuredQueues.get(FSQueueType.PARENT).add(queueName);
        } else {
            configuredQueues.get(FSQueueType.LEAF).add(queueName);
        }
    }
    queueAcls.put(queueName, acls);
    if (maxQueueResources.containsKey(queueName) && minQueueResources.containsKey(queueName)
            && !Resources.fitsIn(minQueueResources.get(queueName), maxQueueResources.get(queueName))) {
        LOG.warn(String.format("Queue %s has max resources %s less than " + "min resources %s", queueName,
                maxQueueResources.get(queueName), minQueueResources.get(queueName)));
    }
}

From source file:org.apache.jxtadoop.conf.Configuration.java

private void loadResource(Properties properties, Object name, boolean quiet) {
    try {// w ww  .  ja  v a2 s. c  o m
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        //ignore all comments inside the xml file
        docBuilderFactory.setIgnoringComments(true);

        //allow includes in the xml file
        docBuilderFactory.setNamespaceAware(true);
        try {
            docBuilderFactory.setXIncludeAware(true);
        } catch (UnsupportedOperationException e) {
            LOG.error("Failed to set setXIncludeAware(true) for parser " + docBuilderFactory + ":" + e, e);
        }
        DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
        Document doc = null;
        Element root = null;

        if (name instanceof URL) { // an URL resource
            URL url = (URL) name;
            if (url != null) {
                if (!quiet) {
                    LOG.info("parsing " + url);
                }
                doc = builder.parse(url.toString());
            }
        } else if (name instanceof String) { // a CLASSPATH resource
            URL url = getResource((String) name);
            if (url != null) {
                if (!quiet) {
                    LOG.info("parsing " + url);
                }
                doc = builder.parse(url.toString());
            }
        } else if (name instanceof Path) { // a file resource
            // Can't use FileSystem API or we get an infinite loop
            // since FileSystem uses Configuration API.  Use java.io.File instead.
            File file = new File(((Path) name).toUri().getPath()).getAbsoluteFile();
            if (file.exists()) {
                if (!quiet) {
                    LOG.info("parsing " + file);
                }
                InputStream in = new BufferedInputStream(new FileInputStream(file));
                try {
                    doc = builder.parse(in);
                } finally {
                    in.close();
                }
            }
        } else if (name instanceof InputStream) {
            try {
                doc = builder.parse((InputStream) name);
            } finally {
                ((InputStream) name).close();
            }
        } else if (name instanceof Element) {
            root = (Element) name;
        }

        if (doc == null && root == null) {
            if (quiet)
                return;
            throw new RuntimeException(name + " not found");
        }

        if (root == null) {
            root = doc.getDocumentElement();
        }
        if (!"configuration".equals(root.getTagName()))
            LOG.fatal("bad conf file: top-level element not <configuration>");
        NodeList props = root.getChildNodes();
        for (int i = 0; i < props.getLength(); i++) {
            Node propNode = props.item(i);
            if (!(propNode instanceof Element))
                continue;
            Element prop = (Element) propNode;
            if ("configuration".equals(prop.getTagName())) {
                loadResource(properties, prop, quiet);
                continue;
            }
            if (!"property".equals(prop.getTagName()))
                LOG.warn("bad conf file: element not <property>");
            NodeList fields = prop.getChildNodes();
            String attr = null;
            String value = null;
            boolean finalParameter = false;
            for (int j = 0; j < fields.getLength(); j++) {
                Node fieldNode = fields.item(j);
                if (!(fieldNode instanceof Element))
                    continue;
                Element field = (Element) fieldNode;
                if ("name".equals(field.getTagName()) && field.hasChildNodes())
                    attr = ((Text) field.getFirstChild()).getData().trim();
                if ("value".equals(field.getTagName()) && field.hasChildNodes())
                    value = ((Text) field.getFirstChild()).getData();
                if ("final".equals(field.getTagName()) && field.hasChildNodes())
                    finalParameter = "true".equals(((Text) field.getFirstChild()).getData());
            }

            // Ignore this parameter if it has already been marked as 'final'
            if (attr != null && value != null) {
                if (!finalParameters.contains(attr)) {
                    properties.setProperty(attr, value);
                    if (finalParameter)
                        finalParameters.add(attr);
                } else {
                    LOG.warn(name + ":a attempt to override final parameter: " + attr + ";  Ignoring.");
                }
            }
        }

    } catch (IOException e) {
        LOG.fatal("error parsing conf file: " + e);
        throw new RuntimeException(e);
    } catch (DOMException e) {
        LOG.fatal("error parsing conf file: " + e);
        throw new RuntimeException(e);
    } catch (SAXException e) {
        LOG.fatal("error parsing conf file: " + e);
        throw new RuntimeException(e);
    } catch (ParserConfigurationException e) {
        LOG.fatal("error parsing conf file: " + e);
        throw new RuntimeException(e);
    }
}

From source file:org.apache.nutch.net.urlnormalizer.regex.RegexURLNormalizer.java

private List readConfiguration(InputStream is) {
    Perl5Compiler compiler = new Perl5Compiler();
    List rules = new ArrayList();
    try {//w ww .j ava2  s  . c  o  m

        // borrowed heavily from code in Configuration.java
        Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);
        Element root = doc.getDocumentElement();
        if ((!"regex-normalize".equals(root.getTagName())) && (LOG.isFatalEnabled())) {
            LOG.fatal("bad conf file: top-level element not <regex-normalize>");
        }
        NodeList regexes = root.getChildNodes();
        for (int i = 0; i < regexes.getLength(); i++) {
            Node regexNode = regexes.item(i);
            if (!(regexNode instanceof Element))
                continue;
            Element regex = (Element) regexNode;
            if ((!"regex".equals(regex.getTagName())) && (LOG.isWarnEnabled())) {
                LOG.warn("bad conf file: element not <regex>");
            }
            NodeList fields = regex.getChildNodes();
            String patternValue = null;
            String subValue = null;
            for (int j = 0; j < fields.getLength(); j++) {
                Node fieldNode = fields.item(j);
                if (!(fieldNode instanceof Element))
                    continue;
                Element field = (Element) fieldNode;
                if ("pattern".equals(field.getTagName()) && field.hasChildNodes())
                    patternValue = ((Text) field.getFirstChild()).getData();
                if ("substitution".equals(field.getTagName()) && field.hasChildNodes())
                    subValue = ((Text) field.getFirstChild()).getData();
                if (!field.hasChildNodes())
                    subValue = "";
            }
            if (patternValue != null && subValue != null) {
                Rule rule = new Rule();
                rule.pattern = (Perl5Pattern) compiler.compile(patternValue);
                rule.substitution = subValue;
                rules.add(rule);
            }
        }
    } catch (Exception e) {
        if (LOG.isFatalEnabled()) {
            LOG.fatal("error parsing conf file: " + e);
        }
        return EMPTY_RULES;
    }
    if (rules.size() == 0)
        return EMPTY_RULES;
    return rules;
}

From source file:org.apache.nutch.protocol.httpclient.Http.java

/**
 * Reads authentication configuration file (defined as 'http.auth.file' in
 * Nutch configuration file) and sets the credentials for the configured
 * authentication scopes in the HTTP client object.
 * /* w  w  w.j av a  2  s .c  o m*/
 * @throws ParserConfigurationException
 *           If a document builder can not be created.
 * @throws SAXException
 *           If any parsing error occurs.
 * @throws IOException
 *           If any I/O error occurs.
 */
private static synchronized void setCredentials()
        throws ParserConfigurationException, SAXException, IOException {
    if (authRulesRead)
        return;

    authRulesRead = true; // Avoid re-attempting to read

    InputStream is = conf.getConfResourceAsInputStream(authFile);
    if (is != null) {
        Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);

        Element rootElement = doc.getDocumentElement();
        if (!"auth-configuration".equals(rootElement.getTagName())) {
            if (LOG.isWarnEnabled())
                LOG.warn("Bad auth conf file: root element <" + rootElement.getTagName() + "> found in "
                        + authFile + " - must be <auth-configuration>");
        }

        // For each set of credentials
        NodeList credList = rootElement.getChildNodes();
        for (int i = 0; i < credList.getLength(); i++) {
            Node credNode = credList.item(i);
            if (!(credNode instanceof Element))
                continue;

            Element credElement = (Element) credNode;
            if (!"credentials".equals(credElement.getTagName())) {
                if (LOG.isWarnEnabled())
                    LOG.warn("Bad auth conf file: Element <" + credElement.getTagName() + "> not recognized in "
                            + authFile + " - expected <credentials>");
                continue;
            }

            String authMethod = credElement.getAttribute("authMethod");
            // read http form post auth info
            if (StringUtils.isNotBlank(authMethod)) {
                formConfigurer = readFormAuthConfigurer(credElement, authMethod);
                continue;
            }

            String username = credElement.getAttribute("username");
            String password = credElement.getAttribute("password");

            // For each authentication scope
            NodeList scopeList = credElement.getChildNodes();
            for (int j = 0; j < scopeList.getLength(); j++) {
                Node scopeNode = scopeList.item(j);
                if (!(scopeNode instanceof Element))
                    continue;

                Element scopeElement = (Element) scopeNode;

                if ("default".equals(scopeElement.getTagName())) {

                    // Determine realm and scheme, if any
                    String realm = scopeElement.getAttribute("realm");
                    String scheme = scopeElement.getAttribute("scheme");

                    // Set default credentials
                    defaultUsername = username;
                    defaultPassword = password;
                    defaultRealm = realm;
                    defaultScheme = scheme;

                    if (LOG.isTraceEnabled()) {
                        LOG.trace("Credentials - username: " + username + "; set as default" + " for realm: "
                                + realm + "; scheme: " + scheme);
                    }

                } else if ("authscope".equals(scopeElement.getTagName())) {

                    // Determine authentication scope details
                    String host = scopeElement.getAttribute("host");
                    int port = -1; // For setting port to AuthScope.ANY_PORT
                    try {
                        port = Integer.parseInt(scopeElement.getAttribute("port"));
                    } catch (Exception ex) {
                        // do nothing, port is already set to any port
                    }
                    String realm = scopeElement.getAttribute("realm");
                    String scheme = scopeElement.getAttribute("scheme");

                    // Set credentials for the determined scope
                    AuthScope authScope = getAuthScope(host, port, realm, scheme);
                    NTCredentials credentials = new NTCredentials(username, password, agentHost, realm);

                    client.getState().setCredentials(authScope, credentials);

                    if (LOG.isTraceEnabled()) {
                        LOG.trace("Credentials - username: " + username + "; set for AuthScope - " + "host: "
                                + host + "; port: " + port + "; realm: " + realm + "; scheme: " + scheme);
                    }

                } else {
                    if (LOG.isWarnEnabled())
                        LOG.warn("Bad auth conf file: Element <" + scopeElement.getTagName()
                                + "> not recognized in " + authFile + " - expected <authscope>");
                }
            }
            is.close();
        }
    }
}

From source file:org.apache.nutch.protocol.httpclient.Http.java

/**
 * <auth-configuration> <credentials authMethod="formAuth" loginUrl="loginUrl"
 * loginFormId="loginFormId" loginRedirect="true"> <loginPostData> <field name
 * ="username" value="user1"/> </loginPostData>
 * <additionalPostHeaders> <field name="header1" value="vaule1"/>
 * </additionalPostHeaders>/*from  w  w  w . j  a v a2 s. co  m*/
 * <removedFormFields> <field name="header1"/> </removedFormFields> <!--
 * NUTCH-2280: Add <loginCookie> and it sub-node <policy> nodes into the
 * <credentials> node. The <policy> will mark the POST login form cookie
 * policy. The value could be CookiePolicy.<ConstantValues>.
 * --> </credentials> </auth-configuration>
 */
private static HttpFormAuthConfigurer readFormAuthConfigurer(Element credElement, String authMethod) {
    if ("formAuth".equals(authMethod)) {
        HttpFormAuthConfigurer formConfigurer = new HttpFormAuthConfigurer();

        String str = credElement.getAttribute("loginUrl");
        if (StringUtils.isNotBlank(str)) {
            formConfigurer.setLoginUrl(str.trim());
        } else {
            throw new IllegalArgumentException("Must set loginUrl.");
        }
        str = credElement.getAttribute("loginFormId");
        if (StringUtils.isNotBlank(str)) {
            formConfigurer.setLoginFormId(str.trim());
        } else {
            throw new IllegalArgumentException("Must set loginFormId.");
        }
        str = credElement.getAttribute("loginRedirect");
        if (StringUtils.isNotBlank(str)) {
            formConfigurer.setLoginRedirect(Boolean.parseBoolean(str));
        }

        NodeList nodeList = credElement.getChildNodes();

        for (int j = 0; j < nodeList.getLength(); j++) {
            Node node = nodeList.item(j);
            if (!(node instanceof Element))
                continue;

            Element element = (Element) node;
            if ("loginPostData".equals(element.getTagName())) {
                Map<String, String> loginPostData = new HashMap<String, String>();
                NodeList childNodes = element.getChildNodes();
                for (int k = 0; k < childNodes.getLength(); k++) {
                    Node fieldNode = childNodes.item(k);
                    if (!(fieldNode instanceof Element))
                        continue;

                    Element fieldElement = (Element) fieldNode;
                    String name = fieldElement.getAttribute("name");
                    String value = fieldElement.getAttribute("value");
                    loginPostData.put(name, value);
                }
                formConfigurer.setLoginPostData(loginPostData);
            } else if ("additionalPostHeaders".equals(element.getTagName())) {
                Map<String, String> additionalPostHeaders = new HashMap<String, String>();
                NodeList childNodes = element.getChildNodes();
                for (int k = 0; k < childNodes.getLength(); k++) {
                    Node fieldNode = childNodes.item(k);
                    if (!(fieldNode instanceof Element))
                        continue;

                    Element fieldElement = (Element) fieldNode;
                    String name = fieldElement.getAttribute("name");
                    String value = fieldElement.getAttribute("value");
                    additionalPostHeaders.put(name, value);
                }
                formConfigurer.setAdditionalPostHeaders(additionalPostHeaders);
            } else if ("removedFormFields".equals(element.getTagName())) {
                Set<String> removedFormFields = new HashSet<String>();
                NodeList childNodes = element.getChildNodes();
                for (int k = 0; k < childNodes.getLength(); k++) {
                    Node fieldNode = childNodes.item(k);
                    if (!(fieldNode instanceof Element))
                        continue;

                    Element fieldElement = (Element) fieldNode;
                    String name = fieldElement.getAttribute("name");
                    removedFormFields.add(name);
                }
                formConfigurer.setRemovedFormFields(removedFormFields);
            } else if ("loginCookie".equals(element.getTagName())) {
                // NUTCH-2280
                LOG.debug("start loginCookie");
                NodeList childNodes = element.getChildNodes();
                for (int k = 0; k < childNodes.getLength(); k++) {
                    Node fieldNode = childNodes.item(k);
                    if (!(fieldNode instanceof Element))
                        continue;
                    Element fieldElement = (Element) fieldNode;
                    if ("policy".equals(fieldElement.getTagName())) {
                        String policy = fieldElement.getTextContent();
                        formConfigurer.setCookiePolicy(policy);
                        LOG.debug("cookie policy is " + policy);
                    }
                }
            }
        }

        return formConfigurer;
    } else {
        throw new IllegalArgumentException("Unsupported authMethod: " + authMethod);
    }
}

From source file:org.apache.nutch.protocol.httpclient.HttpBak.java

/**
 * Reads authentication configuration file (defined as
 * 'http.auth.file' in Nutch configuration file) and sets the
 * credentials for the configured authentication scopes in the HTTP
 * client object.//  ww w  . ja  v a  2s  . c o m
 *
 * @throws ParserConfigurationException  If a document builder can not
 *                                       be created.
 * @throws SAXException                  If any parsing error occurs.
 * @throws IOException                   If any I/O error occurs.
 */
private static synchronized void setCredentials()
        throws ParserConfigurationException, SAXException, IOException {

    if (authRulesRead)
        return;

    authRulesRead = true; // Avoid re-attempting to read

    InputStream is = conf.getConfResourceAsInputStream(authFile);
    if (is != null) {
        Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);

        Element rootElement = doc.getDocumentElement();
        if (!"auth-configuration".equals(rootElement.getTagName())) {
            if (LOG.isWarnEnabled())
                LOG.warn("Bad auth conf file: root element <" + rootElement.getTagName() + "> found in "
                        + authFile + " - must be <auth-configuration>");
        }

        // For each set of credentials
        NodeList credList = rootElement.getChildNodes();
        for (int i = 0; i < credList.getLength(); i++) {
            Node credNode = credList.item(i);
            if (!(credNode instanceof Element))
                continue;

            Element credElement = (Element) credNode;
            if (!"credentials".equals(credElement.getTagName())) {
                if (LOG.isWarnEnabled())
                    LOG.warn("Bad auth conf file: Element <" + credElement.getTagName() + "> not recognized in "
                            + authFile + " - expected <credentials>");
                continue;
            }

            String username = credElement.getAttribute("username");
            String password = credElement.getAttribute("password");

            // For each authentication scope
            NodeList scopeList = credElement.getChildNodes();
            for (int j = 0; j < scopeList.getLength(); j++) {
                Node scopeNode = scopeList.item(j);
                if (!(scopeNode instanceof Element))
                    continue;

                Element scopeElement = (Element) scopeNode;

                if ("default".equals(scopeElement.getTagName())) {

                    // Determine realm and scheme, if any
                    String realm = scopeElement.getAttribute("realm");
                    String scheme = scopeElement.getAttribute("scheme");

                    // Set default credentials
                    defaultUsername = username;
                    defaultPassword = password;
                    defaultRealm = realm;
                    defaultScheme = scheme;

                    if (LOG.isTraceEnabled()) {
                        LOG.trace("Credentials - username: " + username + "; set as default" + " for realm: "
                                + realm + "; scheme: " + scheme);
                    }

                } else if ("authscope".equals(scopeElement.getTagName())) {

                    // Determine authentication scope details
                    String host = scopeElement.getAttribute("host");
                    int port = -1; // For setting port to AuthScope.ANY_PORT
                    try {
                        port = Integer.parseInt(scopeElement.getAttribute("port"));
                    } catch (Exception ex) {
                        // do nothing, port is already set to any port
                    }
                    String realm = scopeElement.getAttribute("realm");
                    String scheme = scopeElement.getAttribute("scheme");

                    // Set credentials for the determined scope
                    AuthScope authScope = getAuthScope(host, port, realm, scheme);
                    NTCredentials credentials = new NTCredentials(username, password, agentHost, realm);

                    client.getState().setCredentials(authScope, credentials);

                    if (LOG.isTraceEnabled()) {
                        LOG.trace("Credentials - username: " + username + "; set for AuthScope - " + "host: "
                                + host + "; port: " + port + "; realm: " + realm + "; scheme: " + scheme);
                    }

                } else {
                    if (LOG.isWarnEnabled())
                        LOG.warn("Bad auth conf file: Element <" + scopeElement.getTagName()
                                + "> not recognized in " + authFile + " - expected <authscope>");
                }
            }
            is.close();
        }
    }
}

From source file:org.apache.nutch.protocol.httpclient.proxy.Http.java

/**
 * Reads authentication configuration file (defined as 'http.auth.file' in
 * Nutch configuration file) and sets the credentials for the configured
 * authentication scopes in the HTTP client object.
 * //ww w  .jav  a  2s. co  m
 * @throws ParserConfigurationException
 *           If a document builder can not be created.
 * @throws SAXException
 *           If any parsing error occurs.
 * @throws IOException
 *           If any I/O error occurs.
 */
private static synchronized void setCredentials()
        throws ParserConfigurationException, SAXException, IOException {

    if (authRulesRead)
        return;

    authRulesRead = true; // Avoid re-attempting to read

    InputStream is = conf.getConfResourceAsInputStream(authFile);
    if (is != null) {
        Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);

        Element rootElement = doc.getDocumentElement();
        if (!"auth-configuration".equals(rootElement.getTagName())) {
            if (LOG.isWarnEnabled())
                LOG.warn("Bad auth conf file: root element <" + rootElement.getTagName() + "> found in "
                        + authFile + " - must be <auth-configuration>");
        }

        // For each set of credentials
        NodeList credList = rootElement.getChildNodes();
        for (int i = 0; i < credList.getLength(); i++) {
            Node credNode = credList.item(i);
            if (!(credNode instanceof Element))
                continue;

            Element credElement = (Element) credNode;
            if (!"credentials".equals(credElement.getTagName())) {
                if (LOG.isWarnEnabled())
                    LOG.warn("Bad auth conf file: Element <" + credElement.getTagName() + "> not recognized in "
                            + authFile + " - expected <credentials>");
                continue;
            }

            String authMethod = credElement.getAttribute("authMethod");
            // read http form post auth info
            if (StringUtils.isNotBlank(authMethod)) {
                formConfigurer = readFormAuthConfigurer(credElement, authMethod);
                continue;
            }

            String username = credElement.getAttribute("username");
            String password = credElement.getAttribute("password");

            // For each authentication scope
            NodeList scopeList = credElement.getChildNodes();
            for (int j = 0; j < scopeList.getLength(); j++) {
                Node scopeNode = scopeList.item(j);
                if (!(scopeNode instanceof Element))
                    continue;

                Element scopeElement = (Element) scopeNode;

                if ("default".equals(scopeElement.getTagName())) {

                    // Determine realm and scheme, if any
                    String realm = scopeElement.getAttribute("realm");
                    String scheme = scopeElement.getAttribute("scheme");

                    // Set default credentials
                    defaultUsername = username;
                    defaultPassword = password;
                    defaultRealm = realm;
                    defaultScheme = scheme;

                    if (LOG.isTraceEnabled()) {
                        LOG.trace("Credentials - username: " + username + "; set as default" + " for realm: "
                                + realm + "; scheme: " + scheme);
                    }

                } else if ("authscope".equals(scopeElement.getTagName())) {

                    // Determine authentication scope details
                    String host = scopeElement.getAttribute("host");
                    int port = -1; // For setting port to AuthScope.ANY_PORT
                    try {
                        port = Integer.parseInt(scopeElement.getAttribute("port"));
                    } catch (Exception ex) {
                        // do nothing, port is already set to any port
                    }
                    String realm = scopeElement.getAttribute("realm");
                    String scheme = scopeElement.getAttribute("scheme");

                    // Set credentials for the determined scope
                    AuthScope authScope = getAuthScope(host, port, realm, scheme);
                    NTCredentials credentials = new NTCredentials(username, password, agentHost, realm);

                    client.getState().setCredentials(authScope, credentials);

                    if (LOG.isTraceEnabled()) {
                        LOG.trace("Credentials - username: " + username + "; set for AuthScope - " + "host: "
                                + host + "; port: " + port + "; realm: " + realm + "; scheme: " + scheme);
                    }

                } else {
                    if (LOG.isWarnEnabled())
                        LOG.warn("Bad auth conf file: Element <" + scopeElement.getTagName()
                                + "> not recognized in " + authFile + " - expected <authscope>");
                }
            }
            is.close();
        }
    }
}

From source file:org.apache.nutch.protocol.httpclient.proxy.Http.java

/**
 * <auth-configuration> <credentials authMethod="formAuth"
 * loginUrl="loginUrl" loginFormId="loginFormId" loginRedirect="true">
 * <loginPostData> <field name="username" value="user1"/> </loginPostData>
 * <additionalPostHeaders> <field name="header1" value="vaule1"/>
 * </additionalPostHeaders> <removedFormFields> <field name="header1"/>
 * </removedFormFields> </credentials> </auth-configuration>
 *//*from   w  w w.  j  a  v  a2s . co m*/
private static HttpFormAuthConfigurer readFormAuthConfigurer(Element credElement, String authMethod) {
    if ("formAuth".equals(authMethod)) {
        HttpFormAuthConfigurer formConfigurer = new HttpFormAuthConfigurer();

        String str = credElement.getAttribute("loginUrl");
        if (StringUtils.isNotBlank(str)) {
            formConfigurer.setLoginUrl(str.trim());
        } else {
            throw new IllegalArgumentException("Must set loginUrl.");
        }
        str = credElement.getAttribute("loginFormId");
        if (StringUtils.isNotBlank(str)) {
            formConfigurer.setLoginFormId(str.trim());
        } else {
            throw new IllegalArgumentException("Must set loginFormId.");
        }
        str = credElement.getAttribute("loginRedirect");
        if (StringUtils.isNotBlank(str)) {
            formConfigurer.setLoginRedirect(Boolean.parseBoolean(str));
        }

        NodeList nodeList = credElement.getChildNodes();
        for (int j = 0; j < nodeList.getLength(); j++) {
            Node node = nodeList.item(j);
            if (!(node instanceof Element))
                continue;

            Element element = (Element) node;
            if ("loginPostData".equals(element.getTagName())) {
                Map<String, String> loginPostData = new HashMap<String, String>();
                NodeList childNodes = element.getChildNodes();
                for (int k = 0; k < childNodes.getLength(); k++) {
                    Node fieldNode = childNodes.item(k);
                    if (!(fieldNode instanceof Element))
                        continue;

                    Element fieldElement = (Element) fieldNode;
                    String name = fieldElement.getAttribute("name");
                    String value = fieldElement.getAttribute("value");
                    loginPostData.put(name, value);
                }
                formConfigurer.setLoginPostData(loginPostData);
            } else if ("additionalPostHeaders".equals(element.getTagName())) {
                Map<String, String> additionalPostHeaders = new HashMap<String, String>();
                NodeList childNodes = element.getChildNodes();
                for (int k = 0; k < childNodes.getLength(); k++) {
                    Node fieldNode = childNodes.item(k);
                    if (!(fieldNode instanceof Element))
                        continue;

                    Element fieldElement = (Element) fieldNode;
                    String name = fieldElement.getAttribute("name");
                    String value = fieldElement.getAttribute("value");
                    additionalPostHeaders.put(name, value);
                }
                formConfigurer.setAdditionalPostHeaders(additionalPostHeaders);
            } else if ("removedFormFields".equals(element.getTagName())) {
                Set<String> removedFormFields = new HashSet<String>();
                NodeList childNodes = element.getChildNodes();
                for (int k = 0; k < childNodes.getLength(); k++) {
                    Node fieldNode = childNodes.item(k);
                    if (!(fieldNode instanceof Element))
                        continue;

                    Element fieldElement = (Element) fieldNode;
                    String name = fieldElement.getAttribute("name");
                    removedFormFields.add(name);
                }
                formConfigurer.setRemovedFormFields(removedFormFields);
            }
        }

        return formConfigurer;
    } else {
        throw new IllegalArgumentException("Unsupported authMethod: " + authMethod);
    }
}

From source file:org.apache.ode.bpel.runtime.ASSIGN.java

private Element replaceElement(Element lval, Element ptr, Element src, boolean keepSrcElement) {
    Document doc = ptr.getOwnerDocument();
    Node parent = ptr.getParentNode();
    if (keepSrcElement) {
        Element replacement = (Element) doc.importNode(src, true);
        parent.replaceChild(replacement, ptr);
        return (lval == ptr) ? replacement : lval;
    }/*from   w w w .  j av  a2s . c om*/

    Element replacement = doc.createElementNS(ptr.getNamespaceURI(), ptr.getTagName());
    NodeList nl = src.getChildNodes();
    for (int i = 0; i < nl.getLength(); ++i)
        replacement.appendChild(doc.importNode(nl.item(i), true));
    copyAttributes(doc, ptr, replacement);
    copyAttributes(doc, src, replacement);
    parent.replaceChild(replacement, ptr);
    DOMUtils.copyNSContext(ptr, replacement);

    return (lval == ptr) ? replacement : lval;
}