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

io.milton.http.annotated.ChildrenOfAnnotationHandler Maven / Gradle / Ivy

Go to download

Milton Community Edition: Supports DAV level 1 and is available on Apache2 license

There is a newer version: 4.0.3.2215
Show newest version
/*
 *
 * Copyright 2014 McEvoy Software Ltd.
 *
 * Licensed 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 io.milton.http.annotated;

import io.milton.annotations.ChildrenOf;
import io.milton.http.Request.Method;
import io.milton.http.exceptions.BadRequestException;
import io.milton.http.exceptions.NotAuthorizedException;
import io.milton.http.exceptions.NotFoundException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 *
 * @author brad
 */
public class ChildrenOfAnnotationHandler extends AbstractAnnotationHandler {

	public ChildrenOfAnnotationHandler(final AnnotationResourceFactory outer) {
		super(outer, ChildrenOf.class, Method.PROPFIND);
	}

	public Set execute(AnnoCollectionResource parent, boolean isChildLookup) throws Exception {
		Set result = new HashSet<>();
		List candidateMethods = getMethods(parent.source.getClass());
		// Find any override methods
		Set overrideSourceTypes = new HashSet<>();
		for (ControllerMethod cm : candidateMethods) {
			ChildrenOf anno = (ChildrenOf) cm.anno;
			if (anno.override()) {
				overrideSourceTypes.add(cm.sourceType);
			}
		}
		// If we have override methods, then remove any methods targeting base classes of the override source types
		if (overrideSourceTypes.size() > 0) {
			Iterator it = candidateMethods.iterator();
			while (it.hasNext()) {
				Class sourceType = it.next().sourceType;
				for (Class overrideClass : overrideSourceTypes) {
					if (overrideClass != sourceType && sourceType.isAssignableFrom(overrideClass)) {
						it.remove();
						break;
					}
				}
			}
		}

		for (ControllerMethod cm : candidateMethods) {
			try {
				if (lookupPermitted(isChildLookup, cm)) {
					Object o = invoke(cm, parent);
					annoResourceFactory.createAndAppend(result, o, parent, cm);
				}
			} catch (NotAuthorizedException | NotFoundException | BadRequestException e) {
				throw e;
			} catch (Exception e) {
				throw new RuntimeException(e);
			}

		}
		return result;
	}

	private boolean lookupPermitted(boolean childLookup, ControllerMethod cm) {
		ChildrenOf anno = (ChildrenOf) cm.anno;
		if (childLookup) {
			return anno.allowChildLookups();
		}
		return true;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy