All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.psu.swe.scim.server.schema.Registry Maven / Gradle / Ivy

There is a newer version: 2.22.4
Show newest version
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
 
* http://www.apache.org/licenses/LICENSE-2.0

* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package edu.psu.swe.scim.server.schema;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.ejb.Singleton;
import javax.ejb.Startup;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import edu.psu.swe.scim.spec.resources.ScimResource;
import edu.psu.swe.scim.spec.schema.ResourceType;
import edu.psu.swe.scim.spec.schema.Schema;
import lombok.extern.slf4j.Slf4j;

@Singleton
@Startup
@Slf4j
public class Registry {

  private Map schemaMap = new HashMap<>();
  
  private Map> schemaUrnToScimResourceClass = new HashMap<>();

  private Map> endpointToScimResourceClass = new HashMap<>();

  private Map resourceTypeMap = new HashMap<>();
  
  private ObjectMapper objectMapper;

  public Schema getSchema(String urn) {
    return schemaMap.get(urn);
  }

  public Set getAllSchemaUrns() {
    return Collections.unmodifiableSet(schemaMap.keySet());
  }

  public Collection getAllSchemas() {
    return Collections.unmodifiableCollection(schemaMap.values());
  }
  
  public Schema getBaseSchemaOfResourceType(String resourceType) {
    ResourceType rt = resourceTypeMap.get(resourceType);
    if (rt == null) {
      return null;
    }
    
    String schemaUrn = rt.getSchemaUrn();
    return schemaMap.get(schemaUrn);
  }

  public void addSchema(Schema schema) throws JsonProcessingException {
    log.info("Adding schema " + schema.getId() + " into the registry");
    schemaMap.put(schema.getId(), schema);
  }

  public void addSchemaDoc(String schemaDoc) {
    // Unmarshall the JSON document to a Schema and its associated object graph.
    try {
      Schema schema = objectMapper.readValue(schemaDoc, Schema.class);
      schemaMap.put(schema.getId(), schema);
    } catch (Throwable t) {
      log.error("Unexpected Throwable was caught while unmarshalling JSON, schema will not be added: " + t.getLocalizedMessage());
    }
  }

  public  void addScimResourceSchemaUrn(String schemaUrn, Class scimResourceClass) {
    schemaUrnToScimResourceClass.put(schemaUrn, scimResourceClass);
  }

  public  void addScimResourceEndPoint(String endpoint, Class scimResourceClass) {
    endpointToScimResourceClass.put(endpoint, scimResourceClass);
  }

  public  Class findScimResourceClassFromEndpoint(String endpoint) {
    @SuppressWarnings("unchecked")
    Class scimResourceClass = (Class) endpointToScimResourceClass.get(endpoint);

    return scimResourceClass;
  }

  public  Class findScimResourceClass(String schemaUrn) {
    @SuppressWarnings("unchecked")
    Class scimResourceClass = (Class) schemaUrnToScimResourceClass.get(schemaUrn);

    return scimResourceClass;
  }

  public ResourceType getResourceType(String name) {
    return resourceTypeMap.get(name);
  }
  
  public Collection getAllResourceTypes() {
    return Collections.unmodifiableCollection(resourceTypeMap.values());
  }
  
  public void addResourceType(ResourceType resourceType) {
    resourceTypeMap.put(resourceType.getName(), resourceType);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy