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);
}
}