Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2014. Escalon System-Entwicklung, Dietrich Schulten
*
* 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 de.escalon.hypermedia.spring;
import de.escalon.hypermedia.PropertyUtils;
import de.escalon.hypermedia.action.Action;
import de.escalon.hypermedia.action.Cardinality;
import de.escalon.hypermedia.action.Input;
import de.escalon.hypermedia.action.ResourceHandler;
import de.escalon.hypermedia.affordance.ActionDescriptor;
import de.escalon.hypermedia.affordance.ActionInputParameter;
import de.escalon.hypermedia.affordance.DataType;
import de.escalon.hypermedia.affordance.PartialUriTemplate;
import org.jetbrains.annotations.NotNull;
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.hateoas.MethodLinkBuilderFactory;
import org.springframework.hateoas.Resources;
import org.springframework.hateoas.core.AnnotationMappingDiscoverer;
import org.springframework.hateoas.core.DummyInvocationUtils;
import org.springframework.hateoas.core.MappingDiscoverer;
import org.springframework.hateoas.core.MethodParameters;
import org.springframework.hateoas.mvc.ControllerLinkBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.beans.PropertyDescriptor;
import java.lang.annotation.Annotation;
import java.lang.reflect.*;
import java.util.*;
/**
* Factory for {@link AffordanceBuilder}s in a Spring MVC rest service. Normally one should use the static methods of
* AffordanceBuilder to get an AffordanceBuilder. Created by dschulten on 03.10.2014.
*/
public class AffordanceBuilderFactory implements MethodLinkBuilderFactory {
private static final MappingDiscoverer MAPPING_DISCOVERER = new AnnotationMappingDiscoverer(RequestMapping.class);
@Override
public AffordanceBuilder linkTo(Method method, Object... parameters) {
return linkTo(method.getDeclaringClass(), method, parameters);
}
@Override
public AffordanceBuilder linkTo(Class> controller, Method method, Object... parameters) {
String pathMapping = MAPPING_DISCOVERER.getMapping(controller, method);
Map requestParamNames = getRequestParamNames(method);
Map inputBeanParamNames = getInputBeanParamNames(method);
String query = join(requestParamNames, inputBeanParamNames);
// explicitly write out params where variable name and param are different, rest as continuation
String mapping = StringUtils.isEmpty(query) ? pathMapping : pathMapping + query;
PartialUriTemplate partialUriTemplate = new PartialUriTemplate(AffordanceBuilder.getBuilder()
.build()
.toString() + mapping);
Map values = new HashMap();
Iterator variableNames = partialUriTemplate.getVariableNames()
.iterator();
// there may be more or less mapping variables than arguments
for (Object parameter : parameters) {
if (!variableNames.hasNext()) {
break;
}
values.put(variableNames.next(), parameter);
}
// there may be more or less mapping variables than arguments
// do not use input bean param names here
for (Object argument : parameters) {
if (!variableNames.hasNext()) {
break;
}
String variableName = variableNames.next();
if (!inputBeanParamNames.containsKey(variableName)) {
values.put(variableName, argument);
}
}
ActionDescriptor actionDescriptor = createActionDescriptor(method, values, parameters);
return new AffordanceBuilder(partialUriTemplate.expand(values), Collections.singletonList(actionDescriptor));
}
private String join(Map... params) {
StringBuilder levelFourQuery = new StringBuilder();
for (Map paramMap : params) {
for (Map.Entry parameter : paramMap.entrySet()) {
if (levelFourQuery.length() > 0) {
levelFourQuery.append(",");
}
levelFourQuery.append(parameter.getValue());
}
}
StringBuilder ret = new StringBuilder();
if (levelFourQuery.length() > 0) {
ret.append("{?")
.append(levelFourQuery)
.append("}");
}
return ret.toString();
}
@Override
public AffordanceBuilder linkTo(Class> target) {
return linkTo(target, new Object[0]);
}
@Override
public AffordanceBuilder linkTo(Class> controller, Object... parameters) {
Assert.notNull(controller);
String mapping = MAPPING_DISCOVERER.getMapping(controller);
PartialUriTemplate partialUriTemplate = new PartialUriTemplate(mapping == null ? "/" : mapping);
Map values = new HashMap();
Iterator names = partialUriTemplate.getVariableNames()
.iterator();
// there may be more or less mapping variables than arguments
for (Object parameter : parameters) {
if (!names.hasNext()) {
break;
}
values.put(names.next(), parameter);
}
return new AffordanceBuilder().slash(partialUriTemplate.expand(values));
}
@Override
public AffordanceBuilder linkTo(Class> controller, Map parameters) {
String mapping = MAPPING_DISCOVERER.getMapping(controller);
PartialUriTemplate partialUriTemplate = new PartialUriTemplate(mapping == null ? "/" : mapping);
return new AffordanceBuilder().slash(partialUriTemplate.expand(parameters));
}
@Override
public AffordanceBuilder linkTo(Object invocationValue) {
Assert.isInstanceOf(DummyInvocationUtils.LastInvocationAware.class, invocationValue);
DummyInvocationUtils.LastInvocationAware invocations = (DummyInvocationUtils.LastInvocationAware)
invocationValue;
DummyInvocationUtils.MethodInvocation invocation = invocations.getLastInvocation();
Method invokedMethod = invocation.getMethod();
String pathMapping = MAPPING_DISCOVERER.getMapping(invokedMethod);
Iterator