At this point open the project in your IDE.
Create a new class TwitterSettingServlet which will let you set Twitter credentials from the admin panel.
TwitterSettingsServlet.java
1 2package com.example.ampstutorial; import javax.servlet.http.HttpServlet; public class TwitterSettingsServlet extends HttpServlet { }
Let's start with very simple output. Override method doGet:
TwitterSettingsServlet.java
1 2import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().append("Hello world!"); }
To make your servlet visible in the Profile settings menu, add it to atlassian-plugin.xml
.
atlassian-plugin.xml
1 2<!-- Load TwitterSettingsServlet --> <servlet key="twitterSettingsServlet" class="com.example.ampstutorial.TwitterSettingsServlet"> <url-pattern>/twitter-settings</url-pattern> </servlet> <!-- Make twitter-settings servlet visible in profile settings menu --> <web-item key="config-link" section="system.userprofile.tab"> <link>/plugins/servlet/twitter-settings</link> <label key="Twitter Configuration"/> </web-item>
Install the servlet with pi
and go to http://localhost:3990/fecru/. Expand the top-rightmost menu and choose Profile settings. You should now see Twitter Configuration on the list. Click it to get your "Hello world!" message. Notice that the menu disappeared.
Add the following lines at the beginning of the servlet's doGet
method:
TwitterSettingsServlet.java
1 2request.setAttribute("decorator", "fisheye.userprofile.tab"); response.setContentType("text/html");
Remember to use pi
each time to reload the servlet.
The Twitter Configuration item is not highlighted correctly when selected. That's because the decorator can't identify the link belonging to the active tab. We need to set the profile.tab.key
attribute to the key of the Web Item. Replace the plain "Hello world!" string with:
TwitterSettingsServlet.java
1 2response.getWriter().print( "<html><head>" + "<meta name='profile.tab.key' content='com.example.ampstutorial.fecrutwitter:config-link'/>" + "</head><body><div>Hello world!</div></body></html>");
com.example.ampstutorial.fecrutwitter:config-link
is the plugin key (from the <atlassian-plugin>
element) and the key of the Web Item, separated by a colon.
Now the correct text should be highlighted when you click on the Twitter Configuration link.
Your servlet can use a templating library to produce HTML. Fisheye/Crucible provide some utility classes to help you use Velocity , but you could use other libraries too.
pom.xml
1 2<dependency> <groupId>com.atlassian.templaterenderer</groupId> <artifactId>atlassian-template-renderer-api</artifactId> <version>1.5.4</version> <scope>provided</scope> </dependency>
atlassian-plugin.xml
1 2<!-- Import the template renderer --> <component-import key="templateRenderer" interface="com.atlassian.templaterenderer.TemplateRenderer" />
In the resources directory create the "Hello world!" template:
templates/twitterSettings.vm
1 2<html> <head> <meta name='profile.tab.key' content='com.example.ampstutorial.fecrutwitter:config-link'/> </head> <body> Hello world! </body> </html>
TwitterSettingsServlet.java
1 2import com.atlassian.templaterenderer.TemplateRenderer; private final TemplateRenderer templateRenderer; public TwitterSettingsServlet(TemplateRenderer templateRenderer) { this.templateRenderer = templateRenderer; }
In doGet
replace the response you had so far with:
TwitterSettingsServlet.java
1 2import com.google.common.collect.ImmutableMap; templateRenderer.render("/templates/twitterSettings.vm", ImmutableMap.of(), response.getWriter());
Rate this page: