Created
January 10, 2011 17:26
-
-
Save leonardinius/773088 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package xxx.yyy; | |
import com.atlassian.gadgets.*; | |
import com.atlassian.gadgets.view.GadgetViewFactory; | |
import com.atlassian.gadgets.view.View; | |
import com.atlassian.jira.security.xsrf.RequiresXsrfCheck; | |
import com.atlassian.jira.util.collect.MapBuilder; | |
import com.atlassian.jira.web.action.issue.AbstractIssueSelectAction; | |
import org.apache.commons.lang.StringEscapeUtils; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import webwork.action.ServletActionContext; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import java.io.IOException; | |
import java.io.StringWriter; | |
import java.net.URI; | |
import java.text.MessageFormat; | |
import java.util.Map; | |
public class MyWebAction extends AbstractIssueSelectAction | |
{ | |
// ------------------------------ FIELDS ------------------------------ | |
private static final String PREF_IS_POPUP = "isPopup"; | |
private static final String PREF_IS_CONFIGURED = "isConfigured"; | |
private static final String GADGET_URI = MessageFormat.format( | |
"rest/gadgets/1.0/g/{0}:gadget/my-gadget.xml", // my-gadget.xml - gadget definition xml | |
"my-plugin-key"); // plugin key as defined at atlassian-plugin@key in the atlassian.-plugin.xml | |
// .artifactId}" | |
final GadgetRenderer renderer = new GadgetRenderer(); | |
// OSGi component - should be imported as component | |
private final GadgetRequestContextFactory gadgetRequestContextFactory; | |
// OSGi component - should be imported as component | |
private final GadgetViewFactory gadgetViewFactory; | |
private final Logger logger = LoggerFactory.getLogger(MyWebAction.class); | |
// --------------------------- CONSTRUCTORS --------------------------- | |
public MyWebAction(final GadgetRequestContextFactory gadgetRequestContextFactory, final GadgetViewFactory gadgetViewFactory) | |
{ | |
super(); | |
this.gadgetRequestContextFactory = gadgetRequestContextFactory; | |
this.gadgetViewFactory = gadgetViewFactory; | |
} | |
// --------------------- GETTER / SETTER METHODS --------------------- | |
public GadgetRenderer getRenderer() | |
{ | |
return renderer; | |
} | |
// -------------------------- OTHER METHODS -------------------------- | |
@SuppressWarnings({"UnusedDeclaration"}) | |
@RequiresXsrfCheck | |
@Override | |
public String doDefault() throws Exception | |
{ | |
return super.doDefault(); | |
} | |
public String jsEncode(String encodeMe) | |
{ | |
return StringEscapeUtils.escapeJavaScript(encodeMe); | |
} | |
// -------------------------- INNER CLASSES -------------------------- | |
public class GadgetRenderer | |
{ | |
private static final String PROJECT_ID = "projectId"; | |
@SuppressWarnings({"UnusedDeclaration"}) | |
public String render() throws GadgetNotFoundException | |
{ | |
final StringWriter out = new StringWriter(); | |
try | |
{ | |
final GadgetRequestContext requestContext = gadgetRequestContextFactory.get(getRequest()); | |
gadgetViewFactory.createGadgetView(getGadgetState(), View.DEFAULT, requestContext).writeTo(out); | |
out.flush(); | |
} | |
catch (IOException e) | |
{ | |
logger.error("Error rendering gadget '" + GADGET_URI + "'", e); | |
} | |
return out.getBuffer().toString(); | |
} | |
private String getGadgetId() | |
{ | |
//XXX: should not be hardcoded?? | |
// It seems it could be any hardcoded value as long as you provide the gadget with all the user preferences it needs | |
return "xyz-gadget"; | |
} | |
private GadgetState getGadgetState() throws GadgetNotFoundException | |
{ | |
final MapBuilder<String, String> preferencesBuilder = MapBuilder.newBuilder(); | |
preferencesBuilder.add(PROJECT_ID, getSelectedProjectObject().getKey()); // xyz gadget configuration parameter | |
// ... add as many parameters you need here | |
preferencesBuilder.add(PREF_IS_POPUP, Boolean.TRUE.toString()); | |
preferencesBuilder.add(PREF_IS_CONFIGURED, Boolean.TRUE.toString()); | |
final Map<String, String> preferences = preferencesBuilder.toMutableMap(); | |
return GadgetState.gadget(GadgetId.valueOf(getGadgetId())) | |
.specUri(URI.create(GADGET_URI)) | |
.userPrefs(preferences) | |
.build(); | |
} | |
private HttpServletRequest getRequest() {return ServletActionContext.getRequest();} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment