@Override public void handle(Request request, Response response) throws Exception { try (JsonWriter json = response.newJsonWriter()) { json.beginObject().name("importers").beginArray(); for (ProfileImporter importer : importers) { json.beginObject() .prop("key", importer.getKey()) .prop("name", importer.getName()) .name("languages").beginArray(); for (String languageKey : importer.getSupportedLanguages()) { json.value(languageKey); } json.endArray().endObject(); } json.endArray().endObject(); } } }
private QProfileResult importXml(QProfileDto profile, String importerKey, Reader xml, DbSession dbSession) { QProfileResult result = new QProfileResult(); ValidationMessages messages = ValidationMessages.create(); ProfileImporter importer = getProfileImporter(importerKey); RulesProfile definition = importer.importProfile(xml, messages); List<ActiveRuleChange> changes = importProfile(profile, definition, dbSession); result.addChanges(changes); processValidationMessages(messages, result); return result; }
@Test public void supportAllLanguages() { ProfileImporter importer = new ProfileImporter("all", "All") { @Override public RulesProfile importProfile(Reader reader, ValidationMessages messages) { return null; } }; assertThat(importer.getSupportedLanguages().length, is(0)); importer.setSupportedLanguages(); assertThat(importer.getSupportedLanguages().length, is(0)); } }
@Override public void define(WebService.NewController controller) { NewAction create = controller.createAction(ACTION_CREATE) .setPost(true) .setDescription("Create a quality profile.<br>" + "Requires to be logged in and the 'Administer Quality Profiles' permission.") .setResponseExample(getClass().getResource("create-example.json")) .setSince("5.2") .setHandler(this); createOrganizationParam(create) .setSince("6.4"); create.createParam(PARAM_NAME) .setRequired(true) .setMaximumLength(NAME_MAXIMUM_LENGTH) .setDescription("Quality profile name") .setExampleValue("My Sonar way") .setDeprecatedKey("profileName", "6.6"); create.createParam(PARAM_LANGUAGE) .setRequired(true) .setDescription("Quality profile language") .setExampleValue("js") .setPossibleValues(getOrderedLanguageKeys(languages)); for (ProfileImporter importer : importers) { create.createParam(getBackupParamName(importer.getKey())) .setDescription(String.format("A configuration file for %s.", importer.getName())); } }
private ProfileImporter getProfileImporter(String importerKey) { for (ProfileImporter importer : importers) { if (StringUtils.equals(importerKey, importer.getKey())) { return importer; } } throw BadRequestException.create("No such importer : " + importerKey); }
@Override public void define(WebService.NewController controller) { NewAction create = controller.createAction(ACTION_CREATE) .setPost(true) .setDescription("Create a quality profile.<br>" + "Requires to be logged in and the 'Administer Quality Profiles' permission.") .setResponseExample(getClass().getResource("create-example.json")) .setSince("5.2") .setHandler(this); createOrganizationParam(create) .setSince("6.4"); create.createParam(PARAM_NAME) .setRequired(true) .setMaximumLength(NAME_MAXIMUM_LENGTH) .setDescription("Quality profile name") .setExampleValue("My Sonar way") .setDeprecatedKey("profileName", "6.6"); create.createParam(PARAM_LANGUAGE) .setRequired(true) .setDescription("Quality profile language") .setExampleValue("js") .setPossibleValues(getLanguageKeys(languages)); for (ProfileImporter importer : importers) { create.createParam(getBackupParamName(importer.getKey())) .setDescription(String.format("A configuration file for %s.", importer.getName())); } }
private CreateWsResponse doHandle(DbSession dbSession, CreateRequest createRequest, Request request, OrganizationDto organization) { QProfileResult result = new QProfileResult(); QProfileDto profile = profileFactory.checkAndCreateCustom(dbSession, organization, QProfileName.createFor(createRequest.getLanguage(), createRequest.getName())); result.setProfile(profile); for (ProfileImporter importer : importers) { String importerKey = importer.getKey(); InputStream contentToImport = request.paramAsInputStream(getBackupParamName(importerKey)); if (contentToImport != null) { result.add(exporters.importXml(profile, importerKey, contentToImport, dbSession)); } } activeRuleIndexer.commitAndIndex(dbSession, result.getChanges()); return buildResponse(result, organization); }
@Override public void handle(Request request, Response response) throws Exception { try (JsonWriter json = response.newJsonWriter()) { json.beginObject().name("importers").beginArray(); for (ProfileImporter importer : importers) { json.beginObject() .prop("key", importer.getKey()) .prop("name", importer.getName()) .name("languages").beginArray(); for (String languageKey : importer.getSupportedLanguages()) { json.value(languageKey); } json.endArray().endObject(); } json.endArray().endObject(); } } }
@Test public void testSupportedLanguages() { ProfileImporter inmporter = new ProfileImporter("all", "All") { @Override public RulesProfile importProfile(Reader reader, ValidationMessages messages) { return null; } }; inmporter.setSupportedLanguages("java", "php"); assertThat(inmporter.getSupportedLanguages().length, is(2)); assertThat(inmporter.getSupportedLanguages()[0], is("java")); assertThat(inmporter.getSupportedLanguages()[1], is("php")); }
private ProfileImporter getProfileImporter(String importerKey) { for (ProfileImporter importer : importers) { if (StringUtils.equals(importerKey, importer.getKey())) { return importer; } } throw BadRequestException.create("No such importer : " + importerKey); }
private QProfileResult importXml(QProfileDto profile, String importerKey, Reader xml, DbSession dbSession) { QProfileResult result = new QProfileResult(); ValidationMessages messages = ValidationMessages.create(); ProfileImporter importer = getProfileImporter(importerKey); RulesProfile definition = importer.importProfile(xml, messages); List<ActiveRuleChange> changes = importProfile(profile, definition, dbSession); result.addChanges(changes); processValidationMessages(messages, result); return result; }
private CreateWsResponse doHandle(DbSession dbSession, CreateRequest createRequest, Request request, OrganizationDto organization) { QProfileResult result = new QProfileResult(); QProfileDto profile = profileFactory.checkAndCreateCustom(dbSession, organization, QProfileName.createFor(createRequest.getLanguage(), createRequest.getName())); result.setProfile(profile); for (ProfileImporter importer : importers) { String importerKey = importer.getKey(); InputStream contentToImport = request.paramAsInputStream(getBackupParamName(importerKey)); if (contentToImport != null) { result.add(exporters.importXml(profile, importerKey, contentToImport, dbSession)); } } activeRuleIndexer.commitAndIndex(dbSession, result.getChanges()); return buildResponse(result, organization); }