Hi!
I have followed the tutorial on the following page: https://developer.atlassian.com/server/framework/atlassian-sdk/creating-an-admin-configuration-form/
I have gotten it to save and load the pluginsettings, as long as it's being accessed from the ConfigResource, which you can see below:
@Path("/")
public class ConfigResource {
private static final Logger log = LoggerFactory.getLogger(ConfigResource.class);
private static boolean transactionFlag = false;
@ComponentImport
private final UserManager userManager;
@ComponentImport
private final PluginSettingsFactory pluginSettingsFactory;
@ComponentImport
private final TransactionTemplate transactionTemplate;
@Autowired
public ConfigResource(UserManager userManager, PluginSettingsFactory pluginSettingsFactory, TransactionTemplate transactionTemplate) {
this.userManager = userManager;
this.pluginSettingsFactory = pluginSettingsFactory;
this.transactionTemplate = transactionTemplate;
}
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response get(@Context HttpServletRequest request) {
String username = userManager.getRemoteUsername(request);
if (username == null || !userManager.isSystemAdmin(username)) {
return Response.status(Status.UNAUTHORIZED).build();
}
return Response.ok(transactionTemplate.execute(new TransactionCallback() {
public Object doInTransaction() {
PluginSettings settings = pluginSettingsFactory.createGlobalSettings();
Config config = new Config();
config.setHost((String) settings.get(Config.class.getName() + ".host"));
String port = (String) settings.get(Config.class.getName() + ".port");
if (port != null) {
config.setPort(Integer.parseInt(port));
}
config.setEmail((String) settings.get(Config.class.getName() + ".email"));
config.setPassword((String) settings.get(Config.class.getName() + ".password"));
config.setSpacekey((String) settings.get(Config.class.getName() + ".spacekey"));
return config;
}
})).build();
}
@PUT
@Consumes(MediaType.APPLICATION_JSON)
public Response put(final Config config, @Context HttpServletRequest request) {
String username = userManager.getRemoteUsername(request);
if (username == null || !userManager.isSystemAdmin(username)) {
return Response.status(Status.UNAUTHORIZED).build();
}
transactionTemplate.execute(new TransactionCallback() {
public Object doInTransaction() {
PluginSettings pluginSettings = pluginSettingsFactory.createGlobalSettings();
pluginSettings.put(Config.class.getName() + ".host", config.getHost());
pluginSettings.put(Config.class.getName() + ".port", Integer.toString(config.getPort()));
pluginSettings.put(Config.class.getName() + ".email", config.getEmail());
pluginSettings.put(Config.class.getName() + ".password", config.getPassword());
pluginSettings.put(Config.class.getName() + ".spacekey", config.getSpacekey());
return null;
}
});
return Response.noContent().build();
}
@Path("/testconfig")
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response testConfig(final Config config, @Context HttpServletRequest request) {
transactionFlag = false;
String username = userManager.getRemoteUsername(request);
if (username == null || !userManager.isSystemAdmin(username)) {
return Response.status(Status.UNAUTHORIZED).build();
}
transactionTemplate.execute(new TransactionCallback() {
public Object doInTransaction() {
Properties props = new Properties();
props.put("mail.store.protocol", "imap");
props.put("mail.imap.connectiontimeout", "5000");
props.put("mail.imap.timeout", "5000");
Session session = Session.getDefaultInstance(props, null);
Store store;
try {
store = session.getStore();
} catch (NoSuchProviderException e) {
log.error("ERROR: " + e.getMessage());
return null;
}
try {
store.connect(config.getHost(), config.getPort(), config.getEmail(), config.getPassword());
if (store.isConnected()) {
store.close();
transactionFlag = true;
return null;
} else {
store.close();
return null;
}
} catch (MessagingException e) {
log.error("ERROR: " + e.getMessage());
return null;
}
}
});
return (transactionFlag) ? Response.status(Status.OK).build() : Response.status(Status.NOT_ACCEPTABLE).build();
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public static final class Config {
@XmlElement private String host;
@XmlElement private int port;
@XmlElement private String email;
@XmlElement private String password;
@XmlElement private String spacekey;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSpacekey() {
return spacekey;
}
public void setSpacekey(String spacekey) {
this.spacekey = spacekey;
}
}
}
However, I have another Java file called "CheckMail.java", where I would like to use these settings. However I get a null pointer when I try to access these variables. I have tried the following:
package net.teliacompany.diva.confluence.parbackend.utilities;
import java.util.Properties;
import javax.inject.Inject;
import javax.inject.Named;
import javax.mail.Folder;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
import com.atlassian.sal.api.pluginsettings.PluginSettings;
import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.teliacompany.diva.confluence.parbackend.utilities.ConfigResource.Config;
public class CheckMail {
private static final Logger log = LoggerFactory.getLogger(CheckMail.class);
public CheckMail() {
}
@ComponentImport
private PluginSettingsFactory pluginSettingsFactory;
@Inject
public CheckMail(PluginSettingsFactory pluginSettingsFactory) {
this.pluginSettingsFactory = pluginSettingsFactory;
}
public boolean checkIfThereAnyNewAvailableMails() throws MessagingException {
log.warn("checking mail");
PluginSettings settings = pluginSettingsFactory.createGlobalSettings();
log.warn("Host: " + settings.get(Config.class.getName() + ".host"));
log.warn("Port: " + settings.get(Config.class.getName() + ".port"));
log.warn("Email: " + settings.get(Config.class.getName() + ".email"));
log.warn("Password: " + settings.get(Config.class.getName() + ".password"));
log.warn("spaceKey: " + settings.get(Config.class.getName() + ".spacekey"));
return true;
// log.warn("checking mail");
// Properties props = new Properties();
// props.put("mail.store.protocol", "imap");
// Session session = Session.getDefaultInstance(props, null);
// Store store = session.getStore();
// Folder inbox = store.getFolder("INBOX");
// inbox.open(Folder.READ_ONLY);
// int messageCount = inbox.getMessageCount();
// inbox.close(false);
// store.close();
// return (messageCount > 0) ? true : false;
}
}
Does anyone here know how to access PluginSettings from another class/file?
Thanks!
I suppose you need to mark your CheckMail class with @Component or @Named annotation.
Hey!
Yeah it was probably due to that. It felt like I tried that though.. strange.
In any case, if anyone's interested to see the code and how I solved it for CheckMail.java, please see below:
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
import com.atlassian.sal.api.pluginsettings.PluginSettings;
import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import net.teliacompany.diva.confluence.parbackend.utilities.ConfigResource.Config;
@Component
public class CheckMail {
private static final Logger log = LoggerFactory.getLogger(CheckMail.class);
@ComponentImport
private static PluginSettingsFactory pluginSettingsFactory;
@Autowired
public CheckMail(PluginSettingsFactory pluginSettingsFactory) {
this.pluginSettingsFactory = pluginSettingsFactory;
}
public static int checkIfThereAnyNewAvailableMails() throws MessagingException {
log.warn("checking mail");
int messageCountToReturn = 0;
PluginSettings settings = pluginSettingsFactory.createGlobalSettings();
String host = (String) settings.get(Config.class.getName() + ".host");
int port = Integer.parseInt((String) settings.get(Config.class.getName() + ".port"));
String user = (String) settings.get(Config.class.getName() + ".email");
String password = (String) settings.get(Config.class.getName() + ".password");
Properties props = new Properties();
props.put("mail.store.protocol", "imap");
props.put("mail.imap.connectiontimeout", "5000");
props.put("mail.imap.timeout", "5000");
Session session = Session.getDefaultInstance(props, null);
Store store;
try {
store = session.getStore();
try {
store.connect(host, port, user, password);
if (store.isConnected()) {
log.warn("We connected!");
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
log.warn("inbox messages: " + inbox.getMessageCount());
messageCountToReturn = inbox.getMessageCount();
inbox.close(false);
store.close();
return messageCountToReturn;
} else {
store.close();
return messageCountToReturn;
}
} catch (MessagingException e) {
store.close();
log.error("ERROR: " + e.getMessage());
return messageCountToReturn;
}
} catch (NoSuchProviderException e) {
log.error("ERROR: " + e.getMessage());
return messageCountToReturn;
}
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.