@Override
  public <T> Action getAction(T input) {
    if (!canHandle(input)) {
      return null;
    }

    Metacard metacard = (Metacard) input;

    if (StringUtils.isBlank(metacard.getId())) {
      LOGGER.info("No id given. No action to provide.");
      return null;
    }

    if (isHostUnset(SystemBaseUrl.getHost())) {
      LOGGER.info("Host name/ip not set. Cannot create link for metacard.");
      return null;
    }

    String metacardId = null;
    String metacardSource = null;

    try {
      metacardId = URLEncoder.encode(metacard.getId(), CharEncoding.UTF_8);
      metacardSource = URLEncoder.encode(getSource(metacard), CharEncoding.UTF_8);
    } catch (UnsupportedEncodingException e) {
      LOGGER.info("Unsupported Encoding exception", e);
      return null;
    }

    return getAction(metacardId, metacardSource);
  }
 protected void verifyBasics(Metacard metacard) {
   assertEquals(DEFAULT_TITLE, metacard.getTitle());
   assertEquals(DEFAULT_TYPE, metacard.getContentTypeName());
   assertEquals(DEFAULT_VERSION, metacard.getContentTypeVersion());
   assertEquals(
       sampleMetadata().replaceAll("\\s", ""), metacard.getMetadata().replaceAll("\\s", ""));
 }
  private String generateSchemaLocationFromMetacards(
      List<Metacard> metacards, Map<String, String> prefixToUriMapping) {
    if (metacards != null) {
      StringBuilder descFeatureService = new StringBuilder();
      descFeatureService
          .append(contextRoot)
          .append("/wfs?service=wfs&request=DescribeFeatureType&version=2.0.0&typeName=");

      StringBuilder schemaLocation = new StringBuilder();
      Set<QName> qnames = new HashSet<QName>();
      for (Metacard metacard : metacards) {
        qnames.add(
            WfsQnameBuilder.buildQName(
                metacard.getMetacardType().getName(), metacard.getContentTypeName()));
      }
      for (QName qname : qnames) {
        prefixToUriMapping.put(qname.getPrefix(), qname.getNamespaceURI());
        schemaLocation
            .append(qname.getNamespaceURI())
            .append(" ")
            .append(descFeatureService)
            .append(qname.getPrefix())
            .append(":")
            .append(qname.getLocalPart())
            .append(" ");
      }
      return schemaLocation.toString();
    } else {
      LOGGER.warn("Metacard list is null");
      return null;
    }
  }
  protected String getSource(Metacard metacard) {

    if (StringUtils.isNotBlank(metacard.getSourceId())) {
      return metacard.getSourceId();
    }

    return SystemInfo.getSiteName();
  }
  @Test
  public void testSetId() throws IOException, CatalogTransformerException, Exception {

    Metacard metacard =
        createTransformer()
            .transform(new ByteArrayInputStream(sampleNMEAString().getBytes()), SAMPLE_ID);

    assertEquals(SAMPLE_ID + " doesn't equal " + metacard.getId(), SAMPLE_ID, metacard.getId());
  }
 @Test
 public void testGroovySource() throws Exception {
   InputStream stream =
       Thread.currentThread().getContextClassLoader().getResourceAsStream("testGROOVY.groovy");
   Metacard metacard = transform(stream);
   assertNotNull(metacard);
   assertNotNull(metacard.getMetadata());
   assertThat(metacard.getMetadata(), containsString("this is a comment"));
   assertThat(metacard.getContentTypeName(), containsString("text/plain"));
 }
 @Test
 public void testAudioMidi() throws Exception {
   InputStream stream =
       Thread.currentThread().getContextClassLoader().getResourceAsStream("testMID.mid");
   Metacard metacard = transform(stream);
   assertNotNull(metacard);
   assertNotNull(metacard.getMetadata());
   assertThat(metacard.getMetadata(), containsString("PPQ"));
   assertThat(metacard.getContentTypeName(), is("audio/midi"));
 }
 @Test
 public void testAudioAiff() throws Exception {
   InputStream stream =
       Thread.currentThread().getContextClassLoader().getResourceAsStream("testAIFF.aif");
   Metacard metacard = transform(stream);
   assertNotNull(metacard);
   assertNotNull(metacard.getMetadata());
   assertThat(metacard.getMetadata(), containsString("PCM_SIGNED"));
   assertThat(metacard.getContentTypeName(), is("audio/x-aiff"));
 }
 private void setupMockBasicMetacard(String sourceId, String resourceUri, String metacardId)
     throws URISyntaxException {
   when(mockMetacard.getId()).thenReturn(metacardId);
   if (sourceId != null) {
     when(mockMetacard.getSourceId()).thenReturn(sourceId);
   }
   if (resourceUri != null) {
     when(mockMetacard.getResourceURI()).thenReturn(new URI(resourceUri));
   }
 }
 @Test
 public void testCppSource() throws Exception {
   InputStream stream =
       Thread.currentThread().getContextClassLoader().getResourceAsStream("testCPP.cpp");
   Metacard metacard = transform(stream);
   assertNotNull(metacard);
   assertNotNull(metacard.getMetadata());
   assertThat(metacard.getMetadata(), containsString("Hello world example"));
   assertThat(metacard.getContentTypeName(), containsString("text/plain"));
 }
  protected void applyUpdateOperationRules(
      UpdateRequest updateRequest,
      Entry<Serializable, Metacard> anUpdate,
      Metacard aMetacard,
      Date now) {

    if (UpdateRequest.UPDATE_BY_ID.equals(updateRequest.getAttributeName())
        && !anUpdate.getKey().toString().equals(aMetacard.getId())) {

      LOGGER.info(
          "{} in metacard must match the Update {}, overwriting metacard {} [{}] with the update identifier [{}]",
          Metacard.ID,
          Metacard.ID,
          Metacard.ID,
          aMetacard.getId(),
          anUpdate.getKey());
      aMetacard.setAttribute(new AttributeImpl(Metacard.ID, anUpdate.getKey()));
    }

    if (aMetacard.getCreatedDate() == null) {
      LOGGER.info(
          "{} date should match the original metacard. Changing date to current timestamp so it is at least not null.",
          Metacard.CREATED);
      aMetacard.setAttribute(new AttributeImpl(Metacard.CREATED, now));
    }

    if (aMetacard.getModifiedDate() == null) {
      aMetacard.setAttribute(new AttributeImpl(Metacard.MODIFIED, now));
    }

    if (aMetacard.getEffectiveDate() == null) {
      aMetacard.setAttribute(new AttributeImpl(Metacard.EFFECTIVE, now));
    }
  }
  private void assertListStringAttribute(Metacard mc, String attrName, String[] expectedValues) {
    if (mc.getAttribute(attrName) != null) {
      List<?> values = (List<?>) mc.getAttribute(attrName).getValues();
      assertThat(values, not(nullValue()));
      assertThat(values.size(), equalTo(expectedValues.length));

      List<String> valuesList = new ArrayList<String>();
      valuesList.addAll((List<? extends String>) values);
      assertThat(valuesList, hasItems(expectedValues));
    }
  }
 @Test
 public void testBmp() throws Exception {
   InputStream stream =
       Thread.currentThread().getContextClassLoader().getResourceAsStream("testBMP.bmp");
   Metacard metacard = transform(stream);
   assertNotNull(metacard);
   assertNotNull(metacard.getMetadata());
   assertThat(
       metacard.getMetadata(),
       containsString("<meta name=\"Compression CompressionTypeName\" content=\"BI_RGB\"/>"));
   assertThat(metacard.getContentTypeName(), is("image/x-ms-bmp"));
 }
 @Test
 public void testJavaSource() throws Exception {
   InputStream stream =
       Thread.currentThread()
           .getContextClassLoader()
           .getResourceAsStream("testpackage/testJAVA.java");
   Metacard metacard = transform(stream);
   assertNotNull(metacard);
   assertNotNull(metacard.getMetadata());
   assertThat(metacard.getMetadata(), containsString("HelloWorld"));
   assertThat(metacard.getContentTypeName(), containsString("text/plain"));
 }
 @Test
 public void testPng() throws Exception {
   InputStream stream =
       Thread.currentThread().getContextClassLoader().getResourceAsStream("testPNG.png");
   Metacard metacard = transform(stream);
   assertNotNull(metacard);
   assertNotNull(metacard.getMetadata());
   assertThat(
       metacard.getMetadata(),
       containsString("<meta name=\"Compression Lossless\" content=\"true\"/>"));
   assertThat(metacard.getContentTypeName(), is("image/png"));
 }
 @Test
 public void testTiff() throws Exception {
   InputStream stream =
       Thread.currentThread().getContextClassLoader().getResourceAsStream("testTIFF.tif");
   Metacard metacard = transform(stream);
   assertNotNull(metacard);
   assertNotNull(metacard.getMetadata());
   assertThat(
       metacard.getMetadata(),
       containsString("<meta name=\"tiff:BitsPerSample\" content=\"8\"/>"));
   assertThat(metacard.getContentTypeName(), is("image/tiff"));
 }
 @Test
 public void testJavaClass() throws Exception {
   InputStream stream =
       Thread.currentThread()
           .getContextClassLoader()
           .getResourceAsStream("CatalogFrameworkImpl.class");
   Metacard metacard = transform(stream);
   assertNotNull(metacard);
   assertThat(metacard.getTitle(), is("CatalogFrameworkImpl"));
   assertNotNull(metacard.getMetadata());
   assertThat(metacard.getMetadata(), containsString("DEFAULT_RESOURCE_NOT_FOUND_MESSAGE"));
   assertThat(metacard.getContentTypeName(), is("application/java-vm"));
 }
  @Test
  public void testFailingInput()
      throws IOException, CatalogTransformerException, ParseException, Exception {

    Metacard metacard =
        createTransformer().transform(new ByteArrayInputStream(sampleNMEAString2().getBytes()));

    assertEquals("FEDERAL ST LAURENT(8PNN)", metacard.getTitle());

    WKTReader reader = new WKTReader();

    assertEquals(metacard.getLocation(), null);
  }
 @Test
 public void testMp3() throws Exception {
   InputStream stream =
       Thread.currentThread().getContextClassLoader().getResourceAsStream("testMP3id3v1_v2.mp3");
   Metacard metacard = transform(stream);
   assertNotNull(metacard);
   assertThat(metacard.getTitle(), is("Test Title"));
   assertNotNull(metacard.getMetadata());
   assertThat(
       metacard.getMetadata(),
       containsString("<meta name=\"xmpDM:artist\" content=\"Test Artist\"/>"));
   assertThat(metacard.getContentTypeName(), is("audio/mpeg"));
 }
  @Test()
  public void testIPhone()
      throws IOException, CatalogTransformerException, UnsupportedQueryException,
          SourceUnavailableException, FederationException, ParseException {
    File file = new File(TEST_DATA_PATH + "Apple iPhone 4.jpg");
    FileInputStream fis = FileUtils.openInputStream(file);
    Metacard metacard = createTransformer().transform(fis);

    assertNotNull(metacard);

    assertNotNull(metacard.getCreatedDate());
    assertThat(metacard.getCreatedDate().getYear() + 1900, is(2011));
    assertThat(metacard.getCreatedDate().getMonth() + 1, is(1));
    assertThat(metacard.getCreatedDate().getDate(), is(13));
    assertThat(metacard.getCreatedDate().getHours(), is(14));
    assertThat(metacard.getCreatedDate().getMinutes(), is(33));
    assertThat(metacard.getCreatedDate().getSeconds(), is(39));

    assertEquals(metacard.getCreatedDate(), metacard.getModifiedDate());

    WKTReader reader = new WKTReader();
    Geometry geometry = reader.read(metacard.getLocation());
    assertEquals(12.488833, geometry.getCoordinate().x, 0.00001);
    assertEquals(41.853, geometry.getCoordinate().y, 0.00001);
  }
Example #21
0
 @Override
 public PolicyResponse processPostQuery(Result input, Map<String, Serializable> properties)
     throws StopProcessingException {
   HashMap<String, Set<String>> itemPolicy = new HashMap<>();
   Metacard metacard = input.getMetacard();
   if (metacard.getTags().contains(RegistryConstants.REGISTRY_TAG)) {
     if ((whiteList && !registryEntryIds.contains(metacard.getId()))
         || (!whiteList && registryEntryIds.contains(metacard.getId()))) {
       itemPolicy.putAll(bypassAccessPolicy);
     } else {
       itemPolicy.putAll(readAccessPolicy);
     }
   }
   return new PolicyResponseImpl(new HashMap<>(), itemPolicy);
 }
Example #22
0
 private PolicyResponse getWritePolicy(Metacard input, Map<String, Serializable> properties) {
   HashMap<String, Set<String>> operationPolicy = new HashMap<>();
   if (Requests.isLocal(properties) && input.getTags().contains(RegistryConstants.REGISTRY_TAG)) {
     Attribute attribute = input.getAttribute(RegistryObjectMetacardType.REGISTRY_BASE_URL);
     if (isRegistryDisabled()
         || (attribute != null
             && attribute.getValue() instanceof String
             && ((String) attribute.getValue()).startsWith(SystemBaseUrl.getBaseUrl()))) {
       operationPolicy.putAll(bypassAccessPolicy);
     } else {
       operationPolicy.putAll(writeAccessPolicy);
     }
   }
   return new PolicyResponseImpl(operationPolicy, new HashMap<>());
 }
  @Override
  public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
    Map<String, String> cswAttrMap =
        new CaseInsensitiveMap(
            DefaultCswRecordMap.getDefaultCswRecordMap().getCswToMetacardAttributeNames());
    Object mappingObj = context.get(CswConstants.CSW_MAPPING);

    if (mappingObj instanceof Map<?, ?>) {
      CswUnmarshallHelper.removeExistingAttributes(cswAttrMap, (Map<String, String>) mappingObj);
    }

    String resourceUriMapping =
        (isString(context.get(Metacard.RESOURCE_URI)))
            ? (String) context.get(Metacard.RESOURCE_URI)
            : null;

    String thumbnailMapping =
        (isString(context.get(Metacard.THUMBNAIL)))
            ? (String) context.get(Metacard.THUMBNAIL)
            : null;

    CswAxisOrder cswAxisOrder = CswAxisOrder.LON_LAT;
    Object cswAxisOrderObject = context.get(CswConstants.AXIS_ORDER_PROPERTY);

    if (cswAxisOrderObject != null && cswAxisOrderObject.getClass().isEnum()) {
      Enum value = (Enum) cswAxisOrderObject;
      cswAxisOrder = CswAxisOrder.valueOf(value.name());
    }

    Map<String, String> namespaceMap = null;
    Object namespaceObj = context.get(CswConstants.NAMESPACE_DECLARATIONS);

    if (namespaceObj instanceof Map<?, ?>) {
      namespaceMap = (Map<String, String>) namespaceObj;
    }

    Metacard metacard =
        CswUnmarshallHelper.createMetacardFromCswRecord(
            reader, cswAttrMap, resourceUriMapping, thumbnailMapping, cswAxisOrder, namespaceMap);

    Object sourceIdObj = context.get(Metacard.SOURCE_ID);

    if (sourceIdObj instanceof String) {
      metacard.setSourceId((String) sourceIdObj);
    }

    return metacard;
  }
  /** Test where the subsample count is missing from the configuration. */
  @Test
  public void testMissingConfiguration() {

    locationKlvProcessor.process(handlers, metacard, klvConfiguration);

    assertThat(metacard.getLocation(), nullValue());
  }
Example #25
0
 @Override
 public ResourceResponse processPostResource(ResourceResponse input, Metacard metacard)
     throws StopProcessingException {
   if (input.getRequest() == null || input.getRequest().getProperties() == null) {
     throw new StopProcessingException(
         "Unable to filter contents of current message, no user Subject available.");
   }
   KeyValueCollectionPermission securityPermission =
       new KeyValueCollectionPermission(CollectionPermission.READ_ACTION);
   Subject subject = getSubject(input);
   Attribute attr = metacard.getAttribute(Metacard.SECURITY);
   if (!checkPermissions(attr, securityPermission, subject, CollectionPermission.READ_ACTION)) {
     for (FilterStrategy filterStrategy : filterStrategies.values()) {
       FilterResult filterResult = filterStrategy.process(input, metacard);
       if (filterResult.processed()) {
         if (filterResult.response() == null) {
           throw new StopProcessingException("Subject not permitted to receive resource");
         } else {
           input = (ResourceResponse) filterResult.response();
         }
         break;
         // returned metacards are ignored for resource requests
       }
     }
     if (filterStrategies.size() == 0) {
       throw new StopProcessingException("Subject not permitted to receive resource");
     }
   }
   return input;
 }
  @Test
  public void testOpenOffice() throws Exception {
    InputStream stream =
        Thread.currentThread().getContextClassLoader().getResourceAsStream("testOpenOffice2.odt");

    /*
     * The dates in testOpenOffice2.odt do not contain timezones. If no timezone is specified,
     * the Tika input transformer assumes the local time zone.  Set the system timezone to UTC
     * so we can do assertions.
     */
    TimeZone defaultTimeZone = TimeZone.getDefault();
    TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

    Metacard metacard = transform(stream);
    assertNotNull(metacard);
    assertThat(metacard.getTitle(), is("Test OpenOffice2 Document"));
    assertThat(convertDate(metacard.getCreatedDate()), is("2007-09-14 11:06:08 UTC"));
    assertThat(convertDate(metacard.getModifiedDate()), is("2013-02-13 06:52:10 UTC"));
    assertNotNull(metacard.getMetadata());
    assertThat(
        metacard.getMetadata(),
        containsString("This is a sample Open Office document, written in NeoOffice 2.2.1"));
    assertThat(metacard.getContentTypeName(), is("application/vnd.oasis.opendocument.text"));

    // Reset timezone back to local time zone.
    TimeZone.setDefault(defaultTimeZone);
  }
 private void assertMetacard(Metacard mc, Map<String, Object> expectedValues) {
   assertThat(mc.getId(), equalTo((String) expectedValues.get(Metacard.ID)));
   assertListStringAttribute(
       mc,
       CswRecordMetacardType.CSW_IDENTIFIER,
       (String[]) expectedValues.get(CswRecordMetacardType.CSW_IDENTIFIER));
   assertThat(mc.getTitle(), equalTo((String) expectedValues.get(Metacard.TITLE)));
   assertListStringAttribute(
       mc,
       CswRecordMetacardType.CSW_TITLE,
       (String[]) expectedValues.get(CswRecordMetacardType.CSW_TITLE));
   assertThat(mc.getModifiedDate(), equalTo((Date) expectedValues.get(Metacard.MODIFIED)));
   assertListStringAttribute(
       mc,
       CswRecordMetacardType.CSW_MODIFIED,
       (String[]) expectedValues.get(CswRecordMetacardType.CSW_MODIFIED));
   assertListStringAttribute(
       mc,
       CswRecordMetacardType.CSW_SUBJECT,
       (String[]) expectedValues.get(CswRecordMetacardType.CSW_SUBJECT));
   assertListStringAttribute(
       mc,
       CswRecordMetacardType.CSW_ABSTRACT,
       (String[]) expectedValues.get(CswRecordMetacardType.CSW_ABSTRACT));
   assertListStringAttribute(
       mc,
       CswRecordMetacardType.CSW_RIGHTS,
       (String[]) expectedValues.get(CswRecordMetacardType.CSW_RIGHTS));
   assertListStringAttribute(
       mc,
       CswRecordMetacardType.CSW_LANGUAGE,
       (String[]) expectedValues.get(CswRecordMetacardType.CSW_LANGUAGE));
   assertThat(
       (String) mc.getAttribute(CswRecordMetacardType.CSW_TYPE).getValue(),
       equalTo((String) expectedValues.get(CswRecordMetacardType.CSW_TYPE)));
   assertListStringAttribute(
       mc,
       CswRecordMetacardType.CSW_FORMAT,
       (String[]) expectedValues.get(CswRecordMetacardType.CSW_FORMAT));
   assertThat(mc.getLocation(), equalTo((String) expectedValues.get(Metacard.GEOGRAPHY)));
   assertListStringAttribute(
       mc,
       CswRecordMetacardType.OWS_BOUNDING_BOX,
       (String[]) expectedValues.get(CswRecordMetacardType.OWS_BOUNDING_BOX));
 }
  @Test
  public void testGeoTaggedJpeg() throws Exception {
    InputStream stream =
        Thread.currentThread().getContextClassLoader().getResourceAsStream("testJPEG_GEO.jpg");

    /*
     * The dates in testJPED_GEO.jpg do not contain timezones. If no timezone is specified,
     * the Tika input transformer assumes the local time zone.  Set the system timezone to UTC
     * so we can do assertions.
     */
    TimeZone defaultTimeZone = TimeZone.getDefault();
    TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

    Metacard metacard = transform(stream);
    assertNotNull(metacard);
    assertNotNull(metacard.getMetadata());
    assertThat(
        metacard.getMetadata(), containsString("<meta name=\"Model\" content=\"Canon EOS 40D\"/>"));
    assertThat(metacard.getContentTypeName(), is("image/jpeg"));
    assertThat(convertDate(metacard.getCreatedDate()), is("2009-08-11 09:09:45 UTC"));
    assertThat(convertDate(metacard.getModifiedDate()), is("2009-10-02 23:02:49 UTC"));
    assertThat(
        (String) metacard.getAttribute(Metacard.GEOGRAPHY).getValue(),
        is("POINT(-54.1234 12.54321)"));

    // Reset timezone back to local time zone.
    TimeZone.setDefault(defaultTimeZone);
  }
  @Test
  public void testProcess() {

    klvConfiguration.set(KlvProcessor.Configuration.SUBSAMPLE_COUNT, 50);

    locationKlvProcessor.process(handlers, metacard, klvConfiguration);

    assertThat(metacard.getLocation(), is(wkt));
  }
Example #30
0
  @Override
  public QueryResponse processPostQuery(QueryResponse input) throws StopProcessingException {
    if (input.getRequest() == null || input.getRequest().getProperties() == null) {
      throw new StopProcessingException(
          "Unable to filter contents of current message, no user Subject available.");
    }
    Subject subject = getSubject(input);

    List<Result> results = input.getResults();
    List<Result> newResults = new ArrayList<>(results.size());
    Metacard metacard;
    KeyValueCollectionPermission securityPermission =
        new KeyValueCollectionPermission(CollectionPermission.READ_ACTION);
    int filteredMetacards = 0;
    for (Result result : results) {
      metacard = result.getMetacard();
      Attribute attr = metacard.getAttribute(Metacard.SECURITY);
      if (!checkPermissions(attr, securityPermission, subject, CollectionPermission.READ_ACTION)) {
        for (FilterStrategy filterStrategy : filterStrategies.values()) {
          FilterResult filterResult = filterStrategy.process(input, metacard);
          if (filterResult.processed()) {
            if (filterResult.metacard() != null) {
              newResults.add(new ResultImpl(filterResult.metacard()));
            }
            break;
            // returned responses are ignored for queries
          }
        }
        filteredMetacards++;
      } else {
        newResults.add(result);
      }
    }

    LOGGER.info("Filtered {} metacards, returned {}", filteredMetacards, newResults.size());
    SecurityLogger.logInfo(
        "Filtered " + filteredMetacards + " metacards, returned " + newResults.size());

    input.getResults().clear();
    input.getResults().addAll(newResults);
    newResults.clear();
    return input;
  }