Last updated Feb 19, 2024

Fisheye and Crucible Development : 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

1
2
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

1
2
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

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.

Decorate servlet with userprofile.tab decorator

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

TwitterSettingsServlet.java

1
2
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

1
2
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

1
2
<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

1
2
<!-- 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

1
2
<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

1
2
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

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

Rate this page: