It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to access session/cookies/request in pageEvent? (Confluence plugin) Edited

Hi every Body!

I intergrated jira, confluence, crowd. (Using SSO).

In my confluence plugin, I handle page event. In page event, I want to create JIRA issue using SSO authentication(Crowd).

So, how to access session/cookies/request in pageEvent?

I tried with 

ServletActionContext.getRequest(); 

but it alway return null value.

Thanks for your help!

package confluence.smart.comment.api;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import javax.inject.Inject;
import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Context;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.web.servlet.support.RequestContextUtils;

import com.atlassian.applinks.api.ApplicationLink;
import com.atlassian.applinks.api.ApplicationLinkRequest;
import com.atlassian.applinks.api.ApplicationLinkRequestFactory;
import com.atlassian.applinks.api.ApplicationLinkResponseHandler;
import com.atlassian.applinks.api.ApplicationLinkService;
import com.atlassian.applinks.api.CredentialsRequiredException;
import com.atlassian.applinks.api.application.jira.JiraApplicationType;
import com.atlassian.applinks.api.auth.AuthenticationProvider;
import com.atlassian.applinks.spi.auth.AuthenticationConfigurationManager;
import com.atlassian.applinks.spi.auth.AuthenticationProviderPluginModule;
import com.atlassian.confluence.api.model.content.ContentStatus;
import com.atlassian.confluence.core.ContentEntityManager;
import com.atlassian.confluence.core.ContentEntityObject;
import com.atlassian.confluence.core.DefaultSaveContext;
import com.atlassian.confluence.event.events.content.comment.CommentCreateEvent;
import com.atlassian.confluence.event.events.content.page.PageCreateEvent;
import com.atlassian.confluence.event.events.content.page.PageUpdateEvent;
import com.atlassian.confluence.event.events.types.Authentication;
import com.atlassian.confluence.pages.CommentManager;
import com.atlassian.confluence.pages.DefaultPageManager;
import com.atlassian.confluence.pages.PageManager;
import com.atlassian.confluence.setup.settings.SettingsManager;
import com.atlassian.crowd.integration.rest.entity.SessionEntity;
import com.atlassian.crowd.service.client.CrowdClient;
import com.atlassian.crowd.service.factory.CrowdClientFactory;
import com.atlassian.event.api.EventListener;
import com.atlassian.event.api.EventPublisher;
import com.atlassian.json.jsonorg.JSONObject;
import com.atlassian.plugin.osgi.container.OsgiContainerManager;
import com.atlassian.plugin.spring.scanner.annotation.component.Scanned;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
import com.atlassian.sal.api.net.Response;
import com.atlassian.sal.api.net.ResponseException;
import com.atlassian.sal.api.net.Request.MethodType;
import com.atlassian.sal.api.pluginsettings.PluginSettings;
import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory;
import com.atlassian.spring.container.ContainerManager;
import com.opensymphony.webwork.ServletActionContext;
import com.opensymphony.xwork.ActionContext;

import confluence.smart.comment.admin.Config;
import confluence.smart.comment.utility.Util;

@Scanned
public class SmartCommentEventListener implements DisposableBean{

private static final Logger log = Logger.getLogger(SmartCommentEventListener.class);

private final String TITLE_COMMENT = "#Version comment";

@ComponentImport
private ApplicationLinkService applicationLinkService;

@ComponentImport
private EventPublisher eventPublisher;

@ComponentImport
private ContentEntityManager contentEntityManager;

@ComponentImport
private SettingsManager settingsManager ;

@ComponentImport
private PluginSettingsFactory pluginSettingsFactory;

private ApplicationLink jiraLink;

@Inject
public SmartCommentEventListener(@ComponentImport EventPublisher eventPublisher,
@ComponentImport ApplicationLinkService applicationLinkService,
@ComponentImport ContentEntityManager contentEntityManager,
@ComponentImport SettingsManager settingsManager,
@ComponentImport PluginSettingsFactory pluginSettingsFactory
) {
this.eventPublisher = eventPublisher;
this.applicationLinkService = applicationLinkService;
this.contentEntityManager = contentEntityManager;
this.settingsManager = settingsManager;
jiraLink = applicationLinkService.getPrimaryApplicationLink(JiraApplicationType.class);
eventPublisher.register(this);
this.pluginSettingsFactory = pluginSettingsFactory;

}

@EventListener
public void pageUpdateEvent(PageUpdateEvent event) {
//commentToJiraWithSSO
//how to get cookies for making jira request with sso using crowd

commentToJiraWithSSO(someComment);

}


private void commentToJiraWithSSO(Comment comment) {
PluginSettings settings = pluginSettingsFactory.createGlobalSettings();
String jiraBaseUrl = (String) settings.get(Config.class.getName() + ".jiraBaseUrl");
JSONObject jsonComment = new JSONObject();
jsonComment.put("body", comment.getCommentContent());
if(jiraBaseUrl != null && !jiraBaseUrl.equals("")) {
URL obj;
String responseText = "";
try {
obj = new URL(jiraBaseUrl + "/rest/api/2/issue/"+comment.getIssueKey()+"/comment" );
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod(MethodType.POST.toString());
HttpServletRequest request = ServletActionContext.getRequest();
Cookie[] cookiesArr = request.getCookies();
String cookies = "";
for (Cookie cookie : cookiesArr) {
cookies = cookies + cookie.getName() + "=" + cookie.getValue() + "; domain=" + cookie.getDomain()+ "; path="+cookie.getPath()+ ";";
}
con.setRequestProperty("Cookie", cookies);
con.setRequestProperty("X-Forwarded-For", PortalUtil.getHttpServletRequest(request).getRemoteAddr());
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) { // success
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
responseText = response.toString();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

@Override
public void destroy() throws Exception {
eventPublisher.unregister(this);

}

}

 

1 answer

0 votes
Minh_Tran Atlassian Team Jan 09, 2020

@Mai Hữu i suggest you to use ApplicationLink to do the Jira comment from Confluence plugin https://developer.atlassian.com/server/framework/atlassian-sdk/application-links/

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Posted in Confluence

How is your team having fun and bonding, remotely, utilizing Confluence?

Thanks everyone for answering last week’s question. The winner of the random drawing from those who commented is: @LarryBrock I’ll contact you separately with your prize details. This wee...

306 views 9 7
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you