<%@ page import="org.wso2.carbon.context.CarbonContext" %>

<h2>WSO2 Carbon Caching Demo</h2>

<hr/>
<p>

<h3>Add to Cache</h3>

<form action="index.jsp" method="POST">
    <table border="0">
        <tr>
            <td>Key</td>
            <td><input type="text" name="key"/></td>
        </tr>
        <tr>
            <td>Value</td>
            <td><input type="text" name="value"/></td>
        </tr>
        <tr>
            <td> </td>
            <td><input type="submit" value="Add" name="add"></td>
        </tr>
    </table>
</form>
</p>
<hr/>
<p>

<h3>Read from Cache</h3>

<form action="index.jsp" method="POST">
    <table border="0">
        <tr>
            <td>Key</td>
            <td><input type="text" name="key"/></td>
        </tr>
        <tr>
            <td> </td>
            <td><input type="submit" value="View" name="view"></td>
        </tr>
    </table>
</form>
</p>
<hr/>

<%
    // The CarbonContext instance used to obtain the cache
    CarbonContext cCtx = CarbonContext.getCurrentContext();

    if (request.getParameter("add") != null) {
        String key = request.getParameter("key");
        cCtx.getCache().put(key, request.getParameter("value"));
%>
<p>
    Added entry: <%= key %>
</p>
<%
    } else if (request.getParameter("view") != null) {
        String key = request.getParameter("key");
        if (cCtx.getCache().get(key) != null) {
            String content = (String)cCtx.getCache().get(key);
%>
            <p>
                Value of entry <%= key%> : <%= content %>
            </p>
<%
        } else {
%>
            <p>
                Unable to find an entry by the given key <%= key%>!
            </p>
<%
        }
    }
%>