blob: 0d0c8837b64004b1e9a2951c34f083aaf2916244 [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 org.apache.directory.scim.server.exception.UnableToUpdateResourceException;
import org.apache.directory.scim.server.provider.Provider;
import org.apache.directory.scim.server.provider.UpdateRequest;
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 jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Named;
@Named
@ApplicationScoped
public class InMemoryGroupService implements Provider<ScimGroup> {
private final Map<String, ScimGroup> groups = new HashMap<>();
@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) {
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;
}
groups.put(id, resource);
resource.setId(id);
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) {
return new FilterResponse<>(groups.values(), pageRequest, groups.size());
}
@Override
public List<Class<? extends ScimExtension>> getExtensionList() {
return Collections.emptyList();
}
}