-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathJavascriptResourceExtension.java
More file actions
158 lines (127 loc) · 5.56 KB
/
JavascriptResourceExtension.java
File metadata and controls
158 lines (127 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* Copyright (c) 2010-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
*/
package com.marklogic.client.example.cookbook;
import com.marklogic.client.DatabaseClient;
import com.marklogic.client.FailedRequestException;
import com.marklogic.client.ForbiddenUserException;
import com.marklogic.client.ResourceNotFoundException;
import com.marklogic.client.admin.ExtensionMetadata;
import com.marklogic.client.admin.MethodType;
import com.marklogic.client.admin.ResourceExtensionsManager;
import com.marklogic.client.admin.ResourceExtensionsManager.MethodParameters;
import com.marklogic.client.document.XMLDocumentManager;
import com.marklogic.client.example.cookbook.Util.ExampleProperties;
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.StringHandle;
import com.marklogic.client.util.RequestParameters;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/**
* JavascriptResourceExtension installs an extension for managing spelling dictionary resources.
*/
public class JavascriptResourceExtension {
public static void main(String[] args)
throws IOException, ResourceNotFoundException, ForbiddenUserException, FailedRequestException
{
run(Util.loadProperties());
}
// install and then use the resource extension
public static void run(ExampleProperties props)
throws IOException, ResourceNotFoundException, ForbiddenUserException, FailedRequestException
{
System.out.println("example: "+JavascriptResourceExtension.class.getName());
installResourceExtension(props);
useResource(props);
tearDownExample(props);
}
/**
* HelloWorld provides an example of a class that implements
* a resource extension client, exposing a method for each service.
* Typically, this class would be a top-level class.
*/
static public class HelloWorld extends ResourceManager {
static final public String NAME = "helloWorld";
static final public ExtensionMetadata.ScriptLanguage scriptLanguage
= ExtensionMetadata.JAVASCRIPT;
private XMLDocumentManager docMgr;
public HelloWorld(DatabaseClient client) {
super();
// a Resource Manager must be initialized by a Database Client
client.init(NAME, this);
// the Dictionary Manager delegates some services to a document manager
docMgr = client.newXMLDocumentManager();
}
public String sayHello() {
RequestParameters params = new RequestParameters();
params.add("service", "hello");
params.add("planet", "Earth");
// call the service
ServiceResultIterator resultItr = getServices().get(params);
// iterate over the results
List<String> responses = new ArrayList<>();
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);
}
}
// install the resource extension on the server
public static void installResourceExtension(ExampleProperties props) throws IOException {
DatabaseClient client = Util.newAdminClient(props);
// use either shortcut or strong typed IO
installResourceExtension(client);
// release the client
client.release();
}
public static void installResourceExtension(DatabaseClient client) throws IOException {
// create a manager for resource extensions
ResourceExtensionsManager resourceMgr = client.newServerConfigManager().newResourceExtensionsManager();
// specify metadata about the resource extension
ExtensionMetadata metadata = new ExtensionMetadata();
metadata.setTitle("Hello World Resource Services");
metadata.setDescription("This resource extension is written in javascript");
metadata.setProvider("MarkLogic");
metadata.setVersion("0.1");
metadata.setScriptLanguage(HelloWorld.scriptLanguage);
// acquire the resource extension source code
InputStream sourceStream = Util.openStream(
"scripts"+File.separator+HelloWorld.NAME+".sjs");
if (sourceStream == null)
throw new IOException("Could not read example resource extension");
// write the resource extension to the database
resourceMgr.writeServicesAs(HelloWorld.NAME, sourceStream, metadata,
new MethodParameters(MethodType.GET));
System.out.println("(Shortcut) Installed the resource extension on the server");
}
// use the resource manager
public static void useResource(ExampleProperties props)
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException
{
DatabaseClient client = Util.newClient(props);
// create the resource extension client
HelloWorld hello = new HelloWorld(client);
String response = hello.sayHello();
System.out.println("Called hello worlds service, got response:["+ response + "]");
// release the client
client.release();
}
// clean up by deleting the example resource extension
public static void tearDownExample(ExampleProperties props) {
DatabaseClient client = Util.newAdminClient(props);
ResourceExtensionsManager resourceMgr = client.newServerConfigManager().newResourceExtensionsManager();
resourceMgr.deleteServices(HelloWorld.NAME);
client.release();
}
}