@Override
  public final void write(List<? extends Document> documents) throws Exception {

    for (Document doc : documents) {
      String[] cmd = {
        imageMagickProg,
        "-density",
        thumbnailDensity,
        "-depth",
        "8",
        "-quality",
        "85",
        "-resize",
        "1600x800",
        pdfOutputPath + File.separator + doc.getDocName() + ".pdf[0]",
        outputPath + File.separator + doc.getDocName() + ".png"
      };

      try {
        Process process = new ProcessBuilder(cmd).start();
        IOUtils.closeQuietly(process.getOutputStream());
        InputStream processInputStream = process.getInputStream();
        logStream(processInputStream);
        FutureTask<Integer> waitTask = new FutureTask<>(process::waitFor);
        Thread waitThread = new Thread(waitTask);
        waitThread.start();
        try {
          waitTask.get(30, TimeUnit.SECONDS);
        } catch (Exception e) {
          LOG.error(e.getMessage());
          waitThread.interrupt();
          process.destroy();
          waitTask.cancel(true);
        } finally {
          IOUtils.closeQuietly(processInputStream);
          process.destroy();
          waitThread.interrupt();
          waitTask.cancel(true);
        }
      } catch (IOException e) {

      }
    }
  }
  @Test
  public void testUnmarshalGetRecordsResponseConversionWithEmptyBoundingBox() {
    XStream xstream = new XStream(new WstxDriver());
    xstream.setClassLoader(this.getClass().getClassLoader());

    GetRecordsResponseConverter grrc = new GetRecordsResponseConverter(mockProvider);
    xstream.registerConverter(grrc);
    xstream.alias("GetRecordsResponse", CswRecordCollection.class);

    String xml =
        "<csw:GetRecordsResponse xmlns:csw=\"http://www.opengis.net/cat/csw\">\r\n"
            + "  <csw:SearchStatus status=\"subset\" timestamp=\"2013-05-01T02:13:36+0200\"/>\r\n"
            + "  <csw:SearchResults elementSet=\"full\" nextRecord=\"11\" numberOfRecordsMatched=\"479\" numberOfRecordsReturned=\"10\" recordSchema=\"csw:Record\">\r\n"
            + "    <csw:Record xmlns:csw=\"http://www.opengis.net/cat/csw\">\r\n"
            + "      <dc:identifier xmlns:dc=\"http://purl.org/dc/elements/1.1/\">{8C1F6297-EC96-4302-A01E-14988C9149FD}</dc:identifier>\r\n"
            + "      <dc:title xmlns:dc=\"http://purl.org/dc/elements/1.1/\">title 1</dc:title>\r\n"
            + "      <dct:modified xmlns:dct=\"http://purl.org/dc/terms/\">2008-12-15</dct:modified>\r\n"
            + "      <dc:subject xmlns:dc=\"http://purl.org/dc/elements/1.1/\">subject 1</dc:subject>\r\n"
            + "      <dc:subject xmlns:dc=\"http://purl.org/dc/elements/1.1/\">second subject</dc:subject>\r\n"
            + "      <dct:abstract xmlns:dct=\"http://purl.org/dc/terms/\">abstract 1</dct:abstract>\r\n"
            + "      <dc:rights xmlns:dc=\"http://purl.org/dc/elements/1.1/\">copyright 1</dc:rights>\r\n"
            + "      <dc:rights xmlns:dc=\"http://purl.org/dc/elements/1.1/\">copyright 2</dc:rights>\r\n"
            + "      <dc:language xmlns:dc=\"http://purl.org/dc/elements/1.1/\">english</dc:language>      \r\n"
            + "      <ows:BoundingBox xmlns:ows=\"http://www.opengis.net/ows\" crs=\"EPSG:RD_New (28992)\">\r\n"
            + "        <ows:LowerCorner></ows:LowerCorner>\r\n"
            + "        <ows:UpperCorner></ows:UpperCorner>\r\n"
            + "      </ows:BoundingBox>   \r\n"
            + "      <dc:type xmlns:dc=\"http://purl.org/dc/elements/1.1/\">dataset</dc:type>\r\n"
            + "      <dc:format xmlns:dc=\"http://purl.org/dc/elements/1.1/\">Shapefile</dc:format> \r\n"
            + "    </csw:Record>\r\n"
            + "  </csw:SearchResults>\r\n"
            + "</csw:GetRecordsResponse>";
    InputStream inStream = IOUtils.toInputStream(xml);
    CswRecordCollection cswRecords = (CswRecordCollection) xstream.fromXML(inStream);
    IOUtils.closeQuietly(inStream);

    assertThat(cswRecords.getNumberOfRecordsReturned(), is(10L));
    assertThat(cswRecords.getNumberOfRecordsMatched(), is(479L));

    List<Metacard> metacards = cswRecords.getCswRecords();
    assertThat(metacards, not(nullValue()));
    assertThat(metacards.size(), equalTo(1));
  }
  @Override
  @SuppressWarnings("unchecked")
  public String encode(List<CalendarEvent> events) {

    if (events == null || events.isEmpty()) {
      throw new IllegalArgumentException("The calendar events must be defined to encode them");
    }
    Calendar calendarIcs = new Calendar();
    calendarIcs.getProperties().add(new ProdId("-//Silverpeas//iCal4j 1.1//FR"));
    calendarIcs.getProperties().add(Version.VERSION_2_0);
    calendarIcs.getProperties().add(CalScale.GREGORIAN);
    List<VEvent> iCalEvents = new ArrayList<VEvent>();
    ByteArrayOutputStream output = new ByteArrayOutputStream(10240);
    for (CalendarEvent event : events) {
      Date startDate = anICal4JDateCodec().encode(event.getStartDate());
      Date endDate = anICal4JDateCodec().encode(event.getEndDate());
      VEvent iCalEvent;
      if (event.isOnAllDay() && startDate.equals(endDate)) {
        iCalEvent = new VEvent(startDate, event.getTitle());
      } else {
        iCalEvent = new VEvent(startDate, endDate, event.getTitle());
      }

      // Generate UID
      iCalEvent.getProperties().add(generator.generateUid());

      // Add recurring data if any
      if (event.isRecurring()) {
        CalendarEventRecurrence eventRecurrence = event.getRecurrence();
        Recur recur = anICal4JRecurrenceCodec().encode(eventRecurrence);
        iCalEvent.getProperties().add(new RRule(recur));
        iCalEvent.getProperties().add(exceptionDatesFrom(eventRecurrence));
      }
      // Add Description
      iCalEvent.getProperties().add(new Description(event.getDescription()));
      // Add Classification
      iCalEvent.getProperties().add(new Clazz(event.getAccessLevel()));
      // Add Priority
      iCalEvent.getProperties().add(new Priority(event.getPriority()));

      // Add location if any
      if (!event.getLocation().isEmpty()) {
        iCalEvent.getProperties().add(new Location(event.getLocation()));
      }

      // Add event URL if any
      if (event.getUrl() != null) {
        try {
          iCalEvent.getProperties().add(new Url(event.getUrl().toURI()));
        } catch (URISyntaxException ex) {
          throw new EncodingException(ex.getMessage(), ex);
        }
      }

      // Add Categories
      TextList categoryList = new TextList(event.getCategories().asArray());
      if (!categoryList.isEmpty()) {
        iCalEvent.getProperties().add(new Categories(categoryList));
      }
      // Add attendees
      for (String attendee : event.getAttendees().asList()) {
        try {
          iCalEvent.getProperties().add(new Attendee(attendee));
        } catch (URISyntaxException ex) {
          throw new EncodingException("Malformed attendee URI: " + attendee, ex);
        }
      }

      iCalEvents.add(iCalEvent);
    }
    calendarIcs.getComponents().addAll(iCalEvents);
    CalendarOutputter outputter = new CalendarOutputter();
    try {
      outputter.output(calendarIcs, output);
      return output.toString(CharEncoding.UTF_8);
    } catch (Exception ex) {
      throw new EncodingException(
          "The encoding of the events in iCal formatted text has failed!", ex);
    } finally {
      IOUtils.closeQuietly(output);
    }
  }
  @Test
  public void testUnmarshalParseXmlNamespaces() throws XmlPullParserException {

    XStream xstream = new XStream(new WstxDriver());
    xstream.setClassLoader(this.getClass().getClassLoader());

    xstream.registerConverter(new GetRecordsResponseConverter(mockProvider));
    xstream.alias("csw:GetRecordsResponse", CswRecordCollection.class);

    String xml =
        "<?xml version='1.0' encoding='UTF-8'?>"
            + "<csw:GetRecordsResponse "
            + "xmlns:dc=\"http://purl.org/dc/elements/1.1/\" "
            + "xmlns:dct=\"http://purl.org/dc/terms/\" "
            + "xmlns:ows=\"http://www.opengis.net/ows\" "
            + "xmlns:csw=\"http://www.opengis.net/cat/csw/2.0.2\" "
            + "version=\"2.0.2\"><csw:SearchStatus "
            + "timestamp=\"2014-11-11T10:53:32.152-06:00\"/>"
            + "<csw:SearchResults numberOfRecordsMatched=\"1\" "
            + "numberOfRecordsReturned=\"1\" nextRecord=\"0\" "
            + "recordSchema=\"http://www.opengis.net/cat/csw/2.0.2\">"
            + "<csw:Record>\n"
            + "<dc:identifier>0a2e1b1d2a3755e70a96d61e6bddbc5d</dc:identifier>"
            + "<dct:bibliographicCitation>0a2e1b1d2a3755e70a96d61e6bddbc5d</dct:bibliographicCitation>"
            + "<dc:title>US woman attacks Gauguin painting</dc:title>"
            + "<dct:alternative>US woman attacks Gauguin painting</dct:alternative>"
            + "<dc:type>video</dc:type><dc:date>2011-04-06T04:49:20.230-05:00</dc:date>"
            + "<dct:modified>2011-04-06T04:49:20.230-05:00</dct:modified>"
            + "<dct:created>2011-04-06T04:49:20.230-05:00</dct:created>"
            + "<dct:dateAccepted>2011-04-06T04:48:26.180-05:00</dct:dateAccepted>"
            + "<dct:dateCopyrighted>2011-04-06T04:48:26.180-05:00</dct:dateCopyrighted><"
            + "dct:dateSubmitted>2011-04-06T04:49:20.230-05:00</dct:dateSubmitted>"
            + "<dct:issued>2011-04-06T04:49:20.230-05:00</dct:issued>"
            + "<dc:publisher>ddf.distribution</dc:publisher>"
            + "<ows:BoundingBox crs=\"urn:x-ogc:def:crs:EPSG:6.11:4326\">\n"
            + "    <ows:LowerCorner>-50.5056430529222 84.0285103635943</ows:LowerCorner>"
            + "<ows:UpperCorner>-50.5056430529222 84.0285103635943</ows:UpperCorner>"
            + "</ows:BoundingBox></csw:Record><"
            + "/csw:SearchResults>"
            + "</csw:GetRecordsResponse>";
    InputStream inStream = IOUtils.toInputStream(xml);

    ArgumentCaptor<UnmarshallingContext> captor =
        ArgumentCaptor.forClass(UnmarshallingContext.class);

    HierarchicalStreamReader reader =
        new XppReader(
            new InputStreamReader(inStream), XmlPullParserFactory.newInstance().newPullParser());
    xstream.unmarshal(reader, null, null);
    IOUtils.closeQuietly(inStream);

    verify(mockProvider, times(1)).unmarshal(any(HierarchicalStreamReader.class), captor.capture());

    UnmarshallingContext context = captor.getValue();

    assertThat(context, notNullValue());
    assertThat(context.get(CswConstants.NAMESPACE_DECLARATIONS), is(Map.class));
    Map<String, String> namespaces = (Map) context.get(CswConstants.NAMESPACE_DECLARATIONS);
    assertThat(
        namespaces.get(
            CswConstants.XMLNS
                + CswConstants.NAMESPACE_DELIMITER
                + CswConstants.CSW_NAMESPACE_PREFIX),
        is(CswConstants.CSW_OUTPUT_SCHEMA));
    assertThat(
        namespaces.get(
            CswConstants.XMLNS
                + CswConstants.NAMESPACE_DELIMITER
                + CswConstants.DUBLIN_CORE_NAMESPACE_PREFIX),
        is(CswConstants.DUBLIN_CORE_SCHEMA));
    assertThat(
        namespaces.get(
            CswConstants.XMLNS
                + CswConstants.NAMESPACE_DELIMITER
                + CswConstants.DUBLIN_CORE_TERMS_NAMESPACE_PREFIX),
        is(CswConstants.DUBLIN_CORE_TERMS_SCHEMA));
    assertThat(
        namespaces.get(
            CswConstants.XMLNS
                + CswConstants.NAMESPACE_DELIMITER
                + CswConstants.OWS_NAMESPACE_PREFIX),
        is(CswConstants.OWS_NAMESPACE));
  }
  /**
   * This test acutally runs the full thread of calling the GetRecordsResponseConverter then calls
   * the CswInputTransformer.
   */
  @Test
  public void testUnmarshalGetRecordsResponseFull() {

    XStream xstream = new XStream(new WstxDriver());
    xstream.setClassLoader(this.getClass().getClassLoader());

    CswTransformProvider provider = new CswTransformProvider(null, mockInputManager);

    when(mockInputManager.getTransformerBySchema(anyString())).thenReturn(new CswRecordConverter());

    xstream.registerConverter(new GetRecordsResponseConverter(provider));
    xstream.alias("GetRecordsResponse", CswRecordCollection.class);

    String xml =
        "<csw:GetRecordsResponse xmlns:csw=\"http://www.opengis.net/cat/csw\">\r\n"
            + "  <csw:SearchStatus status=\"subset\" timestamp=\"2013-05-01T02:13:36+0200\"/>\r\n"
            + "  <csw:SearchResults elementSet=\"full\" nextRecord=\"11\" numberOfRecordsMatched=\"479\" numberOfRecordsReturned=\"10\" recordSchema=\"csw:Record\">\r\n"
            + "    <csw:Record xmlns:csw=\"http://www.opengis.net/cat/csw\">\r\n"
            + "      <dc:identifier xmlns:dc=\"http://purl.org/dc/elements/1.1/\">{8C1F6297-EC96-4302-A01E-14988C9149FD}</dc:identifier>\r\n"
            + "      <dc:title xmlns:dc=\"http://purl.org/dc/elements/1.1/\">title 1</dc:title>\r\n"
            + "      <dct:modified xmlns:dct=\"http://purl.org/dc/terms/\">2008-12-15</dct:modified>\r\n"
            + "      <dc:subject xmlns:dc=\"http://purl.org/dc/elements/1.1/\">subject 1</dc:subject>\r\n"
            + "      <dc:subject xmlns:dc=\"http://purl.org/dc/elements/1.1/\">second subject</dc:subject>\r\n"
            + "      <dct:abstract xmlns:dct=\"http://purl.org/dc/terms/\">abstract 1</dct:abstract>\r\n"
            + "      <dc:rights xmlns:dc=\"http://purl.org/dc/elements/1.1/\">copyright 1</dc:rights>\r\n"
            + "      <dc:rights xmlns:dc=\"http://purl.org/dc/elements/1.1/\">copyright 2</dc:rights>\r\n"
            + "      <dc:language xmlns:dc=\"http://purl.org/dc/elements/1.1/\">english</dc:language>      \r\n"
            + "      <ows:BoundingBox xmlns:ows=\"http://www.opengis.net/ows\" crs=\"EPSG:RD_New (28992)\">\r\n"
            + "        <ows:LowerCorner>5.121 52.139</ows:LowerCorner>\r\n"
            + "        <ows:UpperCorner>4.468 52.517</ows:UpperCorner>\r\n"
            + "      </ows:BoundingBox>   \r\n"
            + "      <dc:type xmlns:dc=\"http://purl.org/dc/elements/1.1/\">dataset</dc:type>\r\n"
            + "      <dc:format xmlns:dc=\"http://purl.org/dc/elements/1.1/\">Shapefile</dc:format> \r\n"
            + "    </csw:Record>\r\n"
            + "    <csw:Record xmlns:csw=\"http://www.opengis.net/cat/csw\">\r\n"
            + "      <dc:identifier xmlns:dc=\"http://purl.org/dc/elements/1.1/\">{23362852-F370-4369-B0B2-BE74B2859614}</dc:identifier>\r\n"
            + "      <dc:title xmlns:dc=\"http://purl.org/dc/elements/1.1/\">mc2 title</dc:title>\r\n"
            + "      <dct:modified xmlns:dct=\"http://purl.org/dc/terms/\">2010-12-15</dct:modified>\r\n"
            + "      <dc:subject xmlns:dc=\"http://purl.org/dc/elements/1.1/\">first subject</dc:subject>\r\n"
            + "      <dc:subject xmlns:dc=\"http://purl.org/dc/elements/1.1/\">subject 2</dc:subject>\r\n"
            + "      <dct:abstract xmlns:dct=\"http://purl.org/dc/terms/\">mc2 abstract</dct:abstract>\r\n"
            + "      <dc:rights xmlns:dc=\"http://purl.org/dc/elements/1.1/\">first copyright</dc:rights>\r\n"
            + "      <dc:rights xmlns:dc=\"http://purl.org/dc/elements/1.1/\">second copyright</dc:rights>\r\n"
            + "      <dc:language xmlns:dc=\"http://purl.org/dc/elements/1.1/\">english</dc:language>\r\n"
            + "      <ows:BoundingBox xmlns:ows=\"http://www.opengis.net/ows\" crs=\"EPSG:RD_New (28992)\">\r\n"
            + "        <ows:LowerCorner>6.121 53.139</ows:LowerCorner>\r\n"
            + "        <ows:UpperCorner>5.468 53.517</ows:UpperCorner>\r\n"
            + "      </ows:BoundingBox>\r\n"
            + "      <dc:type xmlns:dc=\"http://purl.org/dc/elements/1.1/\">dataset 2</dc:type>\r\n"
            + "      <dc:format xmlns:dc=\"http://purl.org/dc/elements/1.1/\">Shapefile 2</dc:format>\r\n"
            + "    </csw:Record>\r\n"
            + "  </csw:SearchResults>\r\n"
            + "</csw:GetRecordsResponse>";
    InputStream inStream = IOUtils.toInputStream(xml);
    CswRecordCollection cswRecords = (CswRecordCollection) xstream.fromXML(inStream);
    IOUtils.closeQuietly(inStream);

    List<Metacard> metacards = cswRecords.getCswRecords();
    assertThat(metacards, not(nullValue()));
    assertThat(metacards.size(), equalTo(2));

    // verify first metacard's values
    Metacard mc = metacards.get(0);
    assertThat(mc, not(nullValue()));
    Map<String, Object> expectedValues = new HashMap<String, Object>();
    expectedValues.put(Metacard.ID, "{8C1F6297-EC96-4302-A01E-14988C9149FD}");
    expectedValues.put(
        CswRecordMetacardType.CSW_IDENTIFIER,
        new String[] {"{8C1F6297-EC96-4302-A01E-14988C9149FD}"});
    expectedValues.put(Metacard.TITLE, "title 1");
    expectedValues.put(CswRecordMetacardType.CSW_TITLE, new String[] {"title 1"});
    String expectedModifiedDateStr = "2008-12-15";
    DateTimeFormatter dateFormatter = ISODateTimeFormat.dateOptionalTimeParser();
    Date expectedModifiedDate = dateFormatter.parseDateTime(expectedModifiedDateStr).toDate();
    expectedValues.put(CswRecordMetacardType.CSW_MODIFIED, new String[] {expectedModifiedDateStr});
    expectedValues.put(Metacard.MODIFIED, expectedModifiedDate);
    expectedValues.put(
        CswRecordMetacardType.CSW_SUBJECT, new String[] {"subject 1", "second subject"});
    expectedValues.put(CswRecordMetacardType.CSW_ABSTRACT, new String[] {"abstract 1"});
    expectedValues.put(
        CswRecordMetacardType.CSW_RIGHTS, new String[] {"copyright 1", "copyright 2"});
    expectedValues.put(CswRecordMetacardType.CSW_LANGUAGE, new String[] {"english"});
    expectedValues.put(CswRecordMetacardType.CSW_TYPE, "dataset");
    expectedValues.put(CswRecordMetacardType.CSW_FORMAT, new String[] {"Shapefile"});
    expectedValues.put(
        Metacard.GEOGRAPHY,
        "POLYGON((52.139 5.121, 52.517 5.121, 52.517 4.468, 52.139 4.468, 52.139 5.121))");
    expectedValues.put(
        CswRecordMetacardType.OWS_BOUNDING_BOX,
        new String[] {
          "POLYGON((52.139 5.121, 52.517 5.121, 52.517 4.468, 52.139 4.468, 52.139 5.121))"
        });
    assertMetacard(mc, expectedValues);

    expectedValues.clear();

    // verify second metacard's values
    mc = metacards.get(1);
    assertThat(mc, not(nullValue()));
    expectedValues = new HashMap<String, Object>();
    expectedValues.put(Metacard.ID, "{23362852-F370-4369-B0B2-BE74B2859614}");
    expectedValues.put(
        CswRecordMetacardType.CSW_IDENTIFIER,
        new String[] {"{23362852-F370-4369-B0B2-BE74B2859614}"});
    expectedValues.put(Metacard.TITLE, "mc2 title");
    expectedValues.put(CswRecordMetacardType.CSW_TITLE, new String[] {"mc2 title"});
    expectedModifiedDateStr = "2010-12-15";
    dateFormatter = ISODateTimeFormat.dateOptionalTimeParser();
    expectedModifiedDate = dateFormatter.parseDateTime(expectedModifiedDateStr).toDate();
    expectedValues.put(CswRecordMetacardType.CSW_MODIFIED, new String[] {expectedModifiedDateStr});
    expectedValues.put(Metacard.MODIFIED, expectedModifiedDate);
    expectedValues.put(
        CswRecordMetacardType.CSW_SUBJECT, new String[] {"first subject", "subject 2"});
    expectedValues.put(CswRecordMetacardType.CSW_ABSTRACT, new String[] {"mc2 abstract"});
    expectedValues.put(
        CswRecordMetacardType.CSW_RIGHTS, new String[] {"first copyright", "second copyright"});
    expectedValues.put(CswRecordMetacardType.CSW_LANGUAGE, new String[] {"english"});
    expectedValues.put(CswRecordMetacardType.CSW_TYPE, "dataset 2");
    expectedValues.put(CswRecordMetacardType.CSW_FORMAT, new String[] {"Shapefile 2"});
    expectedValues.put(
        Metacard.GEOGRAPHY,
        "POLYGON((53.139 6.121, 53.517 6.121, 53.517 5.468, 53.139 5.468, 53.139 6.121))");
    expectedValues.put(
        CswRecordMetacardType.OWS_BOUNDING_BOX,
        new String[] {
          "POLYGON((53.139 6.121, 53.517 6.121, 53.517 5.468, 53.139 5.468, 53.139 6.121))"
        });
    assertMetacard(mc, expectedValues);

    expectedValues.clear();
  }