Exemplo n.º 1
0
  private Response sendEvent(String operation, Metacard... metacards) {
    try {
      List<Result> results =
          Arrays.asList(metacards).stream().map(ResultImpl::new).collect(Collectors.toList());

      SourceResponse queryResponse = new QueryResponseImpl(query, results, true, metacards.length);

      LOGGER.debug(
          "Attempting to transform an Event with mime-type: {} & outputSchema: {}",
          mimeType,
          outputSchema);

      QueryResponseTransformer transformer;
      Map<String, Serializable> arguments = new HashMap<>();
      if (StringUtils.isBlank(outputSchema)
          && StringUtils.isNotBlank(mimeType)
          && !XML_MIME_TYPES.contains(mimeType)) {
        transformer = transformerManager.getTransformerByMimeType(mimeType);
      } else {
        transformer = transformerManager.getTransformerBySchema(CswConstants.CSW_OUTPUT_SCHEMA);
        if (elementName != null) {
          arguments.put(CswConstants.ELEMENT_NAMES, elementName.toArray());
        }
        arguments.put(CswConstants.OUTPUT_SCHEMA_PARAMETER, outputSchema);
        arguments.put(CswConstants.ELEMENT_SET_TYPE, elementSetType);
        arguments.put(CswConstants.IS_BY_ID_QUERY, false);
        arguments.put(CswConstants.GET_RECORDS, request);
        arguments.put(CswConstants.RESULT_TYPE_PARAMETER, resultType);
        arguments.put(CswConstants.WRITE_NAMESPACES, false);
      }
      if (transformer == null) {
        throw new WebApplicationException(
            new CatalogTransformerException("Unable to locate Transformer."));
      }

      BinaryContent binaryContent = transformer.transform(queryResponse, arguments);

      if (binaryContent == null) {
        throw new WebApplicationException(
            new CatalogTransformerException("Transformer returned null."));
      }

      return webClient.invoke(operation, binaryContent.getByteArray());
    } catch (IOException | CatalogTransformerException | RuntimeException e) {
      LOGGER.error("Error sending event to {}.", callbackUrl, e);
    }
    return null;
  }
Exemplo n.º 2
0
  @Before
  public void setUp() throws Exception {
    System.setProperty("ddf.home", ".");
    callbackURI = new URL("https://localhost:12345/services/csw/subscription/event");
    ObjectFactory objectFactory = new ObjectFactory();
    request = new GetRecordsType();
    request.setOutputSchema(CswConstants.CSW_OUTPUT_SCHEMA);
    request.setResultType(ResultType.RESULTS);
    request.getResponseHandler().add(callbackURI.toString());
    queryType = new QueryType();
    elementSetNameType = new ElementSetNameType();
    elementSetNameType.setValue(ElementSetType.BRIEF);
    queryType.setElementSetName(elementSetNameType);
    request.setAbstractQuery(objectFactory.createAbstractQuery(queryType));
    transformerManager = mock(TransformerManager.class);
    transformer = mock(QueryResponseTransformer.class);
    binaryContent = mock(BinaryContent.class);
    when(transformerManager.getTransformerBySchema(
            Matchers.contains(CswConstants.CSW_OUTPUT_SCHEMA)))
        .thenReturn(transformer);
    when(transformer.transform(any(SourceResponse.class), anyMap())).thenReturn(binaryContent);
    when(binaryContent.getByteArray()).thenReturn("byte array with message contents".getBytes());
    query = mock(QueryRequest.class);

    metacard = mock(Metacard.class);
    webclient = mock(WebClient.class);
    mockCxfClientFactory = mock(SecureCxfClientFactory.class);
    response = mock(Response.class);
    subject = mock(Subject.class);

    mockSecurity = mock(Security.class);
    headers.put(Subject.class.toString(), Arrays.asList(new Subject[] {subject}));
    AccessPlugin accessPlugin = mock(AccessPlugin.class);
    accessPlugins.add(accessPlugin);
    when(mockCxfClientFactory.getWebClient()).thenReturn(webclient);
    //        when(webclient.head()).thenReturn(response);
    when(webclient.invoke(anyString(), any(QueryResponse.class))).thenReturn(response);
    when(response.getHeaders()).thenReturn(headers);
    when(accessPlugin.processPostQuery(any(QueryResponse.class)))
        .thenAnswer(
            new Answer<QueryResponse>() {
              @Override
              public QueryResponse answer(InvocationOnMock invocationOnMock) throws Throwable {
                return (QueryResponse) invocationOnMock.getArguments()[0];
              }
            });

    sendEvent = new SendEventExtension(transformerManager, request, query, mockCxfClientFactory);
    sendEvent.setSubject(subject);
  }