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.
/*
* 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.solr.api;
import java.io.Closeable;
import java.io.IOException;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.SpecProvider;
import org.apache.solr.common.util.*;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.response.SolrQueryResponse;
import org.apache.solr.security.AuthorizationContext;
import org.apache.solr.security.PermissionNameProvider;
import org.apache.solr.util.SolrJacksonAnnotationInspector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This class implements an Api just from an annotated java class
* The class must have an annotation {@link EndPoint}
* Each method must have an annotation {@link Command}
* The methods that implement a command should have the first 2 parameters
* {@link SolrQueryRequest} and {@link SolrQueryResponse} or it may optionally
* have a third parameter which could be a java class annotated with jackson annotations.
* The third parameter is only valid if it is using a json command payload
*/
public class AnnotatedApi extends Api implements PermissionNameProvider , Closeable {
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
public static final String ERR = "Error executing commands :";
private EndPoint endPoint;
private final Map commands ;
private final Cmd singletonCommand;
private final Api fallback;
@Override
public void close() throws IOException {
for (Cmd value : commands.values()) {
if (value.obj instanceof Closeable) {
((Closeable) value.obj).close();
}
break;// all objects are same so close only one
}
}
public EndPoint getEndPoint() {
return endPoint;
}
public static List getApis(Object obj) {
return getApis(obj.getClass(), obj);
}
public static List getApis(final Class klas , Object obj) {
if (!Modifier.isPublic(klas.getModifiers())) {
throw new RuntimeException(klas.getName() + " is not public");
}
if (klas.getAnnotation(EndPoint.class) != null) {
EndPoint endPoint = klas.getAnnotation(EndPoint.class);
List methods = new ArrayList<>();
Map commands = new HashMap<>();
for (Method m : klas.getMethods()) {
Command command = m.getAnnotation(Command.class);
if (command != null) {
methods.add(m);
if (commands.containsKey(command.name())) {
throw new RuntimeException("Duplicate commands " + command.name());
}
commands.put(command.name(), new Cmd(command.name(), obj, m));
}
}
if (commands.isEmpty()) {
throw new RuntimeException("No method with @Command in class: " + klas.getName());
}
SpecProvider specProvider = readSpec(endPoint, methods);
return Collections.singletonList(new AnnotatedApi(specProvider, endPoint, commands, null));
} else {
List apis = new ArrayList<>();
for (Method m : klas.getMethods()) {
EndPoint endPoint = m.getAnnotation(EndPoint.class);
if (endPoint == null) continue;
Cmd cmd = new Cmd("", obj, m);
SpecProvider specProvider = readSpec(endPoint, Collections.singletonList(m));
apis.add(new AnnotatedApi(specProvider, endPoint, Collections.singletonMap("", cmd), null));
}
if (apis.isEmpty()) {
throw new RuntimeException("Invalid Class : " + klas.getName() + " No @EndPoints");
}
return apis;
}
}
private AnnotatedApi(SpecProvider specProvider, EndPoint endPoint, Map commands, Api fallback) {
super(specProvider);
this.endPoint = endPoint;
this.fallback = fallback;
this.commands = commands;
this.singletonCommand = commands.get("");
}
@Override
public Name getPermissionName(AuthorizationContext request) {
return endPoint.permission();
}
@SuppressWarnings({"unchecked", "rawtypes"})
private static SpecProvider readSpec(EndPoint endPoint, List m) {
return () -> {
Map map = new LinkedHashMap();
List methods = new ArrayList<>();
for (SolrRequest.METHOD method : endPoint.method()) {
methods.add(method.name());
}
map.put("methods", methods);
map.put("url", new ValidatingJsonMap(Collections.singletonMap("paths", Arrays.asList(endPoint.path()))));
Map cmds = new HashMap<>();
for (Method method : m) {
Command command = method.getAnnotation(Command.class);
if (command != null && !command.name().isEmpty()) {
cmds.put(command.name(), AnnotatedApi.createSchema(method));
}
}
if (!cmds.isEmpty()) {
map.put("commands", cmds);
}
return new ValidatingJsonMap(map);
};
}
@Override
public void call(SolrQueryRequest req, SolrQueryResponse rsp) {
if (singletonCommand != null) {
singletonCommand.invoke(req, rsp, null);
return;
}
List cmds = req.getCommands(true);
boolean allExists = true;
for (CommandOperation cmd : cmds) {
if (!commands.containsKey(cmd.name)) {
cmd.addError("No such command supported: " + cmd.name);
allExists = false;
}
}
if (!allExists) {
if (fallback != null) {
fallback.call(req, rsp);
return;
} else {
throw new ApiBag.ExceptionWithErrObject(SolrException.ErrorCode.BAD_REQUEST, "Error processing commands",
CommandOperation.captureErrors(cmds));
}
}
for (CommandOperation cmd : cmds) {
commands.get(cmd.name).invoke(req, rsp, cmd);
}
@SuppressWarnings({"rawtypes"})
List