altIDs = altIDList.toArray(new String[altIDList.size()]);
}
Context context = getContext();
Datastream existingDS =
m_management.getDatastream(context, pid, dsID, null);
if (!posted && versionable == null && existingDS != null){
versionable = existingDS.DSVersionable;
}
Date requestModDate = null;
if (lastModifiedDate != null) {
requestModDate = lastModifiedDate.getValue();
}
// If a datastream is set to Deleted state, it must be set to
// another state before any other changes can be made
if (existingDS != null && existingDS.DSState.equals("D")
&& dsState != null) {
if (dsState.equals("A") || dsState.equals("I")) {
m_management.setDatastreamState(context,
pid,
dsID,
dsState,
logMessage);
existingDS.DSState = dsState;
}
}
InputStream is = null;
// Determine if datastream content is included in the request
if (!ignoreContent) {
RequestContent content =
RestUtil.getRequestContent(m_servletRequest, m_headers);
if (content != null && content.getContentStream() != null) {
is = content.getContentStream();
// Give preference to the passed in mimeType
if (mimeType == null && content.getMimeType() != null) {
mimeType = content.getMimeType();
}
}
} else {
LOGGER.warn("ignoring content on {}/{}", pid, dsID);
}
// Make sure that there is a mime type value
if (mimeType == null && mediaType != null) {
mimeType = mediaType.toString();
} else if (mimeType == null && mediaType == null
&& existingDS != null) {
mimeType = existingDS.DSMIME;
}
// set default control group based on mimeType
if (dsLocation == null
&& TEXT_XML.isCompatible(MediaType.valueOf(mimeType))
&& controlGroup == null) {
controlGroup = "X";
}
if (existingDS == null) {
if (posted) {
LOGGER.debug("new ds posted at {}/{}", pid, dsID);
if ((dsLocation == null || dsLocation.equals(""))
&& ("X".equals(controlGroup) || "M"
.equals(controlGroup))) {
if (is == null) {
LOGGER.warn("No content stream to copy for {}/{}",
pid, dsID);
return Response.status(Response.Status.BAD_REQUEST)
.build();
}
dsLocation = m_management.putTempStream(context, is);
}
dsID =
m_management.addDatastream(context,
pid,
dsID,
altIDs,
dsLabel,
versionable,
mimeType,
formatURI,
dsLocation,
controlGroup,
dsState,
checksumType,
checksum,
logMessage);
} else {
LOGGER.warn("new ds but no posted content at {}/{}", pid, dsID);
return Response.status(Response.Status.NOT_FOUND).build();
}
} else {
if ("X".equals(existingDS.DSControlGrp)) {
// Inline XML can only be modified by value. If there is no stream,
// but there is a dsLocation attempt to retrieve the content.
if (is == null && dsLocation != null
&& !dsLocation.equals("")) {
try {
WebClientConfiguration webconfig = m_server.getWebClientConfig();
WebClient webClient = new WebClient(webconfig);
is = webClient.get(dsLocation, true);
} catch (IOException ioe) {
throw new Exception("Could not retrive content from "
+ dsLocation
+ " due to error: "
+ ioe.getMessage());
}
}
m_management.modifyDatastreamByValue(context,
pid,
dsID,
altIDs,
dsLabel,
mimeType,
formatURI,
is,
checksumType,
checksum,
logMessage,
requestModDate);
} else {
// Managed content can only be modified by reference.
// If there is no dsLocation, but there is a content stream,
// store the stream in a temporary location.
if (dsLocation == null
&& ("M".equals(existingDS.DSControlGrp))) {
if (is != null) {
dsLocation = m_management.putTempStream(context, is);
} else {
dsLocation = null;
}
}
m_management.modifyDatastreamByReference(context,
pid,
dsID,
altIDs,
dsLabel,
mimeType,
formatURI,
dsLocation,
checksumType,
checksum,
logMessage,
requestModDate);
}
if (dsState != null) {
if (dsState.equals("A") || dsState.equals("D")
|| dsState.equals("I")) {
if (!dsState.equals(existingDS.DSState)) {
m_management.setDatastreamState(context,
pid,
dsID,
dsState,
logMessage);
}
}
}
if (versionable != existingDS.DSVersionable) {
m_management.setDatastreamVersionable(context,
pid,
dsID,
versionable,
logMessage);
}
}
ResponseBuilder builder;
if (posted) {
builder =
Response.created(m_uriInfo.getRequestUri()
.resolve(URLEncoder.encode(dsID, DEFAULT_ENC)));
} else { // put
builder = Response.ok();
}
builder.header("Content-Type", MediaType.TEXT_XML);
Datastream dsProfile =
m_management.getDatastream(context, pid, dsID, null);
String xml =
getSerializer(context).datastreamProfileToXML(pid,
dsID,
dsProfile,