com.marvelution.jira.plugins.sonar.utils.SonarGadgetUtils Maven / Gradle / Ivy
/*
* Licensed to Marvelution under one or more contributor license
* agreements. See the NOTICE file distributed with this work
* for additional information regarding copyright ownership.
* Marvelution licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.marvelution.jira.plugins.sonar.utils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URI;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import com.atlassian.gadgets.GadgetId;
import com.atlassian.gadgets.GadgetRequestContext;
import com.atlassian.gadgets.GadgetRequestContextFactory;
import com.atlassian.gadgets.GadgetState;
import com.atlassian.gadgets.view.GadgetViewFactory;
import com.atlassian.gadgets.view.View;
import com.atlassian.gadgets.view.ViewType;
import com.atlassian.jira.util.collect.MapBuilder;
import com.atlassian.sal.api.message.I18nResolver;
import com.marvelution.jira.plugins.sonar.service.SonarAssociation;
/**
* Utility class for Sonar Gadgets
*
* @author Mark Rekveld
*/
public class SonarGadgetUtils {
private final Logger logger = Logger.getLogger(SonarGadgetUtils.class);
private final GadgetRequestContextFactory gadgetRequestContextFactory;
private final GadgetViewFactory gadgetViewFactory;
private final I18nResolver i18nResolver;
private static final Pattern GADGET_ID_PATTERN = Pattern.compile("^sonar\\.gadget\\.([a-z]*)\\.title$");
public static final String PREF_IS_CONFIGURED = "isConfigured";
public static final String PREF_TITLE_REQUIRED = "titleRequired";
public static final String PREFS_IS_CONFIGURABLE = "isConfigurable";
public static final String PREFS_REFRESH = "refresh";
public static final String PREF_SONAR_SERVER = "sonarServer";
public static final String PREF_SONAR_PROJECT = "sonarProject";
public static final String GADGET_URI_PREFIX =
"rest/gadgets/1.0/g/com.marvelution.jira.plugins.sonar/gadgets/sonar-";
public static final String GADGET_URI_SUFFIX = "-gadget.xml";
/**
* Constructor
*
* @param gadgetRequestContextFactory the {@link GadgetRequestContextFactory} implementation
* @param gadgetViewFactory the {@link GadgetViewFactory} implementation
* @param i18nResolver the {@link I18nResolver} implementation
*/
public SonarGadgetUtils(GadgetRequestContextFactory gadgetRequestContextFactory,
GadgetViewFactory gadgetViewFactory, I18nResolver i18nResolver) {
this.gadgetRequestContextFactory = gadgetRequestContextFactory;
this.gadgetViewFactory = gadgetViewFactory;
this.i18nResolver = i18nResolver;
}
/**
* Create the Gadget preferences in a {@link MapBuilder} object
*
* @param association the {@link SonarAssociation} to create the preferences for
* @return the {@link MapBuilder} containing the Gadget preferences
* @see SonarGadgetUtils#getGadgetPreferences(String, String)
*/
public MapBuilder createGadgetPreferences(SonarAssociation association) {
return createGadgetPreferences(association.getSonarServer(), association.getSonarProject());
}
/**
* Create the Gadget preferences for the given Sonar Server Url and Sonar Project key
*
* @param sonarServer the Sonar Server Url
* @param sonarProject the Sonar Project Key
* @return the {@link MapBuilder} containing the Gadget preferences
*/
public MapBuilder createGadgetPreferences(String sonarServer, String sonarProject) {
final MapBuilder prefsBuilder = MapBuilder.newBuilder();
prefsBuilder.add(PREF_IS_CONFIGURED, Boolean.TRUE.toString());
prefsBuilder.add(PREFS_IS_CONFIGURABLE, Boolean.FALSE.toString());
prefsBuilder.add(PREFS_REFRESH, Boolean.FALSE.toString());
prefsBuilder.add(PREF_TITLE_REQUIRED, Boolean.FALSE.toString());
prefsBuilder.add(PREF_SONAR_SERVER, sonarServer);
prefsBuilder.add(PREF_SONAR_PROJECT, sonarProject);
return prefsBuilder;
}
/**
* Generate the HTML to view a gadget on a page
*
* @param gadgetId the id of the gadget
* @param prefsBuilder the {@link MapBuilder} configured with all the gadget preferences
* @param request the {@link HttpServletRequest} to get the {@link GadgetRequestContext} from
* @return the HTML {@link String}
*/
public String generateGadgetHtml(String gadgetId, MapBuilder prefsBuilder,
HttpServletRequest request) {
final GadgetState gadget = GadgetState.gadget(GadgetId.valueOf(gadgetId)).specUri(
URI.create(GADGET_URI_PREFIX + gadgetId + GADGET_URI_SUFFIX)).userPrefs(prefsBuilder.toMap()).build();
try {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final Writer gadgetWriter = new OutputStreamWriter(baos);
final GadgetRequestContext requestContext = gadgetRequestContextFactory.get(request);
final View settings = new View.Builder().viewType(ViewType.DEFAULT).writable(false).build();
gadgetViewFactory.createGadgetView(gadget, settings, requestContext).writeTo(gadgetWriter);
gadgetWriter.flush();
return baos.toString();
} catch (IOException e) {
logger.error("Error rendering Sonar '" + gadgetId + "' gadget.", e);
} catch (RuntimeException e) {
logger.error("Runtime error rendering Sonar '" + gadgetId + "' gadget.", e);
}
return "";
}
/**
* Get all the Sonar gadget Ids
*
* @return {@link Set} with all the Sonar Gadget Ids
*/
public Set getGadgetIds() {
final Set gadgetIds = new HashSet();
final Map bundle = i18nResolver.getAllTranslationsForPrefix("sonar.gadget", Locale.ENGLISH);
for (final Entry entry : bundle.entrySet()) {
final Matcher matcher = GADGET_ID_PATTERN.matcher(entry.getKey());
if (matcher.find()) {
gadgetIds.add(matcher.group(1));
}
}
return gadgetIds;
}
}