예제 #1
0
  /** {@inheritDoc} */
  @Override
  public void toLDAPAttributes(
      final SCIMObject scimObject,
      final Collection<Attribute> attributes,
      final LDAPRequestInterface ldapInterface,
      final LDAPSearchResolver searchResolver)
      throws SCIMException {
    final SCIMAttribute scimAttribute =
        scimObject.getAttribute(
            getAttributeDescriptor().getSchema(), getAttributeDescriptor().getName());
    if (scimAttribute != null) {
      final SCIMAttribute managerId = scimAttribute.getValue().getAttribute("managerId");
      if (managerId == null) {
        throw new InvalidResourceException("The manager attribute does not have a managerId.");
      }

      final String resourceID = managerId.getValue().getStringValue();
      try {
        final String dn = searchResolver.getDnFromId(ldapInterface, resourceID);
        attributes.add(new Attribute(ATTR_MANAGER, dn));
      } catch (ResourceNotFoundException e) {
        // If the manager id is non-existent, we want to return a 400 to the
        // client, not a 404.
        throw new InvalidResourceException("The managerId '" + resourceID + "' does not exist.");
      }
    }
  }
  /**
   * Verify that JSON with missing or malformed schema is handled appropriately.
   *
   * @throws Exception If the test fails.
   */
  @Test
  public void testUnmarshalBadSchema() throws Exception {
    final ResourceDescriptor userResourceDescriptor = CoreSchema.USER_DESCRIPTOR;
    final Unmarshaller unmarshaller = new JsonUnmarshaller();
    InputStream testJson;
    // Test unmarshalling a JSON user entry
    testJson = getResource("/com/unboundid/scim/marshal/spec/mixed-user.json");
    try {
      unmarshaller.unmarshal(testJson, userResourceDescriptor, BaseResource.BASE_RESOURCE_FACTORY);
      fail("Expected JSONUnmarshaller to detect an ambiguous " + "resource representation.");
    } catch (InvalidResourceException e) {
      // expected
      System.err.println(e.getMessage());
    }
    testJson = getResource("/com/unboundid/scim/marshal/spec/mixed-user-malformed.json");
    try {
      unmarshaller.unmarshal(testJson, userResourceDescriptor, BaseResource.BASE_RESOURCE_FACTORY);
      fail("Expected JSONUnmarshaller to detect an ambiguous " + "resource representation.");
    } catch (InvalidResourceException e) {
      // expected
      System.err.println(e.getMessage());
    }

    // Try with implicit schema checking enabled
    testJson = getResource("/com/unboundid/scim/marshal/spec/mixed-user.json");
    try {
      System.setProperty(SCIMConstants.IMPLICIT_SCHEMA_CHECKING_PROPERTY, "true");
      final SCIMObject o =
          unmarshaller
              .unmarshal(testJson, userResourceDescriptor, BaseResource.BASE_RESOURCE_FACTORY)
              .getScimObject();
      assertNotNull(o);
      SCIMAttribute username = o.getAttribute(SCIMConstants.SCHEMA_URI_CORE, "userName");
      assertEquals(username.getValue().getStringValue(), "babs");
      SCIMAttribute employeeNumber =
          o.getAttribute(SCIMConstants.SCHEMA_URI_ENTERPRISE_EXTENSION, "employeeNumber");
      assertEquals(employeeNumber.getValue().getStringValue(), "1");
    } finally {
      System.setProperty(SCIMConstants.IMPLICIT_SCHEMA_CHECKING_PROPERTY, "");
    }
    testJson = getResource("/com/unboundid/scim/marshal/spec/mixed-user-malformed.json");
    try {
      System.setProperty(SCIMConstants.IMPLICIT_SCHEMA_CHECKING_PROPERTY, "true");
      unmarshaller.unmarshal(testJson, userResourceDescriptor, BaseResource.BASE_RESOURCE_FACTORY);
      fail("Expected JSONUnmarshaller to fail because no schema can be found " + "for attribute.");
    } catch (InvalidResourceException e) {
      // expected
      System.err.println(e.getMessage());
    } finally {
      System.setProperty(SCIMConstants.IMPLICIT_SCHEMA_CHECKING_PROPERTY, "");
    }

    // Test unmarshalling a JSON user patch with meta data
    testJson = getResource("/com/unboundid/scim/marshal/spec/meta-user-patch.json");
    try {
      unmarshaller.unmarshal(testJson, userResourceDescriptor, BaseResource.BASE_RESOURCE_FACTORY);
      fail("Expected JSONUnmarshaller to detect an ambiguous " + "resource representation.");
    } catch (InvalidResourceException e) {
      // expected
      System.err.println(e.getMessage());
    }
    // Try with implicit schema checking enabled
    testJson = getResource("/com/unboundid/scim/marshal/spec/meta-user-patch.json");
    try {
      System.setProperty(SCIMConstants.IMPLICIT_SCHEMA_CHECKING_PROPERTY, "true");
      final SCIMObject o =
          unmarshaller
              .unmarshal(testJson, userResourceDescriptor, BaseResource.BASE_RESOURCE_FACTORY)
              .getScimObject();
      assertNotNull(o);
      SCIMAttribute metaAttr = o.getAttribute(SCIMConstants.SCHEMA_URI_CORE, "meta");
      String metaString = metaAttr.toString();
      assertTrue(metaString.contains("department"));
      assertTrue(
          metaString.contains(SCIMConstants.SEPARATOR_CHAR_QUALIFIED_ATTRIBUTE + "department"));
    } finally {
      System.setProperty(SCIMConstants.IMPLICIT_SCHEMA_CHECKING_PROPERTY, "");
    }
  }