public Node(final Object object) { if (object == null) { throw new SonarException("object can not be null"); } this.object = object; }
@Test public void should_fail() { SecurityRealm realm = spy(new AlwaysFailsRealm()); settings.setProperty("sonar.security.realm", realm.getName()); try { new SecurityRealmFactory(settings.asConfig(), new SecurityRealm[] {realm}).start(); fail(); } catch (SonarException e) { assertThat(e.getCause()).isInstanceOf(IllegalStateException.class); assertThat(e.getMessage()).contains("Security realm fails to start"); } }
@Override public boolean matches(Object ex) { return ex instanceof SonarException && ((SonarException) ex).getCause() instanceof SocketTimeoutException; }
@Test public void realm_not_found() { settings.setProperty("sonar.security.realm", "Fake"); try { new SecurityRealmFactory(settings.asConfig()); fail(); } catch (SonarException e) { assertThat(e.getMessage()).contains("Realm 'Fake' not found."); } }
@Test public void authenticator_not_found() { settings.setProperty(CoreProperties.CORE_AUTHENTICATOR_CLASS, "Fake"); try { new SecurityRealmFactory(settings.asConfig()); fail(); } catch (SonarException e) { assertThat(e.getMessage()).contains("Authenticator 'Fake' not found."); } }
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); } }
throw new SonarException("The type of the field " + field + " is not supported: " + field.getType()); throw new SonarException("Can not set the value of the field " + field + " in the class: " + check.getClass().getName(), 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); } } } }