Search in sources :

Example 1 with DescriptionType

use of ee.ria.xroad.common.conf.serverconf.model.DescriptionType in project X-Road by nordic-institute.

the class RestMetadataServiceHandlerTest method init.

/**
 * Init data for tests
 */
@Before
public void init() {
    GlobalConf.reload(new TestSuiteGlobalConf());
    KeyConf.reload(new TestSuiteKeyConf());
    ServerConf.reload(new TestSuiteServerConf() {

        @Override
        public DescriptionType getDescriptionType(ServiceId service) {
            return DescriptionType.OPENAPI3;
        }

        @Override
        public String getServiceDescriptionURL(ServiceId service) {
            if (SUBSYSTEM_FOR_JSON_FILE.equals(service.getSubsystemCode())) {
                return "http://localhost:9858/petstore.json";
            } else if (SUBSYSTEM_FOR_UNSUPPORTED_YAML_FILE.equals(service.getSubsystemCode())) {
                return "http://localhost:9858/openapi_incompatible_version.yaml";
            } else {
                return "http://localhost:9858/petstore.yaml";
            }
        }
    });
    httpClientMock = mock(HttpClient.class);
    mockRequest = mock(HttpServletRequest.class);
    mockResponse = mock(HttpServletResponse.class);
    mockProxyMessage = mock(ProxyMessage.class);
    mockServer = new WireMockServer(options().port(MOCK_SERVER_PORT));
    mockServer.stubFor(WireMock.any(urlPathEqualTo("/petstore.json")).willReturn(aResponse().withBodyFile("petstore.json")));
    mockServer.stubFor(WireMock.any(urlPathEqualTo("/petstore.yaml")).willReturn(aResponse().withBodyFile("petstore.yaml")));
    mockServer.start();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ProxyMessage(ee.ria.xroad.proxy.protocol.ProxyMessage) TestSuiteGlobalConf(ee.ria.xroad.proxy.testsuite.TestSuiteGlobalConf) DescriptionType(ee.ria.xroad.common.conf.serverconf.model.DescriptionType) TestSuiteKeyConf(ee.ria.xroad.proxy.testsuite.TestSuiteKeyConf) TestSuiteServerConf(ee.ria.xroad.proxy.testsuite.TestSuiteServerConf) HttpClient(org.apache.http.client.HttpClient) HttpServletResponse(javax.servlet.http.HttpServletResponse) WireMockServer(com.github.tomakehurst.wiremock.WireMockServer) ServiceId(ee.ria.xroad.common.identifier.ServiceId) Before(org.junit.Before)

Example 2 with DescriptionType

use of ee.ria.xroad.common.conf.serverconf.model.DescriptionType in project X-Road by nordic-institute.

the class AllowedMethodsMessage method startUp.

@Override
protected void startUp() throws Exception {
    super.startUp();
    ServerConf.reload(new TestSuiteServerConf() {

        @Override
        public List<ServiceId> getAllowedServicesByDescriptionType(ClientId serviceProvider, ClientId client, DescriptionType descriptionType) {
            assertThat("Wrong client in query", client, is(expectedClientQuery));
            assertThat("Wrong service provider in query", serviceProvider, is(expectedProviderQuery));
            return expectedServices;
        }
    });
}
Also used : DescriptionType(ee.ria.xroad.common.conf.serverconf.model.DescriptionType) TestSuiteServerConf(ee.ria.xroad.proxy.testsuite.TestSuiteServerConf) ClientId(ee.ria.xroad.common.identifier.ClientId) List(java.util.List)

Example 3 with DescriptionType

use of ee.ria.xroad.common.conf.serverconf.model.DescriptionType in project X-Road by nordic-institute.

the class ListMethodsMessage method startUp.

@Override
protected void startUp() throws Exception {
    super.startUp();
    ServerConf.reload(new TestSuiteServerConf() {

        @Override
        public List<ServiceId> getServicesByDescriptionType(ClientId serviceProvider, DescriptionType descriptionType) {
            assertThat("Client id does not match expected", serviceProvider, is(expectedClientId));
            return expectedServices;
        }
    });
}
Also used : DescriptionType(ee.ria.xroad.common.conf.serverconf.model.DescriptionType) TestSuiteServerConf(ee.ria.xroad.proxy.testsuite.TestSuiteServerConf) ClientId(ee.ria.xroad.common.identifier.ClientId) List(java.util.List)

Example 4 with DescriptionType

use of ee.ria.xroad.common.conf.serverconf.model.DescriptionType in project X-Road by nordic-institute.

the class RestMetadataServiceHandlerImpl method handleGetOpenApi.

private void handleGetOpenApi(ProxyMessage requestProxyMessage) throws IOException, HttpClientCreator.HttpClientCreatorException, URISyntaxException {
    List<NameValuePair> pairs = URLEncodedUtils.parse(requestProxyMessage.getRest().getQuery(), Charset.forName("UTF-8"));
    String targetServiceCode = null;
    for (NameValuePair pair : pairs) {
        log.trace("{} : {}", pair.getName(), pair.getValue());
        if (pair.getName().equalsIgnoreCase(QUERY_PARAM_SERVICECODE)) {
            targetServiceCode = pair.getValue();
        }
    }
    if (targetServiceCode == null || targetServiceCode.isEmpty()) {
        throw new CodedException(X_INVALID_REQUEST, "Missing serviceCode in message body");
    }
    ServiceId targetServiceId = ServiceId.create(requestProxyMessage.getRest().getServiceId().getClientId(), targetServiceCode);
    log.trace("targetServiceId={}", targetServiceId);
    DescriptionType descriptionType = ServerConf.getDescriptionType(targetServiceId);
    if (descriptionType == null) {
        throw new CodedException(X_INTERNAL_ERROR, String.format("Service not found: %s", targetServiceId.toString()));
    }
    if (descriptionType != DescriptionType.OPENAPI3) {
        throw new CodedException(X_INTERNAL_ERROR, String.format("Invalid service type: %s", descriptionType.toString()));
    }
    String serviceDescriptionURL = ServerConf.getServiceDescriptionURL(targetServiceId);
    HttpClient client = httpClientCreator.getHttpClient();
    HttpContext httpContext = new BasicHttpContext();
    // ServerMessageProcessor uses the same method to pass the ServiceId to CustomSSLSocketFactory
    httpContext.setAttribute(ServiceId.class.getName(), targetServiceId);
    URI uri = new URI(serviceDescriptionURL);
    HttpResponse response = client.execute(new HttpGet(uri), httpContext);
    StatusLine statusLine = response.getStatusLine();
    if (HttpStatus.SC_OK != statusLine.getStatusCode()) {
        throw new CodedException(X_INTERNAL_ERROR, String.format("Failed reading service description from %s. Status: %s Reason: %s", serviceDescriptionURL, statusLine.getStatusCode(), statusLine.getReasonPhrase()));
    }
    InputStream responseContent = response.getEntity().getContent();
    try {
        OpenapiDescriptionFiletype filetype = getFileType(response, uri);
        Openapi3Anonymiser anonymiser = new Openapi3Anonymiser();
        if (OpenapiDescriptionFiletype.JSON.equals(filetype)) {
            anonymiser.anonymiseJson(responseContent, restResponseBody);
        } else {
            anonymiser.anonymiseYaml(responseContent, restResponseBody);
        }
    } catch (IOException e) {
        throw new CodedException(X_INTERNAL_ERROR, String.format("Failed overwriting origin URL for the openapi servers for %s", serviceDescriptionURL));
    }
    if (response.containsHeader(MimeUtils.HEADER_CONTENT_TYPE)) {
        restResponse.getHeaders().add(new BasicHeader(MimeUtils.HEADER_CONTENT_TYPE, response.getFirstHeader(MimeUtils.HEADER_CONTENT_TYPE).getValue()));
    } else {
        restResponse.getHeaders().add(new BasicHeader(MimeUtils.HEADER_CONTENT_TYPE, DEFAULT_GETOPENAPI_CONTENT_TYPE));
    }
}
Also used : NameValuePair(org.apache.http.NameValuePair) DescriptionType(ee.ria.xroad.common.conf.serverconf.model.DescriptionType) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) URI(java.net.URI) ServiceId(ee.ria.xroad.common.identifier.ServiceId) StatusLine(org.apache.http.StatusLine) CodedException(ee.ria.xroad.common.CodedException) HttpClient(org.apache.http.client.HttpClient) OpenapiDescriptionFiletype(ee.ria.xroad.common.util.OpenapiDescriptionFiletype) BasicHeader(org.apache.http.message.BasicHeader)

Example 5 with DescriptionType

use of ee.ria.xroad.common.conf.serverconf.model.DescriptionType in project X-Road by nordic-institute.

the class RestProxyTest method shouldNotAllowCallingWSDLServices.

@Test
public void shouldNotAllowCallingWSDLServices() {
    ServerConf.reload(new TestServerConf(servicePort) {

        @Override
        public DescriptionType getDescriptionType(ServiceId service) {
            if ("wsdl".equals(service.getServiceCode())) {
                return DescriptionType.WSDL;
            }
            return DescriptionType.REST;
        }
    });
    given().baseUri("http://127.0.0.1").port(proxyClientPort).header("X-Road-Client", "EE/BUSINESS/consumer/subsystem").get(PREFIX + "/EE/BUSINESS/producer/sub/wsdl").then().statusCode(500).header("X-Road-Error", "Server.ServerProxy.ServiceType");
}
Also used : TestServerConf(ee.ria.xroad.proxy.testutil.TestServerConf) DescriptionType(ee.ria.xroad.common.conf.serverconf.model.DescriptionType) ServiceId(ee.ria.xroad.common.identifier.ServiceId) Test(org.junit.Test)

Aggregations

DescriptionType (ee.ria.xroad.common.conf.serverconf.model.DescriptionType)10 ServiceId (ee.ria.xroad.common.identifier.ServiceId)6 CodedException (ee.ria.xroad.common.CodedException)5 TestSuiteServerConf (ee.ria.xroad.proxy.testsuite.TestSuiteServerConf)5 ClientId (ee.ria.xroad.common.identifier.ClientId)4 Test (org.junit.Test)4 WireMockServer (com.github.tomakehurst.wiremock.WireMockServer)3 ProxyMessage (ee.ria.xroad.proxy.protocol.ProxyMessage)3 TestSuiteGlobalConf (ee.ria.xroad.proxy.testsuite.TestSuiteGlobalConf)3 TestSuiteKeyConf (ee.ria.xroad.proxy.testsuite.TestSuiteKeyConf)3 WireMock (com.github.tomakehurst.wiremock.client.WireMock)2 WireMock.aResponse (com.github.tomakehurst.wiremock.client.WireMock.aResponse)2 WireMock.urlPathEqualTo (com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo)2 WireMockConfiguration.options (com.github.tomakehurst.wiremock.core.WireMockConfiguration.options)2 ErrorCodes (ee.ria.xroad.common.ErrorCodes)2 X_INVALID_SERVICE_TYPE (ee.ria.xroad.common.ErrorCodes.X_INVALID_SERVICE_TYPE)2 X_UNKNOWN_SERVICE (ee.ria.xroad.common.ErrorCodes.X_UNKNOWN_SERVICE)2 SystemProperties (ee.ria.xroad.common.SystemProperties)2 GlobalConf (ee.ria.xroad.common.conf.globalconf.GlobalConf)2 ServerConf (ee.ria.xroad.common.conf.serverconf.ServerConf)2