public Node(final Object object) { if (object == null) { throw new SonarException("object can not be null"); } this.object = object; }
static String type(String type) { String validType = TYPE_MAP.get(type); if (null != validType) { return validType; } if (type.matches(".\\[.+\\]")) { return type; } throw new SonarException("Invalid property type [" + type + "]"); }
private static String readInputStream(String filePath, InputStream input) { String result; try { result = IOUtils.toString(input, UTF_8); } catch (IOException e) { throw new SonarException("Fail to load file: " + filePath, e); } finally { IOUtils.closeQuietly(input); } return result; }
/** * @since 3.6 */ public Rule setStatus(String status) { if (!STATUS_LIST.contains(status)) { throw new SonarException("The status of a rule can only contain : " + Joiner.on(", ").join(STATUS_LIST)); } this.status = status; return this; }
public List<Rule> parse(File file) { try (Reader reader = new InputStreamReader(FileUtils.openInputStream(file), UTF_8)) { return parse(reader); } catch (IOException e) { throw new SonarException("Fail to load the file: " + file, e); } }
/** * Warning : the input stream is closed in this method */ public List<Rule> parse(InputStream input) { try (Reader reader = new InputStreamReader(input, UTF_8)) { return parse(reader); } catch (IOException e) { throw new SonarException("Fail to load the xml stream", e); } }
private SonarException failToDownload(URI uri, IOException e) { throw new SonarException(String.format("Fail to download: %s (%s)", uri, getProxySynthesis(uri)), e); }
/** * Search for a property in all the manifests found in the classloader * * @return the values, an empty list if the property is not found. */ public static List<String> getPropertyValues(ClassLoader classloader, String key) { List<String> values = new ArrayList<>(); try { Enumeration<URL> resources = classloader.getResources("META-INF/MANIFEST.MF"); while (resources.hasMoreElements()) { Manifest manifest = new Manifest(resources.nextElement().openStream()); Attributes attributes = manifest.getMainAttributes(); String value = attributes.getValue(key); if (value != null) { values.add(value); } } } catch (IOException e) { throw new SonarException("Fail to load manifests from classloader: " + classloader, e); } return values; } }
public static NotificationQueueDto toNotificationQueueDto(Notification notification) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try { ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(notification); objectOutputStream.close(); return new NotificationQueueDto().setData(byteArrayOutputStream.toByteArray()); } catch (IOException e) { throw new SonarException("Unable to write notification", e); } finally { IOUtils.closeQuietly(byteArrayOutputStream); } }
private Notification convertToNotification(List<NotificationQueueDto> notifications) { try { // If batchSize is increased then we should return a list instead of a single element return notifications.get(0).toNotification(); } catch (InvalidClassException e) { // SONAR-4739 if (!alreadyLoggedDeserializationIssue) { logDeserializationIssue(); alreadyLoggedDeserializationIssue = true; } return null; } catch (IOException | ClassNotFoundException e) { throw new SonarException(UNABLE_TO_READ_NOTIFICATION, e); } }
private void verify() { for (Node node : nodes) { node.reset(); } for (Node node : nodes) { for (Node dep : node.getDependencies()) { if (!nodes.contains(dep)) { throw new SonarException("A dependent node (" + dep + ") of " + " (" + node + ") was not included in the nodes list."); } } node.resolveOrder(); } } }
public void write(RulesProfile profile, Writer writer) { try { appendHeader(profile, writer); appendRules(profile, writer); appendFooter(writer); } catch (IOException e) { throw new SonarException("Fail to export the profile " + profile, e); } }
public SecurityRealmFactory(Configuration config, SecurityRealm[] realms, LoginPasswordAuthenticator[] authenticators) { ignoreStartupFailure = config.getBoolean(SONAR_AUTHENTICATOR_IGNORE_STARTUP_FAILURE.getKey()).orElse(false); String realmName = config.get(SONAR_SECURITY_REALM.getKey()).orElse(null); String className = config.get(CoreProperties.CORE_AUTHENTICATOR_CLASS).orElse(null); SecurityRealm selectedRealm = null; if (!StringUtils.isEmpty(realmName)) { selectedRealm = selectRealm(realms, realmName); if (selectedRealm == null) { throw new SonarException(String.format( "Realm '%s' not found. Please check the property '%s' in conf/sonar.properties", realmName, SONAR_SECURITY_REALM.getKey())); } } if (selectedRealm == null && !StringUtils.isEmpty(className)) { LoginPasswordAuthenticator authenticator = selectAuthenticator(authenticators, className); if (authenticator == null) { throw new SonarException(String.format( "Authenticator '%s' not found. Please check the property '%s' in conf/sonar.properties", className, CoreProperties.CORE_AUTHENTICATOR_CLASS)); } selectedRealm = new CompatibilityRealm(authenticator); } realm = selectedRealm; }
public List<Rule> parse(Reader reader) { XMLInputFactory xmlFactory = XMLInputFactory.newInstance(); xmlFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE); xmlFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE); // just so it won't try to load DTD in if there's DOCTYPE xmlFactory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE); xmlFactory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE); SMInputFactory inputFactory = new SMInputFactory(xmlFactory); try { SMHierarchicCursor rootC = inputFactory.rootElementCursor(reader); rootC.advance(); // <rules> List<Rule> rules = new ArrayList<>(); SMInputCursor rulesC = rootC.childElementCursor("rule"); while (rulesC.getNext() != null) { // <rule> Rule rule = Rule.create(); rules.add(rule); processRule(rule, rulesC); } return rules; } catch (XMLStreamException e) { throw new SonarException("XML is not valid", e); } }
@Override public void start() { if (realm != null) { Logger logger = Loggers.get("org.sonar.INFO"); try { logger.info("Security realm: " + realm.getName()); realm.init(); logger.info("Security realm started"); } catch (RuntimeException e) { if (ignoreStartupFailure) { logger.error("IGNORED - Security realm fails to start: " + e.getMessage()); } else { throw new SonarException("Security realm fails to start: " + e.getMessage(), e); } } } }
public String getTemplate() { String result = loadTemplateFromCache(); try { if (result == null) { result = loadTemplateFromClasspath(); } if (result == null) { result = loadTemplateFromAbsolutePath(); } return result; } catch (IOException e) { throw new SonarException("Can not read the file " + getTemplatePath(), e); } }
@Test public void ignore_connection_errors() { when(reader.readString(any(URI.class), eq(StandardCharsets.UTF_8))).thenThrow(new SonarException()); assertThat(underTest.getUpdateCenter()).isAbsent(); }
private static void processParameter(Rule rule, SMInputCursor ruleC) throws XMLStreamException { RuleParam param = rule.createParameter(); String keyAttribute = ruleC.getAttrValue("key"); if (StringUtils.isNotBlank(keyAttribute)) { /* BACKWARD COMPATIBILITY WITH DEPRECATED FORMAT */ param.setKey(StringUtils.trim(keyAttribute)); } String typeAttribute = ruleC.getAttrValue("type"); if (StringUtils.isNotBlank(typeAttribute)) { /* BACKWARD COMPATIBILITY WITH DEPRECATED FORMAT */ param.setType(type(StringUtils.trim(typeAttribute))); } SMInputCursor paramC = ruleC.childElementCursor(); while (paramC.getNext() != null) { String propNodeName = paramC.getLocalName(); String propText = StringUtils.trim(paramC.collectDescendantText(false)); if (StringUtils.equalsIgnoreCase("key", propNodeName)) { param.setKey(propText); } else if (StringUtils.equalsIgnoreCase("description", propNodeName)) { param.setDescription(propText); } else if (StringUtils.equalsIgnoreCase("type", propNodeName)) { param.setType(type(propText)); } else if (StringUtils.equalsIgnoreCase("defaultValue", propNodeName)) { param.setDefaultValue(propText); } } if (StringUtils.isEmpty(param.getKey())) { throw new SonarException("Node <key> is missing in <param>"); } }
private static void addRuleProperty(Rule rule, Field field) { org.sonar.check.RuleProperty propertyAnnotation = field.getAnnotation(org.sonar.check.RuleProperty.class); if (propertyAnnotation != null) { String fieldKey = StringUtils.defaultIfEmpty(propertyAnnotation.key(), field.getName()); RuleParam param = rule.createParameter(fieldKey); param.setDescription(propertyAnnotation.description()); param.setDefaultValue(propertyAnnotation.defaultValue()); if (!StringUtils.isBlank(propertyAnnotation.type())) { try { param.setType(PropertyType.valueOf(propertyAnnotation.type().trim()).name()); } catch (IllegalArgumentException e) { throw new SonarException("Invalid property type [" + propertyAnnotation.type() + "]", e); } } else { param.setType(guessType(field.getType()).name()); } } }
private void configureSecureConnection(SimpleEmail email) { if (StringUtils.equalsIgnoreCase(configuration.getSecureConnection(), "ssl")) { email.setSSLOnConnect(true); email.setSSLCheckServerIdentity(true); email.setSslSmtpPort(String.valueOf(configuration.getSmtpPort())); // this port is not used except in EmailException message, that's why it's set with the same value than SSL port. // It prevents from getting bad message. email.setSmtpPort(configuration.getSmtpPort()); } else if (StringUtils.equalsIgnoreCase(configuration.getSecureConnection(), "starttls")) { email.setStartTLSEnabled(true); email.setStartTLSRequired(true); email.setSSLCheckServerIdentity(true); email.setSmtpPort(configuration.getSmtpPort()); } else if (StringUtils.isBlank(configuration.getSecureConnection())) { email.setSmtpPort(configuration.getSmtpPort()); } else { throw new SonarException("Unknown type of SMTP secure connection: " + configuration.getSecureConnection()); } }