Skip to content

Commit

Permalink
Resolves ZSTAC-10964
Browse files Browse the repository at this point in the history
Signed-off-by: Qun Li <[email protected]>
  • Loading branch information
live4thee committed May 3, 2018
1 parent da93fd9 commit b6867fb
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
3 changes: 2 additions & 1 deletion conf/zstack-servlet-context.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
</beans>
</beans>
4 changes: 2 additions & 2 deletions core/src/main/java/org/zstack/core/webhook/WebhookCaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.zstack.core.retry.Retry;
import org.zstack.core.retry.RetryCondition;
import org.zstack.header.core.webhooks.WebhookInventory;
import org.zstack.header.rest.RESTConstant;
import org.zstack.header.rest.RESTFacade;
import org.zstack.utils.Utils;
import org.zstack.utils.logging.CLogger;
Expand All @@ -28,11 +29,10 @@ public abstract class WebhookCaller {
(int)TimeUnit.SECONDS.toMillis(30)
);


protected void postToWebhooks(List<WebhookInventory> hooks, String body) {
for (WebhookInventory hook : hooks) {
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(MediaType.APPLICATION_JSON);
requestHeaders.setContentType(MediaType.valueOf(RESTConstant.APP_JSON_UTF8));
requestHeaders.setContentLength(body.length());
HttpEntity<String> req = new HttpEntity<String>(body, requestHeaders);

Expand Down
2 changes: 2 additions & 0 deletions header/src/main/java/org/zstack/header/rest/RESTConstant.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ public interface RESTConstant {
public static final String REST_API_CALL = "/api";
static final String COMMAND_PATH = "commandpath";
public static final String DEFAULT_PARAMETER_NAME = "";

public static final String APP_JSON_UTF8 = "application/json; charset=utf-8";
}
16 changes: 15 additions & 1 deletion header/src/main/java/org/zstack/header/rest/RESTFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import org.springframework.http.HttpEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import org.zstack.header.core.Completion;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -75,6 +77,18 @@ static TimeoutRestTemplate createRestTemplate(int readTimeout, int connectTimeou
HttpComponentsClientHttpRequestFactory factory = new TimeoutHttpComponentsClientHttpRequestFactory();
factory.setReadTimeout(readTimeout);
factory.setConnectTimeout(connectTimeout);
return new TimeoutRestTemplate(factory);
TimeoutRestTemplate template = new TimeoutRestTemplate(factory);

StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
stringHttpMessageConverter.setWriteAcceptCharset(true);
for (int i = 0; i < template.getMessageConverters().size(); i++) {
if (template.getMessageConverters().get(i) instanceof StringHttpMessageConverter) {
template.getMessageConverters().remove(i);
template.getMessageConverters().add(i, stringHttpMessageConverter);
break;
}
}

return template;
}
}

0 comments on commit b6867fb

Please sign in to comment.