Stage 3: Profile settings servlet setup

At this point open the project in your IDE.

Servlet skeleton

Create a new class TwitterSettingServlet which will let you set Twitter credentials from the admin panel.

TwitterSettingsServlet.java
package com.example.ampstutorial;

import javax.servlet.http.HttpServlet;

public class TwitterSettingsServlet extends HttpServlet {
    
}

Http GET: Hello world

Let's start with very simple output. Override method doGet:

TwitterSettingsServlet.java
import 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!");
	}

New entry in Profile settings

To make your servlet visible in the Profile settings menu, add it to atlassian-plugin.xml

atlassian-plugin.xml
<!-- 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.

Decorate servlet with userprofile.tab decorator

Add the following lines at the beginning of the servlet's  doGet  method:

TwitterSettingsServlet.java
request.setAttribute("decorator", "fisheye.userprofile.tab");
response.setContentType("text/html");

Remember to use pi each time to reload the servlet.

Refresh the page and check that after clicking Twitter Configuration the menu on the left no longer disappears.

Menu item highlighting

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
response.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.

Templates

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.

Add TemplateRenderer dependency

pom.xml
<dependency>
	<groupId>com.atlassian.templaterenderer</groupId>
	<artifactId>atlassian-template-renderer-api</artifactId>
	<version>1.5.4</version>
	<scope>provided</scope>
</dependency>

Add TemplateRenderer component

atlassian-plugin.xml
<!-- Import the template renderer -->
<component-import key="templateRenderer" interface="com.atlassian.templaterenderer.TemplateRenderer" />

Create basic template

In the resources directory create the "Hello world!" template:

templates/twitterSettings.vm
<html>
<head>
	<meta name='profile.tab.key' content='com.example.ampstutorial.fecrutwitter:config-link'/>
</head>
<body>
Hello world!
</body>
</html>

Inject template renderer

TwitterSettingsServlet.java
import com.atlassian.templaterenderer.TemplateRenderer;
	private final TemplateRenderer templateRenderer;
	public TwitterSettingsServlet(TemplateRenderer templateRenderer) {
		this.templateRenderer = templateRenderer;
	}

Use template

In doGet replace the response you had so far with:

TwitterSettingsServlet.java
import com.google.common.collect.ImmutableMap;
 
	templateRenderer.render("/templates/twitterSettings.vm", ImmutableMap.of(), response.getWriter());

 

Was this page helpful?

Have a question about this article?

See questions about this article

Powered by Confluence and Scroll Viewport