uk.gov.gchq.gaffer.rest.controller.OperationController Maven / Gradle / Ivy
/*
* Copyright 2020-2023 Crown Copyright
*
* 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 uk.gov.gchq.gaffer.rest.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
import uk.gov.gchq.gaffer.commonutil.CloseableUtil;
import uk.gov.gchq.gaffer.commonutil.pair.Pair;
import uk.gov.gchq.gaffer.core.exception.GafferRuntimeException;
import uk.gov.gchq.gaffer.core.exception.Status;
import uk.gov.gchq.gaffer.operation.Operation;
import uk.gov.gchq.gaffer.rest.factory.ExamplesFactory;
import uk.gov.gchq.gaffer.rest.factory.GraphFactory;
import uk.gov.gchq.gaffer.rest.factory.UserFactory;
import uk.gov.gchq.gaffer.rest.model.OperationDetail;
import uk.gov.gchq.gaffer.rest.service.v2.AbstractOperationService;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Set;
import static uk.gov.gchq.gaffer.jsonserialisation.JSONSerialiser.createDefaultMapper;
import static uk.gov.gchq.gaffer.rest.ServiceConstants.GAFFER_MEDIA_TYPE;
import static uk.gov.gchq.gaffer.rest.ServiceConstants.GAFFER_MEDIA_TYPE_HEADER;
import static uk.gov.gchq.gaffer.rest.ServiceConstants.JOB_ID_HEADER;
@RestController
public class OperationController extends AbstractOperationService implements IOperationController {
private final GraphFactory graphFactory;
private final UserFactory userFactory;
private final ExamplesFactory examplesFactory;
public final ObjectMapper mapper = createDefaultMapper();
@Autowired
public OperationController(final GraphFactory graphFactory, final UserFactory userFactory, final ExamplesFactory examplesFactory) {
this.graphFactory = graphFactory;
this.userFactory = userFactory;
this.examplesFactory = examplesFactory;
}
@Override
public Set> getOperations() {
return getSupportedOperations();
}
@Override
public Set> getOperationsIncludingUnsupported() {
return getSupportedOperations(true);
}
@Override
public Set getAllOperationDetails() {
return getSupportedOperationDetails();
}
@Override
public Set getAllOperationDetailsIncludingUnsupported() {
return getSupportedOperationDetails(true);
}
@Override
public OperationDetail getOperationDetails(@PathVariable("className") @Parameter(name = "className", description = "The Operation class") final String className) {
try {
final Class extends Operation> operationClass = getOperationClass(className);
if (graphFactory.getGraph().getSupportedOperations().contains(operationClass)) {
return new OperationDetail(operationClass, getNextOperations(operationClass), generateExampleJson(operationClass));
} else {
throw new GafferRuntimeException("Class: " + className + " is not supported by the current store.");
}
} catch (final ClassNotFoundException e) {
throw new GafferRuntimeException("Class: " + className + " was not found on the classpath.", e, Status.NOT_FOUND);
} catch (final ClassCastException e) {
throw new GafferRuntimeException(className + " does not extend Operation", e, Status.BAD_REQUEST);
} catch (final IllegalAccessException | InstantiationException e) {
throw new GafferRuntimeException("Unable to instantiate " + className, e);
}
}
@Override
public Set> getNextOperations(@PathVariable("className") @Parameter(name = "className", description = "The Operation class") final String className) {
Class extends Operation> opClass;
try {
opClass = getOperationClass(className);
} catch (final ClassNotFoundException e) {
throw new GafferRuntimeException("Operation class was not found: " + className, e, Status.NOT_FOUND);
} catch (final ClassCastException e) {
throw new GafferRuntimeException(className + " does not extend Operation", e, Status.BAD_REQUEST);
}
return getNextOperations(opClass);
}
@Override
public Operation getOperationExample(@PathVariable("className") @Parameter(name = "className", description = "The Operation class") final String className) {
Class extends Operation> operationClass;
try {
operationClass = getOperationClass(className);
} catch (final ClassNotFoundException e) {
throw new GafferRuntimeException("Class: " + className + " was not found on the classpath.", e, Status.NOT_FOUND);
} catch (final ClassCastException e) {
throw new GafferRuntimeException(className + " does not extend Operation", e, Status.BAD_REQUEST);
}
try {
return generateExampleJson(operationClass);
} catch (final InstantiationException | IllegalAccessException e) {
throw new GafferRuntimeException("Unable to create example for class " + className, e, Status.INTERNAL_SERVER_ERROR);
}
}
@Override
public ResponseEntity
© 2015 - 2024 Weber Informatics LLC | Privacy Policy