blob: fca8299b2e54aabc430cc02bc1b854a939e13cc1 [file] [log] [blame]
/*
* 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 org.apache.directory.scim.example.jersey.service;
import jakarta.annotation.PostConstruct;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.Response;
import org.apache.directory.scim.server.exception.UnableToCreateResourceException;
import org.apache.directory.scim.server.exception.UnableToUpdateResourceException;
import org.apache.directory.scim.server.repository.Repository;
import org.apache.directory.scim.server.repository.UpdateRequest;
import org.apache.directory.scim.server.schema.SchemaRegistry;
import org.apache.directory.scim.spec.protocol.filter.FilterExpressions;
import org.apache.directory.scim.spec.protocol.filter.FilterResponse;
import org.apache.directory.scim.spec.protocol.search.Filter;
import org.apache.directory.scim.spec.protocol.search.PageRequest;
import org.apache.directory.scim.spec.protocol.search.SortRequest;
import org.apache.directory.scim.spec.resources.ScimExtension;
import org.apache.directory.scim.spec.resources.ScimGroup;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Named;
@Named
@ApplicationScoped
public class InMemoryGroupService implements Repository<ScimGroup> {
private final Map<String, ScimGroup> groups = new HashMap<>();
private SchemaRegistry schemaRegistry;
@Inject
public InMemoryGroupService(SchemaRegistry schemaRegistry) {
this.schemaRegistry = schemaRegistry;
}
protected InMemoryGroupService() {}
@PostConstruct
public void init() {
ScimGroup group = new ScimGroup();
group.setId("example-group");
groups.put(group.getId(), group);
}
@Override
public Class<ScimGroup> getResourceClass() {
return ScimGroup.class;
}
@Override
public ScimGroup create(ScimGroup resource) throws UnableToCreateResourceException {
String resourceId = resource.getId();
int idCandidate = resource.hashCode();
String id = resourceId != null ? resourceId : Integer.toString(idCandidate);
while (groups.containsKey(id)) {
id = Integer.toString(idCandidate);
++idCandidate;
}
// check to make sure the group doesn't already exist
boolean existingUserFound = groups.values().stream()
.anyMatch(group -> group.getExternalId().equals(resource.getExternalId()));
if (existingUserFound) {
// HTTP leaking into data layer
throw new UnableToCreateResourceException(Response.Status.CONFLICT, "Group '" + resource.getExternalId() + "' already exists.");
}
resource.setId(id);
groups.put(id, resource);
return resource;
}
@Override
public ScimGroup update(UpdateRequest<ScimGroup> updateRequest) throws UnableToUpdateResourceException {
String id = updateRequest.getId();
ScimGroup resource = updateRequest.getResource();
groups.put(id, resource);
return resource;
}
@Override
public ScimGroup get(String id) {
return groups.get(id);
}
@Override
public void delete(String id) {
groups.remove(id);
}
@Override
public FilterResponse<ScimGroup> find(Filter filter, PageRequest pageRequest, SortRequest sortRequest) {
long count = pageRequest.getCount() != null ? pageRequest.getCount() : groups.size();
long startIndex = pageRequest.getStartIndex() != null
? pageRequest.getStartIndex() - 1 // SCIM is 1-based indexed
: 0;
List<ScimGroup> result = groups.values().stream()
.skip(startIndex)
.limit(count)
.filter(FilterExpressions.inMemory(filter, schemaRegistry.getSchema(ScimGroup.SCHEMA_URI)))
.collect(Collectors.toList());
return new FilterResponse<>(result, pageRequest, result.size());
}
@Override
public List<Class<? extends ScimExtension>> getExtensionList() {
return Collections.emptyList();
}
}