Commits

Ben Woskow committed e8ebbd4

Updating tutorial for API changes in m5. adding HelloWorldServlet.

Comments (0)

Files changed (1)

src/main/java/com/example/plugins/tutorial/servlet/HelloWorldServlet.java

+package com.example.plugins.tutorial.servlet;
+
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import com.atlassian.upm.api.license.entity.PluginLicense;
+import com.atlassian.upm.license.storage.lib.PluginLicenseStoragePluginUnresolvedException;
+import com.atlassian.upm.license.storage.lib.ThirdPartyPluginLicenseStorageManager;
+
+public class HelloWorldServlet extends HttpServlet
+{
+    private final ThirdPartyPluginLicenseStorageManager licenseManager;
+
+    public HelloWorldServlet(ThirdPartyPluginLicenseStorageManager licenseManager)
+    {
+        this.licenseManager = licenseManager;
+    }
+
+    @Override
+    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
+    {
+        resp.setContentType("text/html");
+        resp.getWriter().write("<html><body>");
+        
+        try
+        {
+            //Check and see if a license is currently stored
+            if (licenseManager.getLicense().isDefined())
+            {
+                for (PluginLicense pluginLicense : licenseManager.getLicense())
+                {
+                    //Check and see if the stored license has an error. If not, it is currently valid.
+                    if (pluginLicense.getError().isDefined())
+                    {
+                        //A license is currently stored, however, it is invalid (e.g. expired or user count mismatch)
+                        resp.getWriter().write("I'd love to say hello, but cannot do so because your license has an error: " 
+                                + pluginLicense.getError().get().name());
+                    }
+                    else
+                    {
+                        //A license is currently stored and it is valid.
+                        resp.getWriter().write("Hello, world! You are licensed!");
+                    }
+                }
+            }
+            else
+            {
+                //No license (valid or invalid) is stored.
+                resp.getWriter().write("I'd love to say hello, but cannot do so because you don't have a license.");
+            }
+        }
+        catch (PluginLicenseStoragePluginUnresolvedException e)
+        {
+            //The current license status cannot be retrieved because the Plugin License Storage plugin is unavailable.
+            resp.getWriter().write("I'd love to say hello, but cannot find my required resources. Please speak to a system administrator.");
+        }
+        
+        resp.getWriter().write("</body></html>");
+        resp.getWriter().close();
+    }
+}