1 <%@ page import="org.wso2.carbon.context.CarbonContext" %>
  2 <%@ page import="org.wso2.carbon.context.RegistryType" %>
  3 <%@ page import="org.wso2.carbon.registry.api.Registry" %>
  4 <%@ page import="org.wso2.carbon.registry.api.Resource" %>
  5 <%@ page import="org.wso2.carbon.registry.api.Collection" %>
  6 
  7 <h2>WSO2 Carbon Registry Usage Demo</h2>
  8 
  9 <hr/>
 10 <p>
 11 
 12 <h3>Add New Resource</h3>
 13 <p>
 14 <form action="index.jsp" method="POST">
 15     <table border="0">
 16         <tr>
 17             <td>Registry Type</td>
 18             <td>
 19                 <select name="registryType">
 20                     <option selected="true"><%= RegistryType.SYSTEM_CONFIGURATION.toString() %></option>
 21                     <option><%= RegistryType.SYSTEM_GOVERNANCE.toString() %></option>
 22                 </select>
 23             </td>
 24         </tr>
 25         <tr>
 26             <td>Resource Path</td>
 27             <td><input type="text" name="resourcePath" value="foo/bar"/></td>
 28         </tr>
 29         <tr>
 30             <td>Value</td>
 31             <td><input type="text" name="value" value="WSO2 Carbon"/></td>
 32         </tr>
 33         <tr>
 34             <td>&nbsp;</td>
 35             <td><input type="submit" value="Add" name="add"></td>
 36         </tr>
 37     </table>
 38 </form>
 39 </p>
 40 <hr/>
 41 <p>
 42 
 43 <h3>View Resource</h3>
 44 <p>
 45 <form action="index.jsp" method="POST">
 46     <table border="0">
 47         <tr>
 48             <td>Registry Type</td>
 49             <td>
 50                 <select name="registryType">
 51                     <option selected="true"><%= RegistryType.SYSTEM_CONFIGURATION.toString() %></option>
 52                     <option><%= RegistryType.SYSTEM_GOVERNANCE.toString() %></option>
 53                 </select>
 54             </td>
 55         </tr>
 56         <tr>
 57             <td>Resource Path</td>
 58             <td><input type="text" name="resourcePath" value="foo/bar"/></td>
 59         </tr>
 60         <tr>
 61             <td>&nbsp;</td>
 62             <td><input type="submit" value="View" name="view"></td>
 63         </tr>
 64     </table>
 65 </form>
 66 </p>
 67 <hr/>
 68 
 69 <h3>Add Collection</h3>
 70 <p>
 71 <form action="index.jsp" method="POST">
 72     <table border="0">
 73         <tr>
 74             <td>Registry Type</td>
 75             <td>
 76                 <select name="registryType">
 77                     <option selected="true"><%= RegistryType.SYSTEM_CONFIGURATION.toString() %></option>
 78                     <option><%= RegistryType.SYSTEM_GOVERNANCE.toString() %></option>
 79                 </select>
 80             </td>
 81         </tr>
 82         <tr>
 83             <td>Resource Path</td>
 84             <td><input type="text" name="collectionPath" value="bar"/></td>
 85         </tr>
 86         <tr>
 87             <td>&nbsp;</td>
 88             <td><input type="submit" value="Add" name="addCollection"></td>
 89         </tr>
 90     </table>
 91 </form>
 92 </p>
 93 <hr/>
 94 
 95 <h3>Is Collection Exist</h3>
 96 <p>
 97 <form action="index.jsp" method="POST">
 98     <table border="0">
 99         <tr>
100             <td>Registry Type</td>
101             <td>
102                 <select name="registryType">
103                     <option selected="true"><%= RegistryType.SYSTEM_CONFIGURATION.toString() %></option>
104                     <option><%= RegistryType.SYSTEM_GOVERNANCE.toString() %></option>
105                 </select>
106             </td>
107         </tr>
108         <tr>
109             <td>Collection Path</td>
110             <td><input type="text" name="collectionPath" value="bar"/></td>
111         </tr>
112         <tr>
113             <td>&nbsp;</td>
114             <td><input type="submit" value="View" name="viewCollection"></td>
115         </tr>
116     </table>
117 </form>
118 </p>
119 <hr/>
120 
121 <h3>Delete Resource/Collection</h3>
122 <p>
123 <form action="index.jsp" method="POST">
124     <table border="0">
125         <tr>
126             <td>Registry Type</td>
127             <td>
128                 <select name="registryType">
129                     <option selected="true"><%= RegistryType.SYSTEM_CONFIGURATION.toString() %></option>
130                     <option><%= RegistryType.SYSTEM_GOVERNANCE.toString() %></option>
131                 </select>
132             </td>
133         </tr>
134         <tr>
135             <td>Registry Path</td>
136             <td><input type="text" name="registryPath" value="bar"/></td>
137         </tr>
138         <tr>
139             <td>&nbsp;</td>
140             <td><input type="submit" value="Delete" name="delete"></td>
141         </tr>
142     </table>
143 </form>
144 </p>
145 <hr/>
146 
147 <%
148     // Obtain the reference to the registry from the CarbonContext
149     CarbonContext cCtx = CarbonContext.getThreadLocalCarbonContext();
150 
151     Registry registry = cCtx.getRegistry(RegistryType.SYSTEM_CONFIGURATION);
152     String registryType = request.getParameter("registryType");
153     if(registryType != null) {
154        registry = cCtx.getRegistry(RegistryType.valueOf(registryType));
155     }
156 
157     if (request.getParameter("add") != null) {
158         Resource resource = registry.newResource();
159         resource.setContent(request.getParameter("value"));
160         String resourcePath = request.getParameter("resourcePath");
161         registry.put(resourcePath, resource);
162 %>
163 <p>
164     Added resource: <%= resourcePath %>
165 </p>
166 <%
167     } else if (request.getParameter("view") != null) {
168         String resourcePath = request.getParameter("resourcePath");
169         if (registry.resourceExists(resourcePath)) {
170             Resource resource = registry.get(resourcePath);
171             String content = new String((byte[]) resource.getContent());
172             response.addHeader("resource-content", content);
173 %>
174             <p>
175                 Resource at in Registry <%= registryType%> path <%= resourcePath%> : <%= content %>
176             </p>
177 <%
178         } else {
179 %>
180             <p>
181                 Resource at path <%= resourcePath%> does not exist in Registry <%= registryType%>!
182             </p>
183 <%
184         }
185     } else if (request.getParameter("delete") != null) {
186         String registryPath = request.getParameter("registryPath");
187         if (registry.resourceExists(registryPath)) {
188             Resource resource = registry.get(registryPath);
189             registry.delete(registryPath);
190             if (!registry.resourceExists(registryPath)) {
191             	response.addHeader("resource-deleted", "true");
192 %>
193                 <p>
194                     <%= registryPath%> path deleted from <%= registryType%>
195                 </p>
196 <%
197             }
198         } else {
199 %>
200             <p>
201                 <%= registryPath%> does not exist in Registry <%= registryType%>!
202             </p>
203 <%
204         }
205     } else if (request.getParameter("viewCollection") != null) {
206          String collectionPath = request.getParameter("collectionPath");
207          if (registry.resourceExists(collectionPath)) {
208              Resource resource = registry.get(collectionPath);
209              if(resource instanceof Collection){
210                 response.addHeader("collection-exist", "true");
211 %>
212                 <p>
213                     Registry collection exists in <%= registryType%> path <%= collectionPath%>
214                 </p>
215 <%
216              } else {
217 %>
218                 <p>
219                     <%= collectionPath%> is not a collection.
220                 </p>
221 <%
222              }
223          } else {
224 %>
225             <p>
226                 Collection at path <%= collectionPath%> does not exist in Registry <%= registryType%>!
227             </p>
228 <%
229          }
230     } else if (request.getParameter("addCollection") != null) {
231          Resource collection = registry.newCollection();
232          String collectionPath = request.getParameter("collectionPath");
233          registry.put(collectionPath, collection);
234 %>
235         <p>
236             Added collection : <%= collectionPath %>
237         </p>
238 <%
239     }
240 %>