Search in sources :

Example 46 with NamingEnumeration

use of javax.naming.NamingEnumeration in project jmeter by apache.

the class LDAPExtSampler method sample.

@Override
public SampleResult sample(Entry e) {
    XMLBuffer xmlBuffer = new XMLBuffer();
    // $NON-NLS-1$
    xmlBuffer.openTag("ldapanswer");
    SampleResult res = new SampleResult();
    res.setResponseData("successfull", null);
    // $NON-NLS-1$
    res.setResponseMessage("Success");
    // $NON-NLS-1$
    res.setResponseCode("0");
    // $NON-NLS-1$
    res.setContentType("text/xml");
    boolean isSuccessful = true;
    res.setSampleLabel(getName());
    DirContext dirContext = ldapContexts.get(getThreadName());
    try {
        // $NON-NLS-1$
        xmlBuffer.openTag("operation");
        final String testType = getTest();
        // $NON-NLS-1$
        xmlBuffer.tag("opertype", testType);
        log.debug("performing test: " + testType);
        if (testType.equals(UNBIND)) {
            res.setSamplerData("Unbind");
            // $NON-NLS-1$
            xmlBuffer.tag("baseobj", getRootdn());
            // $NON-NLS-1$
            xmlBuffer.tag("binddn", getUserDN());
            unbindOp(dirContext, res);
        } else if (testType.equals(BIND)) {
            res.setSamplerData("Bind as " + getUserDN());
            // $NON-NLS-1$
            xmlBuffer.tag("baseobj", getRootdn());
            // $NON-NLS-1$
            xmlBuffer.tag("binddn", getUserDN());
            // $NON-NLS-1$
            xmlBuffer.tag("connectionTO", getConnTimeOut());
            bindOp(res);
        } else if (testType.equals(SBIND)) {
            res.setSamplerData("SingleBind as " + getUserDN());
            // $NON-NLS-1$
            xmlBuffer.tag("baseobj", getRootdn());
            // $NON-NLS-1$
            xmlBuffer.tag("binddn", getUserDN());
            // $NON-NLS-1$
            xmlBuffer.tag("connectionTO", getConnTimeOut());
            singleBindOp(res);
        } else if (testType.equals(COMPARE)) {
            res.setSamplerData("Compare " + getPropertyAsString(COMPAREFILT) + " " + getPropertyAsString(COMPAREDN));
            // $NON-NLS-1$
            xmlBuffer.tag("comparedn", getPropertyAsString(COMPAREDN));
            // $NON-NLS-1$
            xmlBuffer.tag("comparefilter", getPropertyAsString(COMPAREFILT));
            NamingEnumeration<SearchResult> cmp = null;
            try {
                res.sampleStart();
                cmp = LdapExtClient.compare(dirContext, getPropertyAsString(COMPAREFILT), getPropertyAsString(COMPAREDN));
                if (!cmp.hasMore()) {
                    // $NON-NLS-1$
                    res.setResponseCode("5");
                    res.setResponseMessage("compareFalse");
                    isSuccessful = false;
                }
            } finally {
                res.sampleEnd();
                if (cmp != null) {
                    cmp.close();
                }
            }
        } else if (testType.equals(ADD)) {
            res.setSamplerData("Add object " + getBaseEntryDN());
            // $NON-NLS-1$
            xmlBuffer.tag("attributes", getArguments().toString());
            // $NON-NLS-1$
            xmlBuffer.tag("dn", getBaseEntryDN());
            addTest(dirContext, res);
        } else if (testType.equals(DELETE)) {
            res.setSamplerData("Delete object " + getBaseEntryDN());
            // $NON-NLS-1$
            xmlBuffer.tag("dn", getBaseEntryDN());
            deleteTest(dirContext, res);
        } else if (testType.equals(MODIFY)) {
            res.setSamplerData("Modify object " + getBaseEntryDN());
            // $NON-NLS-1$
            xmlBuffer.tag("dn", getBaseEntryDN());
            // $NON-NLS-1$
            xmlBuffer.tag("attributes", getLDAPArguments().toString());
            modifyTest(dirContext, res);
        } else if (testType.equals(RENAME)) {
            res.setSamplerData("ModDN object " + getPropertyAsString(MODDDN) + " to " + getPropertyAsString(NEWDN));
            // $NON-NLS-1$
            xmlBuffer.tag("dn", getPropertyAsString(MODDDN));
            // $NON-NLS-1$
            xmlBuffer.tag("newdn", getPropertyAsString(NEWDN));
            renameTest(dirContext, res);
        } else if (testType.equals(SEARCH)) {
            final String scopeStr = getScope();
            final int scope = getScopeAsInt();
            final String searchFilter = getPropertyAsString(SEARCHFILTER);
            final String searchBase = getPropertyAsString(SEARCHBASE);
            final String timeLimit = getTimelim();
            final String countLimit = getCountlim();
            res.setSamplerData("Search with filter " + searchFilter);
            // $NON-NLS-1$
            xmlBuffer.tag("searchfilter", StringEscapeUtils.escapeXml10(searchFilter));
            // $NON-NLS-1$
            xmlBuffer.tag("baseobj", getRootdn());
            // $NON-NLS-1$
            xmlBuffer.tag("searchbase", searchBase);
            // $NON-NLS-1$
            xmlBuffer.tag("scope", scopeStr);
            // $NON-NLS-1$
            xmlBuffer.tag("countlimit", countLimit);
            // $NON-NLS-1$
            xmlBuffer.tag("timelimit", timeLimit);
            NamingEnumeration<SearchResult> srch = null;
            try {
                res.sampleStart();
                srch = LdapExtClient.searchTest(dirContext, searchBase, searchFilter, scope, getCountlimAsLong(), getTimelimAsInt(), getRequestAttributes(getAttrs()), isRetobj(), isDeref());
                if (isParseFlag()) {
                    try {
                        // $NON-NLS-1$
                        xmlBuffer.openTag("searchresults");
                        writeSearchResults(xmlBuffer, srch);
                    } finally {
                        // $NON-NLS-1$
                        xmlBuffer.closeTag("searchresults");
                    }
                } else {
                    // $NON-NLS-1$
                    xmlBuffer.tag(// $NON-NLS-1$
                    "searchresults", // $NON-NLS-1$
                    "hasElements=" + srch.hasMoreElements());
                }
            } finally {
                if (srch != null) {
                    srch.close();
                }
                res.sampleEnd();
            }
        }
    } catch (NamingException ex) {
        // TODO: tidy this up
        String returnData = ex.toString();
        final int indexOfLDAPErrCode = returnData.indexOf("LDAP: error code");
        if (indexOfLDAPErrCode >= 0) {
            res.setResponseMessage(returnData.substring(indexOfLDAPErrCode + 21, returnData.indexOf(// $NON-NLS-1$
            ']')));
            res.setResponseCode(returnData.substring(indexOfLDAPErrCode + 17, indexOfLDAPErrCode + 19));
        } else {
            res.setResponseMessage(returnData);
            // $NON-NLS-1$
            res.setResponseCode("800");
        }
        isSuccessful = false;
    } finally {
        // $NON-NLS-1$
        xmlBuffer.closeTag("operation");
        // $NON-NLS-1$
        xmlBuffer.tag("responsecode", res.getResponseCode());
        // $NON-NLS-1$
        xmlBuffer.tag("responsemessage", res.getResponseMessage());
        res.setResponseData(xmlBuffer.toString(), null);
        res.setDataType(SampleResult.TEXT);
        res.setSuccessful(isSuccessful);
    }
    return res;
}
Also used : SampleResult(org.apache.jmeter.samplers.SampleResult) NamingEnumeration(javax.naming.NamingEnumeration) SearchResult(javax.naming.directory.SearchResult) NamingException(javax.naming.NamingException) DirContext(javax.naming.directory.DirContext) XMLBuffer(org.apache.jorphan.util.XMLBuffer)

Example 47 with NamingEnumeration

use of javax.naming.NamingEnumeration in project tomee by apache.

the class JNDIContext method list.

@SuppressWarnings("unchecked")
@Override
public NamingEnumeration<NameClassPair> list(String name) throws NamingException {
    if (name == null) {
        throw new InvalidNameException("The name cannot be null");
    } else if (name.startsWith("java:")) {
        name = name.replaceFirst("^java:", "");
    } else if (!name.startsWith("/")) {
        name = tail + name;
    }
    final JNDIRequest req = new JNDIRequest(RequestMethodCode.JNDI_LIST, name);
    req.setModuleId(moduleId);
    final JNDIResponse res;
    try {
        res = request(req);
    } catch (Exception e) {
        if (e instanceof RemoteException && e.getCause() instanceof ConnectException) {
            e = (Exception) e.getCause();
            throw (ServiceUnavailableException) new ServiceUnavailableException("Cannot list '" + name + "'.").initCause(e);
        }
        throw (NamingException) new NamingException("Cannot list '" + name + "'.").initCause(e);
    }
    switch(res.getResponseCode()) {
        case ResponseCodes.JNDI_OK:
            return null;
        case ResponseCodes.JNDI_ENUMERATION:
            return (NamingEnumeration) res.getResult();
        case ResponseCodes.JNDI_NOT_FOUND:
            throw new NameNotFoundException(name);
        case ResponseCodes.JNDI_NAMING_EXCEPTION:
            final Throwable throwable = ((ThrowableArtifact) res.getResult()).getThrowable();
            if (throwable instanceof NamingException) {
                throw (NamingException) throwable;
            }
            throw (NamingException) new NamingException().initCause(throwable);
        case ResponseCodes.JNDI_ERROR:
            throw (Error) res.getResult();
        default:
            throw new ClientRuntimeException("Invalid response from server :" + res.getResponseCode());
    }
}
Also used : NameNotFoundException(javax.naming.NameNotFoundException) NamingEnumeration(javax.naming.NamingEnumeration) ServiceUnavailableException(javax.naming.ServiceUnavailableException) URISyntaxException(java.net.URISyntaxException) ConfigurationException(javax.naming.ConfigurationException) NamingException(javax.naming.NamingException) AuthenticationException(javax.naming.AuthenticationException) ConnectException(java.net.ConnectException) NameNotFoundException(javax.naming.NameNotFoundException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RemoteException(java.rmi.RemoteException) OperationNotSupportedException(javax.naming.OperationNotSupportedException) ServiceUnavailableException(javax.naming.ServiceUnavailableException) InvalidNameException(javax.naming.InvalidNameException) InvalidNameException(javax.naming.InvalidNameException) NamingException(javax.naming.NamingException) RemoteException(java.rmi.RemoteException) ConnectException(java.net.ConnectException)

Example 48 with NamingEnumeration

use of javax.naming.NamingEnumeration in project simba-os by cegeka.

the class ActiveDirectoryLoginModuleTest method injection.

@Test
@SuppressWarnings("unchecked")
public void injection() throws Exception {
    when(configurationService.getValue(SimbaConfigurationParameter.ENABLE_AD_GROUPS)).thenReturn(Boolean.FALSE);
    Map<String, String> options = new HashMap<>();
    options.put("primaryServer", "localhost:389");
    options.put("baseDN", "'dc=rsvzinasti,dc=be'");
    options.put("filter", "(&amp;(objectClass=person)(sAMAccountName=%USERNAME%))");
    options.put("searchScope", "subtree");
    options.put("authDomain", "rsvzinasti.be");
    options.put("authAttr", "sAMAccountName");
    options.put("securityLevel", "simple");
    NamingEnumeration attrsNamingEnumeration = mock(NamingEnumeration.class);
    when(attrsNamingEnumeration.hasMore()).thenReturn(true);
    Attributes attrs = mock(Attributes.class);
    when(attrs.getAll()).thenReturn(attrsNamingEnumeration);
    SearchResult searchResult = mock(SearchResult.class);
    when(searchResult.getName()).thenReturn(null);
    when(searchResult.getAttributes()).thenReturn(attrs);
    NamingEnumeration<SearchResult> searchResultNamingEnumeration = mock(NamingEnumeration.class);
    when(searchResultNamingEnumeration.hasMoreElements()).thenReturn(true).thenReturn(false);
    when(searchResultNamingEnumeration.next()).thenReturn(searchResult);
    ArgumentCaptor<String> searchFilter = ArgumentCaptor.forClass(String.class);
    final LdapContext ldapContext = mock(LdapContext.class);
    when(ldapContext.search(eq("'dc=rsvzinasti,dc=be'"), searchFilter.capture(), any(SearchControls.class))).thenReturn(searchResultNamingEnumeration);
    ActiveDirectoryLoginModule loginModule = new ActiveDirectoryLoginModule() {

        @Override
        protected LdapContext tryPrimaryContext(Hashtable<String, String> env) {
            return ldapContext;
        }
    };
    loginModule.setUsername(" u\\*() ");
    loginModule.setPassword(" p\\*() ");
    loginModule.initialize(new Subject(), mock(CallbackHandler.class), Collections.emptyMap(), options);
    boolean result = loginModule.verifyLoginData();
    assertThat(result).isTrue();
    assertThat(searchFilter.getValue()).isEqualTo("(&amp;(objectClass=person)(sAMAccountName= u5c2a282900 ))");
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) HashMap(java.util.HashMap) Hashtable(java.util.Hashtable) Attributes(javax.naming.directory.Attributes) NamingEnumeration(javax.naming.NamingEnumeration) SearchResult(javax.naming.directory.SearchResult) Subject(javax.security.auth.Subject) SearchControls(javax.naming.directory.SearchControls) LdapContext(javax.naming.ldap.LdapContext) Test(org.junit.Test)

Aggregations

NamingEnumeration (javax.naming.NamingEnumeration)48 Attribute (javax.naming.directory.Attribute)22 SearchResult (javax.naming.directory.SearchResult)20 NamingException (javax.naming.NamingException)19 Attributes (javax.naming.directory.Attributes)19 SearchControls (javax.naming.directory.SearchControls)18 DirContext (javax.naming.directory.DirContext)14 ArrayList (java.util.ArrayList)11 LdapContext (javax.naming.ldap.LdapContext)10 Test (org.junit.Test)9 InitialContext (javax.naming.InitialContext)7 Hashtable (java.util.Hashtable)6 Context (javax.naming.Context)6 InitialDirContext (javax.naming.directory.InitialDirContext)6 HashMap (java.util.HashMap)5 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3 Binding (javax.naming.Binding)3 NameClassPair (javax.naming.NameClassPair)3 NameNotFoundException (javax.naming.NameNotFoundException)3