Skip to content

Commit

Permalink
support batch and batch capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
ruan-wei committed Jun 22, 2016
1 parent 8419ef4 commit 8111bfe
Show file tree
Hide file tree
Showing 46 changed files with 2,780 additions and 87 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;

import com.emc.documentum.rest.client.sample.client.annotation.NotBatchable;
import com.emc.documentum.rest.client.sample.model.Feed;
import com.emc.documentum.rest.client.sample.model.FolderLink;
import com.emc.documentum.rest.client.sample.model.HomeDocument;
Expand All @@ -21,6 +22,8 @@
import com.emc.documentum.rest.client.sample.model.SearchFeed;
import com.emc.documentum.rest.client.sample.model.ValueAssistant;
import com.emc.documentum.rest.client.sample.model.ValueAssistantRequest;
import com.emc.documentum.rest.client.sample.model.batch.Batch;
import com.emc.documentum.rest.client.sample.model.batch.Capabilities;

/**
* The sample REST client library
Expand All @@ -31,11 +34,13 @@ public interface DCTMRestClient {
/**
* @return the http headers of the previous operation
*/
@NotBatchable
public HttpHeaders getHeaders();

/**
* @return the http status of the previous operation
*/
@NotBatchable
public HttpStatus getStatus();

/**
Expand All @@ -44,26 +49,31 @@ public interface DCTMRestClient {
* only valid for the next operations.
* will be automatically disabled after the operation.
*/
public void enableStreamingForNextRequest();
@NotBatchable
public DCTMRestClient enableStreamingForNextRequest();

/**
* @return the cached HomeDocument object
*/
@NotBatchable
public HomeDocument getHomeDocument();

/**
* @return the product info
*/
@NotBatchable
public RestObject getProductInfo();

/**
* @return the cached Repositories feed
*/
@NotBatchable
public Feed<Repository> getRepositories();

/**
* @return the cached Repository object
*/
@NotBatchable
public Repository getRepository();

/**
Expand Down Expand Up @@ -93,6 +103,7 @@ public interface DCTMRestClient {
* @param params
* @return the cabinet by its name
*/
@NotBatchable
public RestObject getCabinet(String cabinet, String... params);

/**
Expand Down Expand Up @@ -153,20 +164,39 @@ public interface DCTMRestClient {
* @param rel the LinkRelation used to create new object under the parent
* @param objectToCreate the new object with its properties
* @param content the binary content, it can be byte array, String, javax.xml.transform.Source, org.springframework.core.io.Resource, JAXB object, and Jackson json object
* @param contentMediaType the mediatype of the content
* @param params
* @return the created sysobject
*/
public RestObject createObject(RestObject parent, LinkRelation rel, RestObject objectToCreate, Object content, String... params);
public RestObject createObject(RestObject parent, LinkRelation rel, RestObject objectToCreate, Object content, String contentMediaType, String... params);

/**
* create a sysobject (or its subtype) under specified parent's link rel
* @param parent the parent object where the new sysobject will be created under
* @param rel the LinkRelation used to create new object under the parent
* @param objectToCreate the new object with its properties
* @return the created sysobject
*/
public RestObject createObject(RestObject parent, LinkRelation rel, RestObject objectToCreate);

/**
* create a sysobject (or its subtype) under specified folder/cabinet
* @param parent the folder/cabinet where the new sysobject will be created under
* @param objectToCreate the new object with its properties
* @param content the binary content, it can be byte array, String, javax.xml.transform.Source, org.springframework.core.io.Resource, JAXB object, and Jackson json object
* @param contentMediaType the mediatype of the content
* @param params
* @return the created sysobject
*/
public RestObject createObject(RestObject parent, RestObject objectToCreate, Object content, String... params);
public RestObject createObject(RestObject parent, RestObject objectToCreate, Object content, String contentMediaType, String... params);

/**
* create a sysobject (or its subtype) under specified folder/cabinet
* @param parent the folder/cabinet where the new sysobject will be created under
* @param objectToCreate the new object with its properties
* @return the created sysobject
*/
public RestObject createObject(RestObject parent, RestObject objectToCreate);

/**
* @param objectUri
Expand All @@ -180,10 +210,19 @@ public interface DCTMRestClient {
* @param parent the folder/cabinet where the new document will be created under
* @param objectToCreate the new document with its properties
* @param content the binary content, it can be byte array, String, javax.xml.transform.Source, org.springframework.core.io.Resource, JAXB object, and Jackson json object
* @param contentMediaType the mediatype of the content
* @param params
* @return the created document
*/
public RestObject createDocument(RestObject parent, RestObject objectToCreate, Object content, String... params);
public RestObject createDocument(RestObject parent, RestObject objectToCreate, Object content, String contentMediaType, String... params);

/**
* create a document (or its subtype) under specified folder/cabinet
* @param parent the folder/cabinet where the new document will be created under
* @param objectToCreate the new document with its properties
* @return the created document
*/
public RestObject createDocument(RestObject parent, RestObject objectToCreate);

/**
* @param documentUri
Expand Down Expand Up @@ -369,10 +408,11 @@ public interface DCTMRestClient {
* @param oldObject the checked out RestObject
* @param newObject the new metadata to be checked in
* @param content the binary content
* @param contentMediaType the mediatype of the content
* @param params
* @return checked in object
*/
public RestObject checkinNextMajor(RestObject oldObject, RestObject newObject, Object content, String... params);
public RestObject checkinNextMajor(RestObject oldObject, RestObject newObject, Object content, String contentMediaType, String... params);

/**
* check in the object with next minor version
Expand All @@ -381,10 +421,11 @@ public interface DCTMRestClient {
* @param oldObject the checked out RestObject
* @param newObject the new metadata to be checked in
* @param content the binary content
* @param contentMediaType the mediatype of the content
* @param params
* @return checked in object
*/
public RestObject checkinNextMinor(RestObject oldObject, RestObject newObject, Object content, String... params);
public RestObject checkinNextMinor(RestObject oldObject, RestObject newObject, Object content, String contentMediaType, String... params);

/**
* check in the object with branch version
Expand All @@ -393,10 +434,11 @@ public interface DCTMRestClient {
* @param oldObject the checked out RestObject
* @param newObject the new metadata to be checked in
* @param content the binary content
* @param contentMediaType the mediatype of the content
* @param params
* @return checked in object
*/
public RestObject checkinBranch(RestObject oldObject, RestObject newObject, Object content, String... params);
public RestObject checkinBranch(RestObject oldObject, RestObject newObject, Object content, String contentMediaType, String... params);

/**
* materialize the lightweight object
Expand Down Expand Up @@ -571,4 +613,18 @@ public interface DCTMRestClient {
* @return
*/
public FolderLink link(RestObject object, LinkRelation rel, FolderLink link);

/**
* get batch capabilities
* @return
*/
public Capabilities getBatchCapabilities();

/**
* create and execute a batch
* @param batch
* @return
*/
@NotBatchable
public Batch createBatch(Batch batch);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.emc.documentum.rest.client.sample.client.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface NotBatchable {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright (c) 2016. EMC Corporation. All Rights Reserved.
*/

package com.emc.documentum.rest.client.sample.client.converter;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.util.MimeTypeUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StreamUtils;

import com.emc.documentum.rest.client.sample.client.util.SupportedMediaTypes;

public class MultipartBatchHttpMessageConverter implements HttpMessageConverter<MultiValueMap<String, HttpEntity<InputStream>>> {

@Override
public boolean canRead(Class<?> clazz, MediaType mediaType) {
return false;
}

@Override
public boolean canWrite(Class<?> clazz, MediaType mediaType) {
return "multipart".equalsIgnoreCase(mediaType.getType()) &&
"related".equalsIgnoreCase(mediaType.getSubtype()) &&
MultiValueMap.class.isAssignableFrom(clazz);
}

@Override
public List<MediaType> getSupportedMediaTypes() {
return Arrays.asList(SupportedMediaTypes.MULTIPART_RELATED);
}

@Override
public MultiValueMap<String, HttpEntity<InputStream>> read(Class<? extends MultiValueMap<String, HttpEntity<InputStream>>> clazz, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException {
throw new UnsupportedOperationException();
}

@Override
public void write(MultiValueMap<String, HttpEntity<InputStream>> parts, MediaType contentType, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
String boundary = MimeTypeUtils.generateMultipartBoundaryString();
Map<String, String> params = new HashMap<>(contentType.getParameters());
params.put("boundary", boundary);
MediaType mediaType = new MediaType(contentType, params);
outputMessage.getHeaders().setContentType(mediaType);
writeParts(outputMessage.getBody(), parts, boundary);
}

private void writeParts(OutputStream os, MultiValueMap<String, HttpEntity<InputStream>> parts, String boundary) throws IOException {
for (Map.Entry<String, List<HttpEntity<InputStream>>> entry : parts.entrySet()) {
String name = entry.getKey();
for (HttpEntity<InputStream> part : entry.getValue()) {
if (part != null) {
writeBoundary(os, boundary);
writePart(name, part, os);
writeNewLine(os);
}
}
}
writeEnd(os, boundary);
}

private void writePart(String name, HttpEntity<InputStream> part, OutputStream os) throws IOException {
writeHeaders(os, part.getHeaders());
StreamUtils.copy(part.getBody(), os);
writeNewLine(os);
}

private void writeHeaders(OutputStream os, HttpHeaders headers) throws IOException {
for(Map.Entry<String, List<String>> entry : headers.entrySet()) {
for(String value : entry.getValue()) {
os.write(entry.getKey().getBytes("UTF-8"));
os.write(':');
os.write(' ');
os.write(value.getBytes("UTF-8"));
writeNewLine(os);
}
}
writeNewLine(os);
}

private void writeBoundary(OutputStream os, String boundary) throws IOException {
os.write(("--" + boundary).getBytes("UTF-8"));
writeNewLine(os);
}

private void writeEnd(OutputStream os, String boundary) throws IOException {
os.write(("--" + boundary + "--").getBytes("UTF-8"));
writeNewLine(os);
}

private static void writeNewLine(OutputStream os) throws IOException {
os.write('\r');
os.write('\n');
}

private static void writeNewLine2(OutputStream os) throws IOException {
writeNewLine(os);
writeNewLine(os);
}
}
Loading

0 comments on commit 8111bfe

Please sign in to comment.