org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration#com.sendgrid.SendGrid源码实例Demo

下面列出了org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration#com.sendgrid.SendGrid 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: ogham   文件: SendGridAssertions.java
@SuppressWarnings({ "squid:S1166", "unchecked" })
private static String getApiKeyFromFieldOrHeaders(SendGrid client) throws IllegalAccessException {
	try {
		return (String) readField(client, "apiKey", true);
	} catch (IllegalArgumentException e) {
		Map<String, String> requestHeaders = (Map<String, String>) readField(client, "requestHeaders", true);
		String authHeader = requestHeaders.get("Authorization");
		if (authHeader == null) {
			return null;
		}
		String apiKey = authHeader.substring(7);
		// special case to be compatible with previous versions
		if ("null".equals(apiKey)) {
			return null;
		}
		return apiKey;
	}
}
 
@Test
public void oghamAlone() throws Exception {
	contextRunner = contextRunner.withConfiguration(of(OghamSpringBoot2AutoConfiguration.class));
	contextRunner.run((context) -> {
		MessagingService messagingService = context.getBean(MessagingService.class);
		checkEmail(messagingService);
		checkSms(messagingService);
		OghamInternalAssertions.assertThat(messagingService)
			.sendGrid()
				.apiKey(equalTo("ogham"))
				.client(allOf(isA(SendGrid.class), not(isSpringBeanInstance(context, SendGrid.class))))
				.and()
			.thymeleaf()
				.all()
					.engine(isA(TemplateEngine.class))
					.and()
				.and()
			.freemarker()
				.all()
					.configuration()
						.defaultEncoding(equalTo(StandardCharsets.US_ASCII.name()));
	});
}
 
@Test
public void oghamInWebContext() throws Exception {
	contextRunner = contextRunner.withConfiguration(of(WebMvcAutoConfiguration.class, OghamSpringBoot2AutoConfiguration.class));
	contextRunner.run((context) -> {
		MessagingService messagingService = context.getBean(MessagingService.class);
		checkEmail(messagingService);
		checkSms(messagingService);
		OghamInternalAssertions.assertThat(messagingService)
			.sendGrid()
				.apiKey(equalTo("ogham"))
				.client(allOf(isA(SendGrid.class), not(isSpringBeanInstance(context, SendGrid.class))))
				.and()
			.thymeleaf()
				.all()
					.engine(isA(TemplateEngine.class))
					.and()
				.and()
			.freemarker()
				.all()
					.configuration()
						.defaultEncoding(equalTo(StandardCharsets.US_ASCII.name()));
	});
}
 
@Test
public void oghamAlone() throws Exception {
	context.register(OghamSpringBoot1AutoConfiguration.class);
	context.refresh();
	MessagingService messagingService = context.getBean(MessagingService.class);
	checkEmail(messagingService);
	checkSms(messagingService);
	OghamInternalAssertions.assertThat(messagingService)
		.sendGrid()
			.apiKey(equalTo("ogham"))
			.client(allOf(isA(SendGrid.class), not(isSpringBeanInstance(context, SendGrid.class))))
			.and()
		.thymeleaf()
			.all()
				.engine(isA(TemplateEngine.class))
				.and()
			.and()
		.freemarker()
			.all()
				.configuration()
					.defaultEncoding(equalTo(StandardCharsets.US_ASCII.name()));
}
 
@Test
public void oghamInWebContext() throws Exception {
	context.register(WebMvcAutoConfiguration.class, OghamSpringBoot1AutoConfiguration.class);
	context.refresh();
	MessagingService messagingService = context.getBean(MessagingService.class);
	checkEmail(messagingService);
	checkSms(messagingService);
	OghamInternalAssertions.assertThat(messagingService)
		.sendGrid()
			.apiKey(equalTo("ogham"))
			.client(allOf(isA(SendGrid.class), not(isSpringBeanInstance(context, SendGrid.class))))
			.and()
		.thymeleaf()
			.all()
				.engine(isA(TemplateEngine.class))
				.and()
			.and()
		.freemarker()
			.all()
				.configuration()
					.defaultEncoding(equalTo(StandardCharsets.US_ASCII.name()));
}
 
源代码6 项目: ogham   文件: StringContentHandler.java
/**
 * Reads the content and adds it into the email. This method is expected to
 * update the content of the {@code email} parameter.
 * 
 * While the method signature accepts any {@link Content} instance as
 * parameter, the method will fail if anything other than a
 * {@link StringContent} is provided.
 * 
 * @param email
 *            the email to put the content in
 * @param content
 *            the unprocessed content
 * @throws ContentHandlerException
 *             the handler is unable to add the content to the email
 * @throws IllegalArgumentException
 *             the content provided is not of the right type
 */
@Override
public void setContent(final Email original, final SendGrid.Email email, final Content content) throws ContentHandlerException {
	if (email == null) {
		throw new IllegalArgumentException("[email] cannot be null");
	}
	if (content == null) {
		throw new IllegalArgumentException("[content] cannot be null");
	}

	if (content instanceof MayHaveStringContent && ((MayHaveStringContent) content).canProvideString()) {
		final String contentStr = ((MayHaveStringContent) content).asString();

		try {
			final String mime = mimeProvider.detect(contentStr).toString();
			LOG.debug("Email content has detected type {}", mime);
			LOG.trace("content: {}", content);
			setMimeContent(email, contentStr, mime, content);
		} catch (MimeTypeDetectionException e) {
			throw new ContentHandlerException("Unable to set the email content", content, e);
		}
	} else {
		throw new IllegalArgumentException("This instance can only work with MayHaveStringContent instances, but was passed " + content.getClass().getSimpleName());
	}

}
 
源代码7 项目: ogham   文件: MultiContentHandler.java
/**
 * Reads the content and adds it into the email. This method is expected to
 * update the content of the {@code email} parameter.
 * 
 * While the method signature accepts any {@link Content} instance as
 * parameter, the method will fail if anything other than a
 * {@link MultiContent} is provided.
 * 
 * @param email
 *            the email to put the content in
 * @param content
 *            the unprocessed content
 * @throws ContentHandlerException
 *             the handler is unable to add the content to the email
 * @throws IllegalArgumentException
 *             the content provided is not of the right type
 */
@Override
public void setContent(final Email original, final SendGrid.Email email, final Content content) throws ContentHandlerException {
	if (email == null) {
		throw new IllegalArgumentException("[email] cannot be null");
	}
	if (content == null) {
		throw new IllegalArgumentException("[content] cannot be null");
	}

	if (content instanceof MultiContent) {
		for (Content subContent : ((MultiContent) content).getContents()) {
			delegate.setContent(original, email, subContent);
		}
	} else {
		throw new IllegalArgumentException("This instance can only work with MultiContent instances, but was passed " + content.getClass().getSimpleName());
	}
}
 
源代码8 项目: ogham   文件: PriorizedContentHandler.java
@Override
public void setContent(final Email original, final SendGrid.Email email, final Content content) throws ContentHandlerException {
	if (email == null) {
		throw new IllegalArgumentException("[email] cannot be null");
	}
	if (content == null) {
		throw new IllegalArgumentException("[content] cannot be null");
	}

	final Class<?> clazz = content.getClass();
	LOG.debug("Getting content handler for type {}", clazz);
	final SendGridContentHandler handler = findHandler(content);
	if (handler == null) {
		LOG.warn("No content handler found for requested type {}", clazz);
		throw new ContentHandlerException("No content handler found for content type " + clazz.getSimpleName(), content);
	} else {
		handler.setContent(original, email, content);
	}
}
 
源代码9 项目: ogham   文件: SendGridTranslationTest.java
@Test
public void forBasicTextEmail() throws MessagingException, SendGridException {
	// @formatter:off
	final Email email = new Email()
								.subject(SUBJECT)
								.content(CONTENT_TEXT)
								.from(new EmailAddress(FROM_ADDRESS, FROM))
								.to(new EmailAddress(TO_ADDRESS_1, TO));
	// @formatter:on

	messagingService.send(email);

	final ArgumentCaptor<SendGrid.Email> argument = ArgumentCaptor.forClass(SendGrid.Email.class);
	verify(sendGridClient).send(argument.capture());
	final SendGrid.Email val = argument.getValue();

	assertEquals(SUBJECT, val.getSubject());
	assertEquals(FROM, val.getFromName());
	assertEquals(FROM_ADDRESS, val.getFrom());
	assertEquals(TO, val.getToNames()[0]);
	assertEquals(TO_ADDRESS_1, val.getTos()[0]);
	assertEquals(CONTENT_TEXT, val.getText());
}
 
源代码10 项目: ogham   文件: SendGridTranslationTest.java
@Test
public void forAccentedTextEmail() throws MessagingException, SendGridException {
	// @formatter:off
	final Email email = new Email()
								.subject(SUBJECT)
								.content(new StringContent(CONTENT_TEXT_ACCENTED))
								.from(new EmailAddress(FROM_ADDRESS, FROM))
								.to(new EmailAddress(TO_ADDRESS_1, TO));
	// @formatter:on

	messagingService.send(email);

	final ArgumentCaptor<SendGrid.Email> argument = ArgumentCaptor.forClass(SendGrid.Email.class);
	verify(sendGridClient).send(argument.capture());
	final SendGrid.Email val = argument.getValue();

	assertEquals(SUBJECT, val.getSubject());
	assertEquals(FROM, val.getFromName());
	assertEquals(FROM_ADDRESS, val.getFrom());
	assertEquals(TO, val.getToNames()[0]);
	assertEquals(TO_ADDRESS_1, val.getTos()[0]);
	assertEquals(CONTENT_TEXT_ACCENTED, val.getText());
}
 
源代码11 项目: ogham   文件: SendGridTranslationTest.java
@Test
public void forBasicHtmlEmail() throws MessagingException, SendGridException {
	// @formatter:off
	final Email email = new Email()
								.subject(SUBJECT)
								.content(new StringContent(CONTENT_HTML))
								.from(new EmailAddress(FROM_ADDRESS, FROM))
								.to(new EmailAddress(TO_ADDRESS_1, TO));
	// @formatter:on

	messagingService.send(email);

	final ArgumentCaptor<SendGrid.Email> argument = ArgumentCaptor.forClass(SendGrid.Email.class);
	verify(sendGridClient).send(argument.capture());
	final SendGrid.Email val = argument.getValue();

	assertEquals(SUBJECT, val.getSubject());
	assertEquals(FROM, val.getFromName());
	assertEquals(FROM_ADDRESS, val.getFrom());
	assertEquals(TO, val.getToNames()[0]);
	assertEquals(TO_ADDRESS_1, val.getTos()[0]);
	assertEquals(CONTENT_HTML, val.getHtml());
}
 
源代码12 项目: ogham   文件: SendGridTranslationTest.java
@Test
public void forTemplatedTextEmail() throws MessagingException, SendGridException {
	// @formatter:off
	final Email email = new Email()
								.subject(SUBJECT)
								.content(new TemplateContent("string:" + CONTENT_TEXT_TEMPLATE, new SimpleContext("name", NAME)))
								.from(new EmailAddress(FROM_ADDRESS, FROM))
								.to(new EmailAddress(TO_ADDRESS_1, TO), new EmailAddress(TO_ADDRESS_2, TO));
	// @formatter:on

	messagingService.send(email);

	final ArgumentCaptor<SendGrid.Email> argument = ArgumentCaptor.forClass(SendGrid.Email.class);
	verify(sendGridClient).send(argument.capture());
	final SendGrid.Email val = argument.getValue();

	assertEquals(SUBJECT, val.getSubject());
	assertEquals(FROM, val.getFromName());
	assertEquals(FROM_ADDRESS, val.getFrom());
	assertArrayEquals(new String[] { TO, TO }, val.getToNames());
	assertArrayEquals(new String[] { TO_ADDRESS_1, TO_ADDRESS_2 }, val.getTos());
	assertEquals(CONTENT_TEXT_RESULT, val.getText());
}
 
源代码13 项目: ogham   文件: SendGridTranslationTest.java
@Test
public void forTemplatedHtmlEmail() throws MessagingException, SendGridException {
	// @formatter:off
	final Email email = new Email()
								.subject(SUBJECT)
								.content(new TemplateContent("string:" + CONTENT_HTML_TEMPLATE, new SimpleContext("name", NAME)))
								.from(new EmailAddress(FROM_ADDRESS, FROM))
								.to(new EmailAddress(TO_ADDRESS_1, TO), new EmailAddress(TO_ADDRESS_2, TO));
	// @formatter:on

	messagingService.send(email);

	final ArgumentCaptor<SendGrid.Email> argument = ArgumentCaptor.forClass(SendGrid.Email.class);
	verify(sendGridClient).send(argument.capture());
	final SendGrid.Email val = argument.getValue();

	assertEquals(SUBJECT, val.getSubject());
	assertEquals(FROM, val.getFromName());
	assertEquals(FROM_ADDRESS, val.getFrom());
	assertArrayEquals(new String[] { TO, TO }, val.getToNames());
	assertArrayEquals(new String[] { TO_ADDRESS_1, TO_ADDRESS_2 }, val.getTos());
	assertEquals(CONTENT_HTML_RESULT, val.getHtml());
}
 
源代码14 项目: iaf   文件: SendGridSender.java
@Override
public void open() throws SenderException {
	super.open();
	httpclient.open();

	CloseableHttpClient httpClient = httpclient.getHttpClient();
	if(httpClient == null)
		throw new SenderException("no HttpClient found, did it initialize properly?");

	Client client = new Client(httpClient);
	sendGrid = new SendGrid(getCredentialFactory().getPassword(), client);
}
 
源代码15 项目: arcusplatform   文件: EmailProvider.java
@Inject
public EmailProvider(@Named("email.provider.apikey") String sendGridApiKey, PersonDAO personDao, PlaceDAO placeDao, AccountDAO accountDao, NotificationMessageRenderer messageRenderer, UpstreamNotificationResponder responder) {
    this.sendGrid = new SendGrid(sendGridApiKey);
    

    this.personDao = personDao;
    this.placeDao = placeDao;
    this.accountDao = accountDao;
    this.messageRenderer = messageRenderer;
    this.responder = responder;
}
 
源代码16 项目: ogham   文件: OghamPropertiesOnlyTest.java
@Test
public void sendGridPropertiesDefinedInAppPropertiesOrInSystemPropertiesShouldOverrideOghamDefaultProperties() throws IllegalAccessException {
	SendGridV4Sender sender = builder.email().sender(SendGridV4Builder.class).build();
	DelegateSendGridClient delegate = (DelegateSendGridClient) sender.getDelegate();
	SendGrid sendGrid = (SendGrid) FieldUtils.readField(delegate, "delegate", true);
	assertThat(SendGridUtils.getApiKey(sendGrid), equalTo("ogham"));
	assertThat(sendGrid, not(sameInstance(springSendGridClient)));
}
 
源代码17 项目: ogham   文件: OghamPropertiesPrecedenceTest.java
@Test
public void sendGridOghamSpecificPropertiesShouldNotOverrideSpringPropertiesDueToSpringSendGridClientInstance() throws IllegalAccessException {
	SendGridV4Sender sender = builder.email().sender(SendGridV4Builder.class).build();
	DelegateSendGridClient delegate = (DelegateSendGridClient) sender.getDelegate();
	SendGrid sendGrid = (SendGrid) FieldUtils.readField(delegate, "delegate", true);
	assertThat(SendGridUtils.getApiKey(sendGrid), equalTo("spring"));
	assertThat(springSendGridClient, notNullValue());
	assertThat(sendGrid, sameInstance(springSendGridClient));
}
 
源代码18 项目: ogham   文件: SpringPropertiesOnlyTest.java
@Test
public void sendGridPropertiesDefinedInAppPropertiesOrInSystemPropertiesShouldOverrideOghamDefaultProperties() throws IllegalAccessException {
	SendGridV4Sender sender = builder.email().sender(SendGridV4Builder.class).build();
	DelegateSendGridClient delegate = (DelegateSendGridClient) sender.getDelegate();
	SendGrid sendGrid = (SendGrid) FieldUtils.readField(delegate, "delegate", true);
	assertThat(SendGridUtils.getApiKey(sendGrid), equalTo("spring"));
	assertThat(springSendGridClient, notNullValue());
	assertThat(sendGrid, sameInstance(springSendGridClient));
}
 
源代码19 项目: ogham   文件: SendGridUtils.java
@SuppressWarnings("unchecked")
public static String getApiKey(SendGrid sendGrid) throws IllegalAccessException {
	try {
		return (String) readField(sendGrid, "apiKey", true);
	} catch (IllegalArgumentException e) {
		Map<String, String> requestHeaders = (Map<String, String>) readField(sendGrid, "requestHeaders", true);
		String authHeader = requestHeaders.get("Authorization");
		if (authHeader == null) {
			return null;
		}
		return authHeader.substring(7);
	}
}
 
源代码20 项目: ogham   文件: SendGridV4Builder.java
private SendGrid buildSendGrid(String apiKey, Client client, URL url) {
	SendGrid sendGrid = newSendGrid(apiKey, client);
	if (url != null) {
		sendGrid.setHost(url.getHost());
	}
	return sendGrid;
}
 
源代码21 项目: ogham   文件: SendGridAssertions.java
private static String getApiKey(SendGridSender sendGridSender) {
	SendGrid client = getClient(sendGridSender);
	try {
		if (sendGridSender instanceof SendGridV2Sender) {
			return (String) readField(client, "password", true);
		}
		return getApiKeyFromFieldOrHeaders(client);
	} catch (IllegalAccessException e) {
		throw new IllegalStateException("Failed to read 'apiKey' of SendGrid", e);
	}
}
 
源代码22 项目: ogham   文件: OghamSendGridV4Configuration.java
@Bean
@ConditionalOnMissingBean(AbstractSpringSendGridConfigurer.class)
public AbstractSpringSendGridConfigurer springSendGridConfigurer(
		@Autowired(required=false) OghamSendGridProperties properties,
		@Autowired(required=false) SendGridProperties springProperties,
		@Autowired(required=false) SendGrid sendGrid) {
	return new SpringSendGridV4Configurer(properties, springProperties, sendGrid);
}
 
@Test
public void oghamWithSendGridAutoConfigShouldUseSpringSendGridClient() throws Exception {
	contextRunner = contextRunner.withPropertyValues("ogham.email.sendgrid.api-key=ogham");
	contextRunner = contextRunner.withConfiguration(of(SendGridAutoConfiguration.class, OghamSpringBoot2AutoConfiguration.class));
	contextRunner.run((context) -> {
		MessagingService messagingService = context.getBean(MessagingService.class);
		checkEmail(messagingService);
		checkSms(messagingService);
		OghamInternalAssertions.assertThat(messagingService)
			.sendGrid()
				.apiKey(equalTo("spring"))
				.client(isSpringBeanInstance(context, SendGrid.class));
	});
}
 
@Test
public void oghamWithoutSendGridAutoConfigShouldUseOghamSendGridClientWithOghamProperties() throws Exception {
	contextRunner = contextRunner.withPropertyValues("ogham.email.sendgrid.api-key=ogham");
	contextRunner = contextRunner.withConfiguration(of(ManuallyEnableSendGridPropertiesConfiguration.class, OghamSpringBoot2AutoConfiguration.class));
	contextRunner.run((context) -> {
		MessagingService messagingService = context.getBean(MessagingService.class);
		checkEmail(messagingService);
		checkSms(messagingService);
		OghamInternalAssertions.assertThat(messagingService)
			.sendGrid()
				.apiKey(equalTo("ogham"))
				.client(not(isSpringBeanInstance(context, SendGrid.class)));
	});
}
 
@Test
public void oghamWithoutSendGridAutoConfigWithoutOghamPropertiesShouldUseOghamSendGridClientWithSpringProperties() throws Exception {
	contextRunner = contextRunner.withConfiguration(of(ManuallyEnableSendGridPropertiesConfiguration.class, OghamSpringBoot2AutoConfiguration.class));
	contextRunner.run((context) -> {
		MessagingService messagingService = context.getBean(MessagingService.class);
		checkEmail(messagingService);
		checkSms(messagingService);
		OghamInternalAssertions.assertThat(messagingService)
			.sendGrid()
				.apiKey(equalTo("spring"))
				.client(not(isSpringBeanInstance(context, SendGrid.class)));
	});
}
 
@Test
public void useCustomSendGridBean() throws Exception {
	contextRunner = contextRunner.withPropertyValues("ogham.email.sendgrid.api-key=ogham");
	contextRunner = contextRunner.withConfiguration(of(CustomSendGridConfig.class, SendGridAutoConfiguration.class, OghamSpringBoot2AutoConfiguration.class));
	contextRunner.run((context) -> {
		MessagingService messagingService = context.getBean(MessagingService.class);
		checkEmail(messagingService);
		checkSms(messagingService);
		OghamInternalAssertions.assertThat(messagingService)
			.sendGrid()
				.apiKey(nullValue(String.class))
				.client(allOf(isA(CustomSendGrid.class), isSpringBeanInstance(context, SendGrid.class)));
	});
}
 
源代码27 项目: ogham   文件: OghamSendGridV2Configuration.java
@Bean
@ConditionalOnMissingBean(AbstractSpringSendGridConfigurer.class)
public AbstractSpringSendGridConfigurer springSendGridConfigurer(
		@Autowired(required=false) OghamSendGridProperties properties,
		@Autowired(required=false) SendGridProperties springProperties,
		@Autowired(required=false) SendGrid sendGrid) {
	return new SpringSendGridV2Configurer(properties, springProperties, sendGrid);
}
 
@Test
public void oghamWithSendGridAutoConfigShouldUseSpringSendGridClient() throws Exception {
	EnvironmentTestUtils.addEnvironment(context, "ogham.email.sendgrid.api-key=ogham");
	context.register(SendGridAutoConfiguration.class, OghamSpringBoot1AutoConfiguration.class);
	context.refresh();
	
	MessagingService messagingService = context.getBean(MessagingService.class);
	
	checkEmail(messagingService);
	checkSms(messagingService);
	OghamInternalAssertions.assertThat(messagingService)
		.sendGrid()
			.apiKey(equalTo("spring"))
			.client(isSpringBeanInstance(context, SendGrid.class));
}
 
@Test
public void oghamWithoutSendGridAutoConfigShouldUseOghamSendGridClientWithOghamProperties() throws Exception {
	EnvironmentTestUtils.addEnvironment(context, "ogham.email.sendgrid.api-key=ogham");
	context.register(ManuallyEnableSendGridPropertiesConfiguration.class, OghamSpringBoot1AutoConfiguration.class);
	context.refresh();
	
	MessagingService messagingService = context.getBean(MessagingService.class);
	
	checkEmail(messagingService);
	checkSms(messagingService);
	OghamInternalAssertions.assertThat(messagingService)
		.sendGrid()
			.apiKey(equalTo("ogham"))
			.client(not(isSpringBeanInstance(context, SendGrid.class)));
}
 
@Test
public void oghamWithoutSendGridAutoConfigWithoutOghamPropertiesShouldUseOghamSendGridClientWithSpringProperties() throws Exception {
	context.register(ManuallyEnableSendGridPropertiesConfiguration.class, OghamSpringBoot1AutoConfiguration.class);
	context.refresh();
	
	MessagingService messagingService = context.getBean(MessagingService.class);
	
	checkEmail(messagingService);
	checkSms(messagingService);
	OghamInternalAssertions.assertThat(messagingService)
		.sendGrid()
			.apiKey(equalTo("spring"))
			.client(not(isSpringBeanInstance(context, SendGrid.class)));
}