QiMoLin
December 9, 2020, 12:17pm
1
Hi,
I’ve seen some example usages of Velocity templates and wanted to use it for my plugin. I found this link: “Confluence Server Developer Documentation ” and thought it could be very useful in the plugin that I want to build for Bitbucket Server. However, I could not find anything on Velocity templates in Bitbucket Server plugins. So I would like to know if it is possible to use Velocity templates for Bitbucket Server plugins.
Thanks.
Hello!
I was able to use Velocity templates with my custom servlet. There are 4 steps to achieve that.
Add dependencies to your pom.xml:
<dependency>
<groupId>com.atlassian.templaterenderer</groupId>
<artifactId>atlassian-template-renderer-api</artifactId>
<version>${atr.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.atlassian.bitbucket.server</groupId>
<artifactId>bitbucket-api</artifactId>
<version>${bitbucket.api.version}</version>
<scope>provided</scope>
</dependency>
<bitbucket.api.version>7.2.2</bitbucket.api.version>
<atr.version>4.0.0</atr.version>
Add section to atlassian-plugin.xml. For my case it looks like this:
<servlet name="BPMN Diff Page"
i18n-name-key="ru.domclick.bitbucket.bpmn-diff-servlet.name"
key="ru.domclick.bpmn-diff-servlet"
class="ru.domclick.bitbucket.BpmnDiffServlet">
<url-pattern>/bpmn-diff</url-pattern>
</servlet>
Add Velocity template file to your resources. In my case it was a file reources/velocity/bpmn-diff.vm.
Implement servlet class.
package ru.domclick.bitbucket;
import com.atlassian.bitbucket.auth.AuthenticationContext;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
import com.atlassian.templaterenderer.TemplateRenderer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@Component
public class BpmnDiffServlet extends HttpServlet {
private static final String TEMPLATE_PATH = "velocity/bpmn-diff.vm";
private final TemplateRenderer templateRenderer;
private final AuthenticationContext authenticationContext;
@Autowired
public BpmnDiffServlet(
@ComponentImport TemplateRenderer templateRenderer,
@ComponentImport AuthenticationContext authenticationContext
) {
this.templateRenderer = templateRenderer;
this.authenticationContext = authenticationContext;
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
if (authenticationContext.isAuthenticated()) {
response.setContentType("text/html");
Map<String, Object> params = new HashMap<>();
templateRenderer.render(TEMPLATE_PATH, params, response.getWriter());
}
}
}
Hope this helps.