Search in sources :

Example 1 with SizeLimitExceededException

use of javax.naming.SizeLimitExceededException in project zeppelin by apache.

the class LdapRealm method rolesFor.

private Set<String> rolesFor(PrincipalCollection principals, String userNameIn, final LdapContext ldapCtx, final LdapContextFactory ldapContextFactory) throws NamingException {
    final Set<String> roleNames = new HashSet<>();
    final Set<String> groupNames = new HashSet<>();
    final String userName;
    if (getUserLowerCase()) {
        log.debug("userLowerCase true");
        userName = userNameIn.toLowerCase();
    } else {
        userName = userNameIn;
    }
    String userDn;
    if (userSearchAttributeName == null || userSearchAttributeName.isEmpty()) {
        // memberAttributeValuePrefix and memberAttributeValueSuffix 
        // were computed from memberAttributeValueTemplate
        userDn = memberAttributeValuePrefix + userName + memberAttributeValueSuffix;
    } else {
        userDn = getUserDn(userName);
    }
    // Activate paged results
    int pageSize = getPagingSize();
    if (log.isDebugEnabled()) {
        log.debug("Ldap PagingSize: " + pageSize);
    }
    int numResults = 0;
    byte[] cookie = null;
    try {
        ldapCtx.addToEnvironment(Context.REFERRAL, "ignore");
        ldapCtx.setRequestControls(new Control[] { new PagedResultsControl(pageSize, Control.NONCRITICAL) });
        do {
            // ldapsearch -h localhost -p 33389 -D
            // uid=guest,ou=people,dc=hadoop,dc=apache,dc=org -w guest-password
            // -b dc=hadoop,dc=apache,dc=org -s sub '(objectclass=*)'
            NamingEnumeration<SearchResult> searchResultEnum = null;
            SearchControls searchControls = getGroupSearchControls();
            try {
                if (groupSearchEnableMatchingRuleInChain) {
                    searchResultEnum = ldapCtx.search(getGroupSearchBase(), String.format(MATCHING_RULE_IN_CHAIN_FORMAT, groupObjectClass, memberAttribute, userDn), searchControls);
                    while (searchResultEnum != null && searchResultEnum.hasMore()) {
                        // searchResults contains all the groups in search scope
                        numResults++;
                        final SearchResult group = searchResultEnum.next();
                        Attribute attribute = group.getAttributes().get(getGroupIdAttribute());
                        String groupName = attribute.get().toString();
                        String roleName = roleNameFor(groupName);
                        if (roleName != null) {
                            roleNames.add(roleName);
                        } else {
                            roleNames.add(groupName);
                        }
                    }
                } else {
                    searchResultEnum = ldapCtx.search(getGroupSearchBase(), "objectClass=" + groupObjectClass, searchControls);
                    while (searchResultEnum != null && searchResultEnum.hasMore()) {
                        // searchResults contains all the groups in search scope
                        numResults++;
                        final SearchResult group = searchResultEnum.next();
                        addRoleIfMember(userDn, group, roleNames, groupNames, ldapContextFactory);
                    }
                }
            } catch (PartialResultException e) {
                log.debug("Ignoring PartitalResultException");
            } finally {
                if (searchResultEnum != null) {
                    searchResultEnum.close();
                }
            }
            // Re-activate paged results
            ldapCtx.setRequestControls(new Control[] { new PagedResultsControl(pageSize, cookie, Control.CRITICAL) });
        } while (cookie != null);
    } catch (SizeLimitExceededException e) {
        log.info("Only retrieved first " + numResults + " groups due to SizeLimitExceededException.");
    } catch (IOException e) {
        log.error("Unabled to setup paged results");
    }
    // save role names and group names in session so that they can be
    // easily looked up outside of this object
    SecurityUtils.getSubject().getSession().setAttribute(SUBJECT_USER_ROLES, roleNames);
    SecurityUtils.getSubject().getSession().setAttribute(SUBJECT_USER_GROUPS, groupNames);
    if (!groupNames.isEmpty() && (principals instanceof MutablePrincipalCollection)) {
        ((MutablePrincipalCollection) principals).addAll(groupNames, getName());
    }
    if (log.isDebugEnabled()) {
        log.debug("User RoleNames: " + userName + "::" + roleNames);
    }
    return roleNames;
}
Also used : Attribute(javax.naming.directory.Attribute) SearchResult(javax.naming.directory.SearchResult) PartialResultException(javax.naming.PartialResultException) IOException(java.io.IOException) MutablePrincipalCollection(org.apache.shiro.subject.MutablePrincipalCollection) SizeLimitExceededException(javax.naming.SizeLimitExceededException) SearchControls(javax.naming.directory.SearchControls) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) PagedResultsControl(javax.naming.ldap.PagedResultsControl)

Example 2 with SizeLimitExceededException

use of javax.naming.SizeLimitExceededException in project azure-iot-sdk-java by Azure.

the class HttpsBatchMessageTest method testAddMessage.

// Tests_SRS_HTTPSBATCHMESSAGE_11_008: [If adding the message causes the batched message to exceed 256 kb in size, the function shall throw a SizeLimitExceededException.]
// Tests_SRS_HTTPSBATCHMESSAGE_11_009: [If the function throws a SizeLimitExceedException, the batched message shall remain as if the message was never added.]
@Test
public void testAddMessage(@Mocked final HttpsSingleMessage mockMsg) throws SizeLimitExceededException {
    // Note: this will currently result on a message size of 261154 bytes, considering the extra attributes contained on the json-serialized message.
    // Note: so the current body size limit alone actually is (255 * 1024  - 36) bytes.
    final byte[] validSizeBody = new byte[255 * 1024 - 1];
    new NonStrictExpectations() {

        {
            mockMsg.getBodyAsString();
            result = new String(validSizeBody, Message.DEFAULT_IOTHUB_MESSAGE_CHARSET);
        }
    };
    boolean httpsBatchMessageSizeLimitVerified = false;
    try {
        HttpsBatchMessage batchMsg = new HttpsBatchMessage();
        batchMsg.addMessage(mockMsg);
    } catch (SizeLimitExceededException ex) {
        httpsBatchMessageSizeLimitVerified = true;
    }
    assertThat(httpsBatchMessageSizeLimitVerified, is(true));
}
Also used : SizeLimitExceededException(javax.naming.SizeLimitExceededException) HttpsBatchMessage(com.microsoft.azure.sdk.iot.device.transport.https.HttpsBatchMessage) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 3 with SizeLimitExceededException

use of javax.naming.SizeLimitExceededException in project azure-iot-sdk-java by Azure.

the class HttpsBatchMessageTest method addMessageRejectsOverflowingMessageAndPreservesOldBatchState.

// Tests_SRS_HTTPSBATCHMESSAGE_11_009: [If the function throws a SizeLimitExceededException, the batched message shall remain as if the message was never added.]
@Test
public void addMessageRejectsOverflowingMessageAndPreservesOldBatchState(@Mocked final HttpsSingleMessage mockMsg) throws SizeLimitExceededException {
    final int msgBodySize = SERVICEBOUND_MESSAGE_MAX_SIZE_BYTES / 2 + 1;
    final byte[] msgBodyBytes = new byte[msgBodySize];
    final String msgBody = new String(msgBodyBytes, UTF8);
    final boolean isBase64Encoded = false;
    new NonStrictExpectations() {

        {
            mockMsg.getBodyAsString();
            result = msgBody;
            mockMsg.isBase64Encoded();
            result = isBase64Encoded;
        }
    };
    HttpsBatchMessage batchMsg = new HttpsBatchMessage();
    batchMsg.addMessage(mockMsg);
    try {
        batchMsg.addMessage(mockMsg);
    } catch (SizeLimitExceededException e) {
        final int expectedTwoMsgBodySize = 2 * msgBodySize;
        assertThat(batchMsg.getBody().length, is(lessThan(expectedTwoMsgBodySize)));
    }
}
Also used : SizeLimitExceededException(javax.naming.SizeLimitExceededException) HttpsBatchMessage(com.microsoft.azure.sdk.iot.device.transport.https.HttpsBatchMessage) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 4 with SizeLimitExceededException

use of javax.naming.SizeLimitExceededException in project azure-iot-sdk-java by Azure.

the class HttpsTransportTest method sendMessagesSendsSingleMesssageIfBatchFormatExceedsMaxSize.

// Tests_SRS_HTTPSTRANSPORT_11_013: [If no messages fit using the batch format, the function shall send a single message without the batch format.]
@Test
public void sendMessagesSendsSingleMesssageIfBatchFormatExceedsMaxSize(@Mocked final Message mockMsg, @Mocked final HttpsSingleMessage mockHttpsMsg, @Mocked final IotHubEventCallback mockCallback, @Mocked final HttpsBatchMessage mockBatch, @Mocked final IotHubStatusCode mockStatus) throws URISyntaxException, IOException, SizeLimitExceededException {
    final Map<String, Object> context = new HashMap<>();
    new NonStrictExpectations() {

        {
            HttpsSingleMessage.parseHttpsMessage(mockMsg);
            result = mockHttpsMsg;
            new HttpsBatchMessage();
            result = mockBatch;
            mockBatch.addMessage(mockHttpsMsg);
            result = new SizeLimitExceededException();
        }
    };
    HttpsTransport transport = new HttpsTransport(mockConfig);
    transport.open();
    transport.addMessage(mockMsg, mockCallback, context);
    transport.addMessage(mockMsg, mockCallback, context);
    transport.addMessage(mockMsg, mockCallback, context);
    transport.sendMessages();
    final HttpsMessage expectedMsg = mockHttpsMsg;
    new Verifications() {

        {
            mockConn.sendEvent(expectedMsg);
        }
    };
}
Also used : SizeLimitExceededException(javax.naming.SizeLimitExceededException) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 5 with SizeLimitExceededException

use of javax.naming.SizeLimitExceededException in project azure-iot-sdk-java by Azure.

the class HttpsTransport method moveWaitingListToInProgressList.

/**
     * Moves as many messages as can be sent in one HTTPS request from the
     * waiting list to the in-progress list. If a single message is moved to the
     * in-progress list, this indicates that the message is to be sent in the
     * un-batched message format.
     */
private void moveWaitingListToInProgressList() {
    HttpsBatchMessage batch = new HttpsBatchMessage();
    while (!this.waitingList.isEmpty()) {
        IotHubOutboundPacket packet = this.waitingList.peek();
        try {
            HttpsSingleMessage httpsMsg = HttpsSingleMessage.parseHttpsMessage(packet.getMessage());
            batch.addMessage(httpsMsg);
        } catch (SizeLimitExceededException e) {
            break;
        }
        this.waitingList.remove();
        this.inProgressList.add(packet);
    }
    if (!this.waitingList.isEmpty() && batch.numMessages() <= 0) {
        IotHubOutboundPacket packet = this.waitingList.remove();
        this.inProgressList.add(packet);
    }
}
Also used : SizeLimitExceededException(javax.naming.SizeLimitExceededException) IotHubOutboundPacket(com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket)

Aggregations

SizeLimitExceededException (javax.naming.SizeLimitExceededException)5 Test (org.junit.Test)3 HttpsBatchMessage (com.microsoft.azure.sdk.iot.device.transport.https.HttpsBatchMessage)2 NonStrictExpectations (mockit.NonStrictExpectations)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 IotHubOutboundPacket (com.microsoft.azure.sdk.iot.device.transport.IotHubOutboundPacket)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 PartialResultException (javax.naming.PartialResultException)1 Attribute (javax.naming.directory.Attribute)1 SearchControls (javax.naming.directory.SearchControls)1 SearchResult (javax.naming.directory.SearchResult)1 PagedResultsControl (javax.naming.ldap.PagedResultsControl)1 MutablePrincipalCollection (org.apache.shiro.subject.MutablePrincipalCollection)1