Commits

Tim Pettersen committed cb733f7

SSH API test plugin

Comments (0)

Files changed (3)

+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>com.atlassian.stash</groupId>
+        <artifactId>stash-plugins-parent</artifactId>
+        <version>1.1.0-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>stash-ssh-api-test</artifactId>
+    <packaging>atlassian-plugin</packaging>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>com.atlassian.maven.plugins</groupId>
+                <artifactId>maven-stash-plugin</artifactId>
+                <extensions>true</extensions>
+                <configuration>
+                    <products>
+                        <product>
+                            <id>stash</id>
+                            <instanceId>stash</instanceId>
+                            <version>${project.version}</version>
+                            <dataVersion>${project.version}</dataVersion>
+                        </product>
+                    </products>
+                </configuration>
+            </plugin>
+        </plugins>
+
+        <resources>
+            <resource>
+                <directory>src/main/resources</directory>
+                <filtering>true</filtering>
+                <includes>
+                    <include>*.xml</include>
+                </includes>
+            </resource>
+            <resource>
+                <directory>src/main/resources</directory>
+                <filtering>false</filtering>
+                <excludes>
+                    <exclude>*.xml</exclude>
+                </excludes>
+            </resource>
+        </resources>
+    </build>
+
+    <dependencies>
+        <dependency>
+            <groupId>com.atlassian.stash</groupId>
+            <artifactId>stash-api</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>com.atlassian.stash</groupId>
+            <artifactId>stash-ssh</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>javax.servlet</groupId>
+            <artifactId>servlet-api</artifactId>
+            <scope>provided</scope>
+        </dependency>
+    </dependencies>
+
+</project>

src/main/java/com/atlassian/stash/ssh/testplugin/SshTestServlet.java

+package com.atlassian.stash.ssh.testplugin;
+
+import com.atlassian.stash.ssh.api.*;
+import com.atlassian.stash.user.StashAuthenticationContext;
+import com.atlassian.stash.user.StashUser;
+import com.atlassian.stash.util.PageRequestImpl;
+
+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.io.OutputStreamWriter;
+import java.io.Writer;
+
+public class SshTestServlet extends HttpServlet {
+
+    private final StashAuthenticationContext stashAuthenticationContext;
+    private final SshKeyService sshKeyService;
+    private final SshConfigurationService sshConfigurationService;
+    private final SshCloneUrlResolver sshCloneUrlResolver;
+
+    public SshTestServlet(StashAuthenticationContext stashAuthenticationContext,
+                          SshKeyService sshKeyService,
+                          SshConfigurationService sshConfigurationService,
+                          SshCloneUrlResolver sshCloneUrlResolver) {
+        this.stashAuthenticationContext = stashAuthenticationContext;
+        this.sshKeyService = sshKeyService;
+        this.sshConfigurationService = sshConfigurationService;
+        this.sshCloneUrlResolver = sshCloneUrlResolver;
+    }
+
+    @Override
+    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+        resp.setContentType("text/plain");
+        Writer writer = new OutputStreamWriter(resp.getOutputStream());
+        StashUser currentUser = stashAuthenticationContext.getCurrentUser();
+        if (currentUser != null) {
+            writer.write("First ten keys for " + currentUser.getName() + ":\n");
+            for (SshKey key : sshKeyService.findSshKeysForUser(currentUser, new PageRequestImpl(0, 10)).getValues()) {
+                writer.write(key.getLabel() + " " + key.getText() + "\n");
+            }
+            writer.write("\n");
+        } else {
+            writer.write("You are not logged in.\n\n");
+        }
+
+        SshConfiguration configuration = sshConfigurationService.getSshConfiguration();
+        writer.write("--- SSH Configuration ---\n");
+        writer.write("Enabled: " + configuration.isEnabled() + "\n");
+        writer.write("Port: " + configuration.getPort() + "\n");
+        writer.write("Base URL: " + configuration.getBaseUrl() + "\n");
+        writer.write("\n");
+
+        writer.write("Default Base URL: " + sshCloneUrlResolver.getDefaultBaseUrl());
+        writer.flush();
+
+        resp.setStatus(200);
+    }
+
+}

src/main/resources/atlassian-plugin.xml

+<atlassian-plugin key="com.atlassian.stash.stash-ssh-api-test" name="Atlassian Stash SSH API Test" system="true" plugins-version="2">
+    <plugin-info>
+        <description>Test plugin that imports the Stash SSH API</description>
+        <version>1.0-SNAPSHOT</version>
+        <vendor name="Atlassian" url="http://www.atlassian.com" />
+    </plugin-info>
+
+    <component-import key="sshConfigurationService" interface="com.atlassian.stash.ssh.api.SshConfigurationService" />
+    <component-import key="sshKeyService" interface="com.atlassian.stash.ssh.api.SshKeyService" />
+    <component-import key="sshCloneUrlResolver" interface="com.atlassian.stash.ssh.api.SshCloneUrlResolver" />
+
+    <servlet key="ssh-test-servlet" name="SSH Test Servlet" class="com.atlassian.stash.ssh.testplugin.SshTestServlet">
+        <url-pattern>/ssh-test</url-pattern>
+    </servlet>
+
+</atlassian-plugin>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.