001package ca.uhn.fhir.rest.server.method;
002
003/*-
004 * #%L
005 * HAPI FHIR - Server Framework
006 * %%
007 * Copyright (C) 2014 - 2019 University Health Network
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 * 
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import ca.uhn.fhir.context.FhirContext;
024import ca.uhn.fhir.rest.api.Constants;
025import ca.uhn.fhir.rest.api.RestOperationTypeEnum;
026import ca.uhn.fhir.rest.api.server.IRestfulServer;
027import ca.uhn.fhir.rest.api.server.RequestDetails;
028import ca.uhn.fhir.rest.param.ParameterUtil;
029import ca.uhn.fhir.rest.server.RestfulServer;
030import ca.uhn.fhir.rest.server.RestfulServerUtils;
031import ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException;
032
033import javax.annotation.Nonnull;
034import java.io.IOException;
035import java.io.Writer;
036import java.lang.reflect.Method;
037
038public class GraphQLMethodBinding extends BaseMethodBinding<String> {
039
040        private final Integer myIdParamIndex;
041
042        public GraphQLMethodBinding(Method theMethod, FhirContext theContext, Object theProvider) {
043                super(theMethod, theContext, theProvider);
044
045                myIdParamIndex = ParameterUtil.findIdParameterIndex(theMethod, theContext);
046        }
047
048        @Override
049        public String getResourceName() {
050                return null;
051        }
052
053        @Nonnull
054        @Override
055        public RestOperationTypeEnum getRestOperationType() {
056                return RestOperationTypeEnum.GRAPHQL_REQUEST;
057        }
058
059        @Override
060        public boolean isGlobalMethod() {
061                return true;
062        }
063
064        @Override
065        public boolean incomingServerRequestMatchesMethod(RequestDetails theRequest) {
066                if ("$graphql".equals(theRequest.getOperation())) {
067                        return true;
068                }
069
070                return false;
071        }
072
073        @Override
074        public Object invokeServer(IRestfulServer<?> theServer, RequestDetails theRequest) throws BaseServerResponseException, IOException {
075                Object[] methodParams = createMethodParams(theRequest);
076                if (myIdParamIndex != null) {
077                        methodParams[myIdParamIndex] = theRequest.getId();
078                }
079
080                Object response = invokeServerMethod(theServer, theRequest, methodParams);
081
082                int statusCode = Constants.STATUS_HTTP_200_OK;
083                String statusMessage = Constants.HTTP_STATUS_NAMES.get(statusCode);
084                String contentType = Constants.CT_JSON;
085                String charset = Constants.CHARSET_NAME_UTF8;
086                boolean respondGzip = theRequest.isRespondGzip();
087
088                Writer writer = theRequest.getResponse().getResponseWriter(statusCode, statusMessage, contentType, charset, respondGzip);
089
090                String responseString = (String) response;
091                writer.write(responseString);
092                writer.close();
093
094                return null;
095        }
096}