/*
(non-Javadoc)
@see org.fcrepo.server.management.FedoraAPIMMTOM#getDatastream( String
pid ,)String dsID ,)String asOfDateTime )*
341836
@Override
    public org.fcrepo.server.types.gen.Datastream getDatastream(String pid,
                                                                String dsID,
                                                                String asOfDateTime) {
        assertInitialized();
        try {
            MessageContext ctx = context.getMessageContext();
            org.fcrepo.server.storage.types.Datastream ds =
                    m_management.getDatastream(ReadOnlyContext
                            .getSoapContext(ctx), pid, dsID, DateUtility
                            .parseDateOrNull(asOfDateTime));
            return TypeUtility.convertDatastreamToGenDatastream(ds);
        } catch (Throwable th) {
            LOG.error("Error getting datastream", th);
            throw CXFUtility.getFault(th);
        }
    }
1067397
@Override
    public List<String> purgeDatastream(String pid,
                                        String dsID,
                                        String startDT,
                                        String endDT,
                                        String logMessage,
                                        boolean force) {
        LOG.debug("start: purgeDatastream, {}, {}", pid, dsID);
        assertInitialized();
        try {
            MessageContext ctx = context.getMessageContext();
            return toStringList(m_management.purgeDatastream(ReadOnlyContext
                    .getSoapContext(ctx), pid, dsID, DateUtility
                    .parseDateOrNull(startDT), DateUtility
                    .parseDateOrNull(endDT), logMessage));
        } catch (Throwable th) {
            LOG.error("Error purging datastream", th);
            throw CXFUtility.getFault(th);
        } finally {
            LOG.debug("end: purgeDatastream, {}, {}", pid, dsID);
        }
    }
949108
protected void refresh(String dsID) {
        int i = getTabIndex(dsID);
        try {
            List<Datastream> versions =
                    Administrator.APIM.getDatastreamHistory(m_pid, dsID);
            m_currentVersionMap.put(dsID, versions.get(i));
            logger.debug("New create date is: " + versions.get(i).getCreateDate());
            DatastreamPane replacement =
                    new DatastreamPane(m_owner, m_pid, versions, this);
            m_datastreamPanes[i] = replacement;
            m_tabbedPane.setComponentAt(i, replacement);
            m_tabbedPane.setToolTipTextAt(i, versions.get(i).getMIMEType() + " - "
                    + versions.get(i).getLabel() + " ("
                    + versions.get(i).getControlGroup().toString() + ")");
            colorTabForState(dsID, versions.get(i).getState());
            setDirty(dsID, false);
        } catch (Exception e) {
            Administrator
                    .showErrorDialog(Administrator.getDesktop(),
                                     "Error while refreshing",
                                     e.getMessage()
                                             + "\nTry re-opening the object viewer.",
                                     e);
        }
    }
138036
private File getTempFile(DigitalObject object, String pid)
            throws Exception {
        // Create file in temp space, use OID in path for uniqueness
        File directory = new File(tmpDir, object.getId());
        File target = new File(directory, pid);
        if (!target.exists()) {
            target.getParentFile().mkdirs();
            target.createNewFile();
        }

        // These can happily throw exceptions higher
        Payload payload = object.getPayload(pid);
        InputStream in = payload.open();
        FileOutputStream out = null;

        // But here, the payload must receive
        // a close before throwing the error
        try {
            out = new FileOutputStream(target);
            IOUtils.copyLarge(in, out);

        } catch (Exception ex) {
            close(out);
            target.delete();
            payload.close();
            throw ex;
        }

        // We close out here, because the catch statement needed to close
        // before it could delete... so it can't be in 'finally'
        close(out);
        payload.close();

        return target;
    }
1325673
private DataSource getDataSource(ComponentHelper cmp, Resource resource) {
    try {
      /*
       * by default the path is read from request "path" parameter
       * here we overwrite it via a synthetic resource because the path may be overwritten by validation logic
       * to ensure the path is not beyond the configured root path
       */
      ValueMap overwriteProperties = new ValueMapDecorator(ImmutableMap.<String, Object>of("path", resource.getPath()));
      Resource dataSourceResourceWrapper = GraniteUiSyntheticResource.wrapMerge(request.getResource(), overwriteProperties);
      return cmp.getItemDataSource(dataSourceResourceWrapper);
    }
    catch (ServletException | IOException ex) {
      throw new RuntimeException("Unable to get data source.", ex);
    }
  }
702190
def setDatastreamState(self, pid, dsID, dsState):
        '''
        '''
        # /objects/{pid}/datastreams/{dsID} ? [dsState]
        http_args = {'dsState' : dsState}
        url = 'objects/%(pid)s/datastreams/%(dsid)s' % {'pid': pid, 'dsid': dsID}
        response = self.put(url, params=http_args)
        # returns response code 200 on success
        return response.status_code == requests.codes.ok
1022036
private File getFile(String pid, String dsId, String dsVersionId) {
        File dir = new File(baseDir, getPath(pid));
        String filename = encode(pid, dsId, dsVersionId);
        return new File(dir, filename);
    }
772378
@Override
    public ContentModelInfo getContentModelInfo(String pid)
            throws ObjectSourceException, InvalidContentModelException {
        try {
            ObjectInfo object = getValidationObject(pid);
            if (object == null) {
                return null;
            }

            DatastreamInfo dsInfo =
                    object.getDatastreamInfo(DS_COMPOSITE_MODEL);
            if (dsInfo == null) {
                throw new InvalidContentModelException(pid,
                                                       "Content model has no '"
                                                               + DS_COMPOSITE_MODEL
                                                               + "' datastream.");
            }

            if (!DS_COMPOSITE_MODEL_FORMAT.equals(dsInfo.getFormatUri())) {
                throw new InvalidContentModelException(pid, "Datastream '"
                        + DS_COMPOSITE_MODEL + "' has incorrect format URI: '"
                        + dsInfo.getFormatUri() + "'.");
            }

            MIMETypedStream ds =
                    apia.getDatastreamDissemination(pid,
                                                    DS_COMPOSITE_MODEL,
                                                    null);
            DsCompositeModelDoc model =
                    new DsCompositeModelDoc(pid,
                                            org.fcrepo.server.utilities.TypeUtility
                                                    .convertDataHandlerToBytes(ds
                                                            .getStream()));
            return new BasicContentModelInfo(object, model.getTypeModels());
        } catch (Exception e) {
            throw new ObjectSourceException("Problem fetching '"
                                                    + DS_COMPOSITE_MODEL
                                                    + "' datastream for pid='"
                                                    + pid + "'",
                                            e);
        }
    }
842025
public Date setDatastreamVersionable(Context context,
                                         String pid,
                                         String dsID,
                                         boolean versionable,
                                         String logMessage)
            throws ServerException {
        try {
            CreatorJournalEntry cje =
                    new CreatorJournalEntry(METHOD_SET_DATASTREAM_VERSIONABLE,
                                            context);
            cje.addArgument(ARGUMENT_NAME_PID, pid);
            cje.addArgument(ARGUMENT_NAME_DS_ID, dsID);
            cje.addArgument(ARGUMENT_NAME_VERSIONABLE, versionable);
            cje.addArgument(ARGUMENT_NAME_LOG_MESSAGE, logMessage);
            return (Date) cje.invokeAndClose(delegate, writer);
        } catch (JournalException e) {
            throw new GeneralException("Problem creating the Journal", e);
        }
    }
85718
public Datastream[] getDatastreams(Context context,
                                       String pid,
                                       Date asOfDateTime,
                                       String dsState) throws ServerException {
        return delegate.getDatastreams(context, pid, asOfDateTime, dsState);
    }
48454
@Override
    public String compareDatastreamChecksum(String pid,
                                            String dsID,
                                            String versionDate) {
        assertInitialized();
        try {
            MessageContext ctx = context.getMessageContext();
            return m_management.compareDatastreamChecksum(ReadOnlyContext
                    .getSoapContext(ctx), pid, dsID, DateUtility
                    .parseDateOrNull(versionDate));
        } catch (Throwable th) {
            LOG.error("Error comparing datastream checksum", th);
            throw CXFUtility.getFault(th);
        }
    }
279411
public Date setDatastreamState(Context context,
                                   String pid,
                                   String dsID,
                                   String dsState,
                                   String logMessage) throws ServerException {
        return worker.setDatastreamState(context,
                                         pid,
                                         dsID,
                                         dsState,
                                         logMessage);
    }
964408
public Date[] purgeDatastream(Context context,
                                  String pid,
                                  String datastreamID,
                                  Date startDT,
                                  Date endDT,
                                  String logMessage) throws ServerException {
        return worker.purgeDatastream(context,
                                      pid,
                                      datastreamID,
                                      startDT,
                                      endDT,
                                      logMessage);
    }
209118
public Date modifyDatastreamByValue(Context context,
                                        String pid,
                                        String datastreamID,
                                        String[] altIDs,
                                        String dsLabel,
                                        String mimeType,
                                        String formatURI,
                                        InputStream dsContent,
                                        String checksumType,
                                        String checksum,
                                        String logMessage,
                                        Date lastModifiedDate) throws ServerException {
        try {
            CreatorJournalEntry cje =
                    new CreatorJournalEntry(METHOD_MODIFY_DATASTREAM_BY_VALUE,
                                            context);
            cje.addArgument(ARGUMENT_NAME_PID, pid);
            cje.addArgument(ARGUMENT_NAME_DS_ID, datastreamID);
            cje.addArgument(ARGUMENT_NAME_ALT_IDS, altIDs);
            cje.addArgument(ARGUMENT_NAME_DS_LABEL, dsLabel);
            cje.addArgument(ARGUMENT_NAME_MIME_TYPE, mimeType);
            cje.addArgument(ARGUMENT_NAME_FORMAT_URI, formatURI);
            cje.addArgument(ARGUMENT_NAME_DS_CONTENT, dsContent);
            cje.addArgument(ARGUMENT_NAME_CHECKSUM_TYPE, checksumType);
            cje.addArgument(ARGUMENT_NAME_CHECKSUM, checksum);
            cje.addArgument(ARGUMENT_NAME_LOG_MESSAGE, logMessage);
            cje.addArgument(ARGUMENT_NAME_LAST_MODIFIED_DATE, lastModifiedDate);
            return (Date) cje.invokeAndClose(delegate, writer);
        } catch (JournalException e) {
            throw new GeneralException("Problem creating the Journal", e);
        }
    }
181745
@Override
    public List<org.fcrepo.server.types.gen.Datastream> getDatastreams(String pid,
                                                                       String asOfDateTime,
                                                                       String dsState) {
        assertInitialized();
        try {
            MessageContext ctx = context.getMessageContext();
            org.fcrepo.server.storage.types.Datastream[] intDatastreams =
                    m_management.getDatastreams(ReadOnlyContext
                            .getSoapContext(ctx), pid, DateUtility
                            .parseDateOrNull(asOfDateTime), dsState);
            return getGenDatastreams(intDatastreams);
        } catch (Throwable th) {
            LOG.error("Error getting datastreams", th);
            throw CXFUtility.getFault(th);
        }
    }
1026083
@Path("/{dsID}/content")
    @GET
    public Response getDatastream(@PathParam(RestParam.PID) String pid,
                                  @PathParam(RestParam.DSID) String dsID,
                                  @QueryParam(RestParam.AS_OF_DATE_TIME) String dateTime,
                                  @QueryParam(RestParam.DOWNLOAD) String download,
                                  @QueryParam(RestParam.FLASH) @DefaultValue("false") boolean flash) {
        Context context = getContext();
        try {
            Date asOfDateTime = DateUtility.parseDateOrNull(dateTime);
            MIMETypedStream stream =
                    m_access.getDatastreamDissemination(context,
                                                           pid,
                                                           dsID,
                                                           asOfDateTime);
            if (m_datastreamFilenameHelper != null) {
                m_datastreamFilenameHelper
                        .addContentDispositionHeader(context,
                                                     pid,
                                                     dsID,
                                                     download,
                                                     asOfDateTime,
                                                     stream);

            }

            return buildResponse(stream);
        } catch (Exception ex) {
            return handleException(ex, flash);
        }
    }
658043
public Date modifyDatastreamByReference(Context context,
                                            String pid,
                                            String datastreamID,
                                            String[] altIDs,
                                            String dsLabel,
                                            String mimeType,
                                            String formatURI,
                                            String dsLocation,
                                            String checksumType,
                                            String checksum,
                                            String logMessage,
                                            Date lastModifiedDate)
            throws ServerException {
        try {
            CreatorJournalEntry cje =
                    new CreatorJournalEntry(METHOD_MODIFY_DATASTREAM_BY_REFERENCE,
                                            context);
            cje.addArgument(ARGUMENT_NAME_PID, pid);
            cje.addArgument(ARGUMENT_NAME_DS_ID, datastreamID);
            cje.addArgument(ARGUMENT_NAME_ALT_IDS, altIDs);
            cje.addArgument(ARGUMENT_NAME_DS_LABEL, dsLabel);
            cje.addArgument(ARGUMENT_NAME_MIME_TYPE, mimeType);
            cje.addArgument(ARGUMENT_NAME_FORMAT_URI, formatURI);
            cje.addArgument(ARGUMENT_NAME_DS_LOCATION, dsLocation);
            cje.addArgument(ARGUMENT_NAME_CHECKSUM_TYPE, checksumType);
            cje.addArgument(ARGUMENT_NAME_CHECKSUM, checksum);
            cje.addArgument(ARGUMENT_NAME_LOG_MESSAGE, logMessage);
            cje.addArgument(ARGUMENT_NAME_LAST_MODIFIED_DATE, lastModifiedDate);
            return (Date) cje.invokeAndClose(delegate, writer);
        } catch (JournalException e) {
            throw new GeneralException("Problem creating the Journal", e);
        }
    }