-
Couldn't load subscription status.
- Fork 74
Closed
Description
Here is my java script extension code
// module that exports get, post, put and delete
function get(context, params) {
context.outputTypes = ["application/json"];
var arg1 = params.arg1;
var arg2 = params.arg2;
return {
"argument1": arg1,
"argument2": arg2,
"content": "This is a JSON document",
"response": xdmp.getResponseCode(),
"outputTypes": context.outputTypes,
}
};
function post(context, params, input) {
var argUrl = params.uri;
var inputObject = input;
xdmp.eval("declareUpdate(); var argUrl; var input;xdmp.documentInsert(argUrl,input)",({"argUrl":argUrl,"input":inputObject}));
var count = xdmp.eval("fn.count(fn.doc())");
return ({"docCount":count,"response": xdmp.getResponseCode()})
};
// Function responding to PUT method - must use local name 'put'.
function put(context, params, input) {
var argUrl = params.uri;
return {"argument1": context.acceptTypes}
};
// Function responding to DELETE method - must use local name 'delete'.
function deleteFunction(context, params) {
return(xdmp.log("delete!"))
};
exports.GET = get;
exports.POST = post;
exports.PUT = put;
exports.DELETE = deleteFunction;
my test code
package com.marklogic.javaclient;
import static org.junit.Assert.*;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import javax.swing.text.html.FormSubmitEvent;
import org.json.JSONException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.skyscreamer.jsonassert.JSONAssert;
import com.marklogic.client.DatabaseClient;
import com.marklogic.client.DatabaseClientFactory;
import com.marklogic.client.DatabaseClientFactory.Authentication;
import com.marklogic.client.admin.ExtensionMetadata;
import com.marklogic.client.admin.ExtensionMetadata.ScriptLanguage;
import com.marklogic.client.admin.MethodType;
import com.marklogic.client.admin.ResourceExtensionsManager;
import com.marklogic.client.admin.ResourceExtensionsManager.MethodParameters;
import com.marklogic.client.document.JSONDocumentManager;
import com.marklogic.client.document.XMLDocumentManager;
import com.marklogic.client.extensions.ResourceManager;
import com.marklogic.client.extensions.ResourceServices.ServiceResult;
import com.marklogic.client.extensions.ResourceServices.ServiceResultIterator;
import com.marklogic.client.io.Format;
import com.marklogic.client.io.InputStreamHandle;
import com.marklogic.client.io.StringHandle;
import com.marklogic.client.util.RequestParameters;
public class TestJSResourceExtensions extends BasicJavaClientREST {
private static final int BATCH_SIZE=100;
private static final String DIRECTORY ="/bulkResource/";
private static String dbName = "TestJSResourceExtensionDB";
private static String [] fNames = {"TestResourceExtensionDB-1"};
private static String restServerName = "REST-Java-Client-API-Server";
private static int restPort = 8011;
private DatabaseClient client ;
ResourceExtensionsManager resourceMgr;
static public class TestJSExtension extends ResourceManager {
static final public String NAME = "simpleJSResourceModule";
static final public ExtensionMetadata.ScriptLanguage scriptLanguage = ExtensionMetadata.JAVASCRIPT;
private JSONDocumentManager docMgr;
public TestJSExtension(DatabaseClient client) {
super();
// a Resource Manager must be initialized by a Database Client
client.init(NAME, this, ExtensionMetadata.JAVASCRIPT);
// delegates some services to a document manager
docMgr = client.newJSONDocumentManager();
}
public String getJSON() {
RequestParameters params = new RequestParameters();
params.add("arg1", "hello");
params.add("arg2", "Earth");
// specify the mime type for each expected document returned
String[] mimetypes = new String[] {"text/plain"};
// call the service
ServiceResultIterator resultItr = getServices().get(params, mimetypes);
// iterate over the results
List<String> responses = new ArrayList<String>();
StringHandle readHandle = new StringHandle();
while (resultItr.hasNext()) {
ServiceResult result = resultItr.next();
// get the result content
result.getContent(readHandle);
responses.add(readHandle.get());
}
// release the iterator resources
resultItr.close();
return responses.get(0);
}
public String postJSON() {
RequestParameters params = new RequestParameters();
params.add("uri", "helloJs.json");
// specify the mime type for each expected document returned
String[] mimetypes = new String[] {"text/plain"};
StringHandle output = new StringHandle();
String input = "{\"argument1\":\"hello\", \"argument2\":\"Earth\", \"content\":\"This is a JSON document\", \"response\":[200, \"OK\"], \"outputTypes\":\"application/json\"}";
// call the service
ServiceResultIterator resultItr= getServices().post(params, new StringHandle(input).withFormat(Format.JSON), mimetypes);
// iterate over the results
List<String> responses = new ArrayList<String>();
StringHandle readHandle = new StringHandle();
while (resultItr.hasNext()) {
ServiceResult result = resultItr.next();
// get the result content
result.getContent(readHandle);
responses.add(readHandle.get());
}
// release the iterator resources
resultItr.close();
return responses.get(0);
}
}
@BeforeClass
public static void setUpBeforeClass() throws Exception {
System.out.println("In setup");
setupJavaRESTServer(dbName, fNames[0], restServerName,restPort);
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug");
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
System.out.println("In tear down" );
tearDownJavaRESTServer(dbName, fNames, restServerName);
}
@Before
public void setUp() throws Exception {
client = DatabaseClientFactory.newClient("localhost", restPort, "rest-admin", "x", Authentication.DIGEST);
resourceMgr = client.newServerConfigManager().newResourceExtensionsManager();
ExtensionMetadata resextMetadata = new ExtensionMetadata();
resextMetadata.setTitle("BasicJSTest");
resextMetadata.setDescription("Testing resource extension for java script");
System.out.println(resextMetadata.getScriptLanguage());
resextMetadata.setScriptLanguage(ScriptLanguage.JAVASCRIPT);
System.out.println(resextMetadata.getScriptLanguage());
resextMetadata.setVersion("1.0");
MethodParameters getParams = new MethodParameters(MethodType.GET);
getParams.add("my-uri", "xs:string?");
FileInputStream myStream = new FileInputStream("src/test/java/com/marklogic/javaclient/data/JSResource.js");
InputStreamHandle handle = new InputStreamHandle(myStream);
handle.set (myStream);
resourceMgr.writeServices("simpleJSResourceModule", handle, resextMetadata,getParams);
}
@After
public void tearDown() throws Exception {
client.release();
resourceMgr.deleteServices("simpleJSResourceModule");
}
@Test
public void testResourceGetService() throws Exception {
System.out.println(resourceMgr.listServices(new StringHandle().withFormat(Format.JSON)).get());
TestJSExtension tjs= new TestJSExtension(client);
String expected ="{\"argument1\":\"hello\", \"argument2\":\"Earth\", \"content\":\"This is a JSON document\", \"response\":[200, \"OK\"], \"outputTypes\":\"application/json\"}";
JSONAssert.assertEquals(expected, tjs.getJSON(), false);
System.out.println(tjs.postJSON());
}
}