You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brooklyn.apache.org by ygy <gi...@git.apache.org> on 2016/08/03 08:05:28 UTC
[GitHub] brooklyn-server pull request #285: [WIP] Adding HttpExecutor to be used for ...
GitHub user ygy opened a pull request:
https://github.com/apache/brooklyn-server/pull/285
[WIP] Adding HttpExecutor to be used for interchangeable implementations
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/ygy/brooklyn-server feature/http-executor
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/brooklyn-server/pull/285.patch
To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:
This closes #285
----
commit 45f186dfad51d1106116975d8d6aaa12ff49da2c
Author: Yavor Yanchev <ya...@yanchev.com>
Date: 2016-08-02T15:24:19Z
Adding HttpExecutor to be used for interchangeable implementations
----
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by bostko <gi...@git.apache.org>.
Github user bostko commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r76582718
--- Diff: core/src/main/java/org/apache/brooklyn/util/http/executor/HttpExecutorFactoryImpl.java ---
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.util.http.executor;
+
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.apache.brooklyn.util.collections.MutableMap;
+import org.apache.brooklyn.util.core.ClassLoaderUtils;
+import org.apache.brooklyn.util.core.flags.TypeCoercions;
+import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.http.executor.apacheclient.HttpExecutorImpl;
+import org.apache.brooklyn.util.text.StringPredicates;
+import org.apache.brooklyn.util.text.Strings;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.collect.Maps;
+
+public class HttpExecutorFactoryImpl implements HttpExecutorFactory {
+ private static final Logger LOG = LoggerFactory.getLogger(HttpExecutorFactoryImpl.class);
+
+ public HttpExecutorFactoryImpl() {
+ // no-op
+ }
+
+ @Override
+ public HttpExecutor getHttpExecutor(Map<?, ?> props) {
+ HttpExecutor httpExecutor;
+
+ String httpExecutorClass = (String) props.get(HTTP_EXECUTOR_CLASS);
+ if (httpExecutorClass != null) {
+ Map<?, ?> executorProps = Maps.filterKeys(props, StringPredicates.isStringStartingWith(HTTP_EXECUTOR_CLASS_CONFIG_PREFIX));
+ if (executorProps.size() > 0) {
+ Map<String, String> httpExecutorProps = MutableMap.of();
+ for (Entry<?, ?> entry: executorProps.entrySet()) {
+ String keyName = Strings.removeFromStart((String)entry.getKey(), HTTP_EXECUTOR_CLASS_CONFIG_PREFIX);
+ httpExecutorProps.put(keyName, TypeCoercions.coerce(entry.getValue(), String.class));
+ }
+
+ try {
+ httpExecutor = (HttpExecutor) new ClassLoaderUtils(HttpExecutorFactoryImpl.class).loadClass(httpExecutorClass).getConstructor(Map.class).newInstance(httpExecutorProps);
--- End diff --
Shouldn't this come from location extension?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by bostko <gi...@git.apache.org>.
Github user bostko commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r77455765
--- Diff: utils/common/src/main/java/org/apache/brooklyn/util/http/executor/HttpRequest.java ---
@@ -0,0 +1,130 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.util.http.executor;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.net.URI;
+import java.util.Map;
+
+import javax.annotation.Nullable;
+
+import com.google.common.annotations.Beta;
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.Multimap;
+
+@Beta
+public interface HttpRequest {
+
+ // TODO Should we use InputStream for body (rather than byte[]). That would mean we don't have
+ // to hold the entire body in-memory, which might be important for really big payloads!
+
+ @Beta
+ public static class Builder {
--- End diff --
Minor: Doesn't make sence to have a builder for an implementation in the interface.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:
https://github.com/apache/brooklyn-server/pull/285
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: [WIP] Adding HttpExecutor to be used for ...
Posted by aledsage <gi...@git.apache.org>.
Github user aledsage commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r73674452
--- Diff: utils/common/src/main/java/org/apache/brooklyn/util/http/HttpExecutor.java ---
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.util.http;
+
+import org.apache.http.ConnectionReuseStrategy;
+import org.apache.http.client.CredentialsProvider;
+import org.apache.http.client.HttpClient;
+import org.apache.http.impl.client.LaxRedirectStrategy;
+import org.apache.http.params.HttpParams;
+
+@SuppressWarnings("deprecation")
+public interface HttpExecutor extends HttpClient {
+
+ void setParams(HttpParams httpParams);
+
+ void setRedirectStrategy(LaxRedirectStrategy laxRedirectStrategy);
--- End diff --
I'm not convinced we want to tie this interface to the `org.apache.http` classes. I was assuming we'd make it more general. And that we would not extend `HttpClient`, which would presumably make it a lot harder to supply implementations which do not use `org.apache.http`.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: [WIP] Adding HttpExecutor to be used for ...
Posted by aledsage <gi...@git.apache.org>.
Github user aledsage commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r73674530
--- Diff: utils/common/src/main/java/org/apache/brooklyn/util/http/HttpExecutorFactory.java ---
@@ -0,0 +1,25 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.util.http;
+
+public class HttpExecutorFactory {
--- End diff --
I was assuming this would be an interface, with an implementation that used the `org.apache.http` client.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r77506454
--- Diff: utils/common/src/main/java/org/apache/brooklyn/util/http/executor/HttpRequest.java ---
@@ -0,0 +1,130 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.util.http.executor;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.net.URI;
+import java.util.Map;
+
+import javax.annotation.Nullable;
+
+import com.google.common.annotations.Beta;
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.Multimap;
+
+@Beta
+public interface HttpRequest {
+
+ // TODO Should we use InputStream for body (rather than byte[]). That would mean we don't have
+ // to hold the entire body in-memory, which might be important for really big payloads!
+
+ @Beta
+ public static class Builder {
+ protected URI uri;
+ protected String method;
+ protected byte[] body;
+ protected Multimap<String, String> headers = ArrayListMultimap.<String, String>create();
+ protected Credentials.BasicAuth credentials;
+ protected HttpConfig config;
+
+ public Builder uri(URI val) {
+ uri = checkNotNull(val, "uri");
+ return this;
+ }
+
+ public Builder method(String val) {
+ method = checkNotNull(val, "method");
+ return this;
+ }
+
+ /**
+ * This val must not be modified after being passed in - the object will be used while executing the request.
+ */
+ public Builder body(@Nullable byte[] val) {
+ body = val;
+ return this;
+ }
+
+ public Builder headers(Multimap<String, String> val) {
+ headers.putAll(checkNotNull(val, "headers"));
+ return this;
+ }
+
+ public Builder headers(Map<String, String> val) {
+ if (checkNotNull(val, "headers").keySet().contains(null)) {
+ throw new NullPointerException("Headers must not contain null key");
+ }
+ for (Map.Entry<String, String> entry : val.entrySet()) {
+ header(entry.getKey(), entry.getValue());
+ }
+ return this;
+ }
+
+ public Builder header(String key, String val) {
+ headers.put(checkNotNull(key, "key"), val);
+ return this;
+ }
+
+ public Builder credentials(@Nullable Credentials.BasicAuth val) {
+ credentials = val;
+ return this;
+ }
+
+ public Builder config(@Nullable HttpConfig val) {
+ config = val;
+ return this;
+ }
+
+ public Builder from(HttpRequestImpl httpRequest) {
+ this.uri = checkNotNull(httpRequest.uri, "uri");
+ this.method = checkNotNull(httpRequest.method, "method");
+ this.body = httpRequest.body;
+ this.headers = ArrayListMultimap.create(checkNotNull(httpRequest.headers, "headers"));
+ this.credentials = httpRequest.credentials;
+ this.config = httpRequest.config;
+ return this;
+ }
+
+ public HttpRequest build() {
+ return new HttpRequestImpl(this);
+ }
+ }
+
+ URI uri();
+
+ String method();
+
+ /**
+ * The payload of the request, or null if no payload (e.g. for GET requests).
+ */
+ @Nullable
+ byte[] body();
+
+ Multimap<String, String> headers();
+
+ @Nullable
+ Credentials.BasicAuth credentials();
--- End diff --
Don't like how `BasicAuth` is hardcoded in the API. Username + password could be used for digest or a number of other schemes. The credentials are (mostly) orthogonal to the scheme.
* Rename `BasicAuth` to `UsernamePassword(Credentials)`.
* Change Credentials to interface (with getUser, getPassword method) and implement it in the above.
* Use Credentials in the API.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by bostko <gi...@git.apache.org>.
Github user bostko commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r77345487
--- Diff: utils/common/src/main/java/org/apache/brooklyn/util/http/HttpTool.java ---
@@ -250,6 +254,21 @@ public static HttpClientBuilder httpClientBuilder() {
private boolean trustAll;
private boolean trustSelfSigned;
+ public static HttpClientBuilder fromBuilder(HttpClientBuilder other) {
+ HttpClientBuilder result = httpClientBuilder();
+ result.clientConnectionManager = other.clientConnectionManager;
+ result.httpParams = other.httpParams;
--- End diff --
`httpParams` is leftover code which is deprecated so it can be removed now but it is ok to remove it later as well.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by ygy <gi...@git.apache.org>.
Github user ygy commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r77386246
--- Diff: core/src/main/java/org/apache/brooklyn/feed/http/HttpFeed.java ---
@@ -331,52 +365,54 @@ protected void preStart() {
}
Callable<HttpToolResponse> pollJob;
-
- if (pollInfo.method.equals("get")) {
- pollJob = new Callable<HttpToolResponse>() {
- public HttpToolResponse call() throws Exception {
- if (log.isTraceEnabled()) log.trace("http polling for {} sensors at {}", entity, pollInfo);
- return HttpTool.httpGet(httpClient, pollInfo.uriProvider.get(), pollInfo.headers);
- }};
- } else if (pollInfo.method.equals("post")) {
- pollJob = new Callable<HttpToolResponse>() {
- public HttpToolResponse call() throws Exception {
- if (log.isTraceEnabled()) log.trace("http polling for {} sensors at {}", entity, pollInfo);
- return HttpTool.httpPost(httpClient, pollInfo.uriProvider.get(), pollInfo.headers, pollInfo.body);
- }};
- } else if (pollInfo.method.equals("head")) {
- pollJob = new Callable<HttpToolResponse>() {
- public HttpToolResponse call() throws Exception {
- if (log.isTraceEnabled()) log.trace("http polling for {} sensors at {}", entity, pollInfo);
- return HttpTool.httpHead(httpClient, pollInfo.uriProvider.get(), pollInfo.headers);
- }};
- } else {
- throw new IllegalStateException("Unexpected http method: "+pollInfo.method);
- }
-
- getPoller().scheduleAtFixedRate(pollJob, new DelegatingPollHandler<HttpToolResponse>(handlers), minPeriod);
- }
- }
+ pollJob = new Callable<HttpToolResponse>() {
+ public HttpToolResponse call() throws Exception {
+ if (log.isTraceEnabled()) log.trace("http polling for {} sensors at {}", entity, pollInfo);
- // TODO Should we really trustAll for https? Make configurable?
- private HttpClient createHttpClient(HttpPollIdentifier pollIdentifier) {
- URI uri = pollIdentifier.uriProvider.get();
- HttpClientBuilder builder = HttpTool.httpClientBuilder()
- .trustAll()
- .laxRedirect(true);
- if (uri != null) builder.uri(uri);
- if (uri != null) builder.credential(pollIdentifier.credentials);
- if (pollIdentifier.connectionTimeout != null) {
- builder.connectionTimeout(pollIdentifier.connectionTimeout);
- }
- if (pollIdentifier.socketTimeout != null) {
- builder.socketTimeout(pollIdentifier.socketTimeout);
+ BasicAuth creds = null;
+ if (pollInfo.credentials.isPresent()) {
+ creds = org.apache.brooklyn.util.http.executor.Credentials.basic(
+ pollInfo.credentials.get().getUserPrincipal().getName(),
+ pollInfo.credentials.get().getPassword());
+ }
+
+ HttpResponse response = pollInfo.httpExecutor.execute(new HttpRequest.Builder()
+ .headers(pollInfo.headers)
+ .uri(pollInfo.uriProvider.get())
+ .credentials(creds)
+ .method(pollInfo.method)
+ .body(pollInfo.body)
+ .config(HttpConfig.builder()
+ .trustSelfSigned(true)
+ .trustAll(true)
+ .laxRedirect(true)
+ .build())
+ .build());
+ return createHttpToolRespose(response);
+ }};
+ getPoller().scheduleAtFixedRate(pollJob, new DelegatingPollHandler(handlers), minPeriod);
}
- return builder.build();
}
@SuppressWarnings("unchecked")
protected Poller<HttpToolResponse> getPoller() {
- return (Poller<HttpToolResponse>) super.getPoller();
+ return (Poller<HttpToolResponse>) super.getPoller();
+ }
+
+ @SuppressWarnings("unchecked")
+ private HttpToolResponse createHttpToolRespose(HttpResponse response) throws IOException {
+ int responseCode = response.code();
+ if (responseCode == 400) { // Unprocessable Entity - https://stackoverflow.com/questions/6123425/rest-response-code-for-invalid-data
+ throw new IOException(" Unprocessable Entity: " + response.reasonPhrase());
+ }
--- End diff --
Response code has to be interpreted so it is backward compatible with `HttpToolResponse` which depends on `org.apache.http.HttpResponse`.
`HttpToolResponse` checks for response code `0` in `getResponseCode()`.
You are correct about code `400` in general. HTTP code `422` will be used instead. I've put a comment from the RFC why it is used.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by bostko <gi...@git.apache.org>.
Github user bostko commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r77155611
--- Diff: core/src/main/java/org/apache/brooklyn/util/http/executor/HttpExecutorFactoryImpl.java ---
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.util.http.executor;
+
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.apache.brooklyn.util.collections.MutableMap;
+import org.apache.brooklyn.util.core.ClassLoaderUtils;
+import org.apache.brooklyn.util.core.flags.TypeCoercions;
+import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.http.executor.apacheclient.HttpExecutorImpl;
+import org.apache.brooklyn.util.text.StringPredicates;
+import org.apache.brooklyn.util.text.Strings;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.collect.Maps;
+
+public class HttpExecutorFactoryImpl implements HttpExecutorFactory {
+ private static final Logger LOG = LoggerFactory.getLogger(HttpExecutorFactoryImpl.class);
+
+ public HttpExecutorFactoryImpl() {
+ // no-op
+ }
+
+ @Override
+ public HttpExecutor getHttpExecutor(Map<?, ?> props) {
+ HttpExecutor httpExecutor;
+
+ String httpExecutorClass = (String) props.get(HTTP_EXECUTOR_CLASS);
+ if (httpExecutorClass != null) {
+ Map<?, ?> executorProps = Maps.filterKeys(props, StringPredicates.isStringStartingWith(HTTP_EXECUTOR_CLASS_CONFIG_PREFIX));
+ if (executorProps.size() > 0) {
+ Map<String, String> httpExecutorProps = MutableMap.of();
+ for (Entry<?, ?> entry: executorProps.entrySet()) {
+ String keyName = Strings.removeFromStart((String)entry.getKey(), HTTP_EXECUTOR_CLASS_CONFIG_PREFIX);
+ httpExecutorProps.put(keyName, TypeCoercions.coerce(entry.getValue(), String.class));
+ }
+
+ try {
+ httpExecutor = (HttpExecutor) new ClassLoaderUtils(HttpExecutorFactoryImpl.class).loadClass(httpExecutorClass).getConstructor(Map.class).newInstance(httpExecutorProps);
--- End diff --
Better use `getClass()` to highlight that you are using the class from `this`.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server issue #285: Adding HttpExecutor to be used for interchangeab...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on the issue:
https://github.com/apache/brooklyn-server/pull/285
Almost there, only concerned about the content length being available on responses.
I'd like to see tests covering the extensions config key in jclouds and byon locations - can be done in another PR.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by iyovcheva <gi...@git.apache.org>.
Github user iyovcheva commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r76260732
--- Diff: core/src/main/java/org/apache/brooklyn/feed/http/HttpFeed.java ---
@@ -331,52 +365,45 @@ protected void preStart() {
}
Callable<HttpToolResponse> pollJob;
-
- if (pollInfo.method.equals("get")) {
- pollJob = new Callable<HttpToolResponse>() {
- public HttpToolResponse call() throws Exception {
- if (log.isTraceEnabled()) log.trace("http polling for {} sensors at {}", entity, pollInfo);
- return HttpTool.httpGet(httpClient, pollInfo.uriProvider.get(), pollInfo.headers);
- }};
- } else if (pollInfo.method.equals("post")) {
- pollJob = new Callable<HttpToolResponse>() {
- public HttpToolResponse call() throws Exception {
- if (log.isTraceEnabled()) log.trace("http polling for {} sensors at {}", entity, pollInfo);
- return HttpTool.httpPost(httpClient, pollInfo.uriProvider.get(), pollInfo.headers, pollInfo.body);
- }};
- } else if (pollInfo.method.equals("head")) {
- pollJob = new Callable<HttpToolResponse>() {
- public HttpToolResponse call() throws Exception {
- if (log.isTraceEnabled()) log.trace("http polling for {} sensors at {}", entity, pollInfo);
- return HttpTool.httpHead(httpClient, pollInfo.uriProvider.get(), pollInfo.headers);
- }};
- } else {
- throw new IllegalStateException("Unexpected http method: "+pollInfo.method);
- }
-
- getPoller().scheduleAtFixedRate(pollJob, new DelegatingPollHandler<HttpToolResponse>(handlers), minPeriod);
- }
- }
+ pollJob = new Callable<HttpToolResponse>() {
+ public HttpToolResponse call() throws Exception {
+ if (log.isTraceEnabled()) log.trace("http polling for {} sensors at {}", entity, pollInfo);
- // TODO Should we really trustAll for https? Make configurable?
- private HttpClient createHttpClient(HttpPollIdentifier pollIdentifier) {
- URI uri = pollIdentifier.uriProvider.get();
- HttpClientBuilder builder = HttpTool.httpClientBuilder()
- .trustAll()
- .laxRedirect(true);
- if (uri != null) builder.uri(uri);
- if (uri != null) builder.credential(pollIdentifier.credentials);
- if (pollIdentifier.connectionTimeout != null) {
- builder.connectionTimeout(pollIdentifier.connectionTimeout);
- }
- if (pollIdentifier.socketTimeout != null) {
- builder.socketTimeout(pollIdentifier.socketTimeout);
+ BasicAuth creds = null;
+ if (pollInfo.credentials.isPresent()) {
+ creds = org.apache.brooklyn.util.http.executor.Credentials.basic(
+ pollInfo.credentials.get().getUserPrincipal().getName(),
+ pollInfo.credentials.get().getPassword());
+ }
+
+ HttpResponse response = pollInfo.httpExecutor.execute(new HttpRequest.Builder()
+ .headers(pollInfo.headers)
+ .uri(pollInfo.uriProvider.get())
+ .credentials(creds)
+ .method(pollInfo.method)
+ .body(pollInfo.body)
+ .build());
+ return createHttpToolRespose(response);
+ }};
+ getPoller().scheduleAtFixedRate(pollJob, new DelegatingPollHandler(handlers), minPeriod);
}
- return builder.build();
}
@SuppressWarnings("unchecked")
protected Poller<HttpToolResponse> getPoller() {
- return (Poller<HttpToolResponse>) super.getPoller();
+ return (Poller<HttpToolResponse>) super.getPoller();
+ }
+
+ private HttpToolResponse createHttpToolRespose(HttpResponse response) throws IOException {
--- End diff --
very minor - error in syntax - "response"
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r77501984
--- Diff: core/src/main/java/org/apache/brooklyn/util/http/executor/HttpExecutorFactoryImpl.java ---
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.util.http.executor;
+
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.apache.brooklyn.util.collections.MutableMap;
+import org.apache.brooklyn.util.core.ClassLoaderUtils;
+import org.apache.brooklyn.util.core.flags.TypeCoercions;
+import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.http.executor.apacheclient.HttpExecutorImpl;
+import org.apache.brooklyn.util.text.StringPredicates;
+import org.apache.brooklyn.util.text.Strings;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.collect.Maps;
+
+public class HttpExecutorFactoryImpl implements HttpExecutorFactory {
+ private static final Logger LOG = LoggerFactory.getLogger(HttpExecutorFactoryImpl.class);
+
+ public HttpExecutorFactoryImpl() {
+ // no-op
+ }
+
+ @Override
+ public HttpExecutor getHttpExecutor(Map<?, ?> props) {
+ HttpExecutor httpExecutor;
+
+ String httpExecutorClass = (String) props.get(HTTP_EXECUTOR_CLASS);
+ if (httpExecutorClass != null) {
+ Map<?, ?> executorProps = Maps.filterKeys(props, StringPredicates.isStringStartingWith(HTTP_EXECUTOR_CLASS_CONFIG_PREFIX));
+ if (executorProps.size() > 0) {
+ Map<String, String> httpExecutorProps = MutableMap.of();
+ for (Entry<?, ?> entry: executorProps.entrySet()) {
+ String keyName = Strings.removeFromStart((String)entry.getKey(), HTTP_EXECUTOR_CLASS_CONFIG_PREFIX);
+ httpExecutorProps.put(keyName, TypeCoercions.coerce(entry.getValue(), String.class));
+ }
+
+ try {
+ httpExecutor = (HttpExecutor) new ClassLoaderUtils(getClass()).loadClass(httpExecutorClass).getConstructor(Map.class).newInstance(httpExecutorProps);
+ } catch (Exception e) {
+ throw Exceptions.propagate(e);
+ }
+ } else {
+ LOG.error("Missing parameters for: " + HTTP_EXECUTOR_CLASS);
+ throw Exceptions.propagate(new IllegalArgumentException("Missing parameters for: " + HTTP_EXECUTOR_CLASS));
--- End diff --
Some executor implementations don't need additional arguments (i.e. `HttpExecutorImpl`). Don't treat this as an error - create the requested executed with an empty map instead.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r77500841
--- Diff: core/src/main/java/org/apache/brooklyn/location/byon/ByonLocationResolver.java ---
@@ -108,6 +109,7 @@ protected ConfigBag extractConfig(Map<?,?> locationFlags, String spec, LocationR
MutableMap<String, Object> defaultProps = MutableMap.of();
defaultProps.addIfNotNull("user", user);
defaultProps.addIfNotNull("port", port);
+ defaultProps.addIfNotNull(LocationConfigKeys.EXTENSIONS.getName(), config.get(LocationConfigKeys.EXTENSIONS));
--- End diff --
After looking in more details: This adds the `EXTENSIONS` key to the machine spec. If this is skipped, i.e. the machine locations don't have the key, then wouldn't they get it from the parent location through inheritance? Why is BYON any more special than jclouds in handling this case? Is there a test which covers this case (and fails if the line is dropped)?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r77509799
--- Diff: utils/common/src/main/java/org/apache/brooklyn/util/http/executor/apacheclient/HttpExecutorImpl.java ---
@@ -0,0 +1,113 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.util.http.executor.apacheclient;
+
+import java.io.IOException;
+import java.util.Map;
+
+import org.apache.brooklyn.util.http.HttpTool;
+import org.apache.brooklyn.util.http.HttpTool.HttpClientBuilder;
+import org.apache.brooklyn.util.http.HttpToolResponse;
+import org.apache.brooklyn.util.http.executor.HttpConfig;
+import org.apache.brooklyn.util.http.executor.HttpExecutor;
+import org.apache.brooklyn.util.http.executor.HttpRequest;
+import org.apache.brooklyn.util.http.executor.HttpResponse;
+import org.apache.http.auth.Credentials;
+import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.client.HttpClient;
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.Optional;
+
+@Beta
+public class HttpExecutorImpl implements HttpExecutor {
+
+ private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
+
+ private static final HttpConfig DEFAULT_CONFIG = HttpConfig.builder()
+ .laxRedirect(false)
+ .trustAll(false)
+ .trustSelfSigned(false)
+ .build();
--- End diff --
Are these settings universally supported between clients?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r77351496
--- Diff: core/src/main/java/org/apache/brooklyn/location/byon/ByonLocationResolver.java ---
@@ -108,6 +109,7 @@ protected ConfigBag extractConfig(Map<?,?> locationFlags, String spec, LocationR
MutableMap<String, Object> defaultProps = MutableMap.of();
defaultProps.addIfNotNull("user", user);
defaultProps.addIfNotNull("port", port);
+ defaultProps.addIfNotNull(LocationConfigKeys.EXTENSIONS.getName(), config.get(LocationConfigKeys.EXTENSIONS));
--- End diff --
Don't see how this is used, remove.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by ygy <gi...@git.apache.org>.
Github user ygy closed the pull request at:
https://github.com/apache/brooklyn-server/pull/285
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r75683600
--- Diff: utils/common/src/main/java/org/apache/brooklyn/util/http/HttpExecutorFactory.java ---
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.util.http;
+
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.apache.brooklyn.util.collections.MutableMap;
+import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.http.executor.HttpExecutor;
+import org.apache.brooklyn.util.javalang.Reflections;
+import org.apache.brooklyn.util.text.StringPredicates;
+import org.apache.brooklyn.util.text.Strings;
+
+import com.google.common.base.Optional;
+import com.google.common.collect.Iterables;
+
+public class HttpExecutorFactory {
--- End diff --
Should be an interface
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server issue #285: Adding HttpExecutor to be used for interchangeab...
Posted by aledsage <gi...@git.apache.org>.
Github user aledsage commented on the issue:
https://github.com/apache/brooklyn-server/pull/285
LGTM - a few comments from @neykov that are not yet addressed. Have those been discussed?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server issue #285: Adding HttpExecutor to be used for interchangeab...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on the issue:
https://github.com/apache/brooklyn-server/pull/285
Looks like a great start.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r75688643
--- Diff: core/src/main/java/org/apache/brooklyn/feed/http/HttpFeed.java ---
@@ -275,7 +293,23 @@ public HttpFeed() {
protected HttpFeed(Builder builder) {
setConfig(ONLY_IF_SERVICE_UP, builder.onlyIfServiceUp);
Map<String,String> baseHeaders = ImmutableMap.copyOf(checkNotNull(builder.headers, "headers"));
-
+
+ HttpExecutor httpExecutor = null;
+ if (builder.httpExecutor != null) {
+ httpExecutor = builder.httpExecutor;
+ } else {
+ HttpExecutorFactory httpExecutorFactory = null;
+ Collection<Location> locations = builder.entity.getLocations();
+ Maybe<MachineLocation> location = Machines.findUniqueMachineLocation(locations, MachineLocation.class);
+ if (location.isPresent() && location.get().hasExtension(HttpExecutorFactory.class)) {
+ httpExecutorFactory = location.get().getExtension(HttpExecutorFactory.class);
+ Map<String, Object> httpExecutorProps = location.get().getAllConfig(true);
+ httpExecutor = httpExecutorFactory.getHttpExecutor(httpExecutorProps);
+ } else {
+ httpExecutor = HttpExecutorImpl.newInstance();
--- End diff --
`new HttpExecutorImpl()` instead
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r75683850
--- Diff: utils/common/src/main/java/org/apache/brooklyn/util/http/HttpExecutorFactory.java ---
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.util.http;
+
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.apache.brooklyn.util.collections.MutableMap;
+import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.http.executor.HttpExecutor;
+import org.apache.brooklyn.util.javalang.Reflections;
+import org.apache.brooklyn.util.text.StringPredicates;
+import org.apache.brooklyn.util.text.Strings;
+
+import com.google.common.base.Optional;
+import com.google.common.collect.Iterables;
+
+public class HttpExecutorFactory {
+ public static final String HTTP_EXECUTOR_CLASS = "httpExecutorClass";
+
+ public static final String HTTP_EXECUTOR_CLASS_PROPERTIES_PREFIX = HTTP_EXECUTOR_CLASS + ".";
+
+ private HttpExecutorFactory() {
+
+ }
+
+ public <T, S> HttpExecutor getHttpExecutor(Map<?, ?> props) {
+ HttpExecutor httpExecutor;
+
+ Optional<Map<?,?>> executorProps = (Optional<Map<?, ?>>) Iterables.tryFind(props.keySet(), StringPredicates.isStringStartingWith(HTTP_EXECUTOR_CLASS_PROPERTIES_PREFIX));
+
+ if (executorProps.isPresent()) {
+ Map<?, ?> instanceProps = executorProps.get();
+
+ String httpExecutorClass = (String) instanceProps.get(HTTP_EXECUTOR_CLASS);
--- End diff --
You are filtering for `HTTP_EXECUTOR_CLASS_PROPERTIES_PREFIX`, but expect `HTTP_EXECUTOR_CLASS` to be included - I think not?
Also if only `HTTP_EXECUTOR_CLASS` is included in the config `isPresent()` above will be `false`.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r77501296
--- Diff: core/src/main/java/org/apache/brooklyn/util/http/executor/HttpExecutorFactoryImpl.java ---
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.util.http.executor;
+
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.apache.brooklyn.util.collections.MutableMap;
+import org.apache.brooklyn.util.core.ClassLoaderUtils;
+import org.apache.brooklyn.util.core.flags.TypeCoercions;
+import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.http.executor.apacheclient.HttpExecutorImpl;
+import org.apache.brooklyn.util.text.StringPredicates;
+import org.apache.brooklyn.util.text.Strings;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.collect.Maps;
+
+public class HttpExecutorFactoryImpl implements HttpExecutorFactory {
+ private static final Logger LOG = LoggerFactory.getLogger(HttpExecutorFactoryImpl.class);
+
+ public HttpExecutorFactoryImpl() {
+ // no-op
+ }
+
+ @Override
+ public HttpExecutor getHttpExecutor(Map<?, ?> props) {
+ HttpExecutor httpExecutor;
+
+ String httpExecutorClass = (String) props.get(HTTP_EXECUTOR_CLASS);
+ if (httpExecutorClass != null) {
+ Map<?, ?> executorProps = Maps.filterKeys(props, StringPredicates.isStringStartingWith(HTTP_EXECUTOR_CLASS_CONFIG_PREFIX));
+ if (executorProps.size() > 0) {
+ Map<String, String> httpExecutorProps = MutableMap.of();
+ for (Entry<?, ?> entry: executorProps.entrySet()) {
+ String keyName = Strings.removeFromStart((String)entry.getKey(), HTTP_EXECUTOR_CLASS_CONFIG_PREFIX);
+ httpExecutorProps.put(keyName, TypeCoercions.coerce(entry.getValue(), String.class));
--- End diff --
Why force all executor params to be strings?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r77537414
--- Diff: core/src/main/java/org/apache/brooklyn/core/location/AbstractLocation.java ---
@@ -718,6 +719,35 @@ public boolean removeChild(Location child) {
return removed;
}
+ public void init() {
+ super.init();
+ loadExtension();
+ }
+
+ public void rebind() {
+ super.rebind();
+ loadExtension();
+ }
+
+ private void loadExtension() {
+ Map<String, String> extensions = getConfig(LocationConfigKeys.EXTENSIONS);
+ if (extensions != null) {
+ for (Map.Entry<String, String> extension: extensions.entrySet()) {
+ try {
+ Class<?> extensionClassType = new ClassLoaderUtils(this, getManagementContext()).loadClass(extension.getKey());
+
+ if (!hasExtension(extensionClassType)) {
+ Object extensionClass = new ClassLoaderUtils(this, getManagementContext()).loadClass(extension.getValue()).newInstance();
+ addExtension((Class)extensionClassType, extensionClass);
+ }
+ } catch (Exception e) {
+ LOG.error("Location extension can not be loaded (rethrowing): {}", new Object[] {extension.getKey(), extension.getValue(), e});
--- End diff --
Update the message to match the argument count
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by aledsage <gi...@git.apache.org>.
Github user aledsage commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r76570589
--- Diff: core/src/main/java/org/apache/brooklyn/core/location/AbstractLocation.java ---
@@ -718,6 +719,35 @@ public boolean removeChild(Location child) {
return removed;
}
+ public void init() {
+ super.init();
+ loadExtension();
+ }
+
+ public void rebind() {
+ super.rebind();
+ loadExtension();
+ }
+
+ private void loadExtension() {
+ try {
+ Map<String, String> extensions = getConfig(LocationConfigKeys.EXTENSIONS);
+ if (extensions != null) {
+ for (Map.Entry<String, String> extension: extensions.entrySet()) {
+ Class<?> extensionClassType = new ClassLoaderUtils(this, getManagementContext()).loadClass(extension.getKey());
+
+ if (!hasExtension(extensionClassType)) {
+ Object extensionClass = new ClassLoaderUtils(this, getManagementContext()).loadClass(extension.getValue()).newInstance();
+ addExtension((Class)extensionClassType, extensionClass);
+ }
+ }
+ }
+ } catch (Exception e) {
+ LOG.error("Location extension can not be loaded {}", e);
--- End diff --
Minor: this will call `LOG.error(String msg, Throwable t)` so the `{}` has no effect. That might be what we want, to ensure the stacktrace definitely gets logged (depending what happens to the propagated exception).
If we do want just the exception's toString, would need to change it to something like:
LOG.error("Location extension can not be loaded (rethrowing): {}", new Object[] {e});
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by ygy <gi...@git.apache.org>.
GitHub user ygy reopened a pull request:
https://github.com/apache/brooklyn-server/pull/285
Adding HttpExecutor to be used for interchangeable implementations
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/ygy/brooklyn-server feature/http-executor
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/brooklyn-server/pull/285.patch
To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:
This closes #285
----
commit 776a923c229fab34039d73ab078bc264b0cb0c2f
Author: Aled Sage <al...@gmail.com>
Date: 2016-08-09T13:41:37Z
Adds HttpExecutor interface
commit e785c29e8b7d298f22c0d7d44e993264f4dca0a5
Author: Aled Sage <al...@gmail.com>
Date: 2016-08-09T13:41:58Z
Adds apache-httpclient impl of HttpExecutor
commit 5faa686bfc4752554eaf20facdffbce6d0850673
Author: Yavor Yanchev <ya...@yanchev.com>
Date: 2016-08-16T06:39:48Z
Refactoring HttpFeed to use the new HttpExecutor interface.
commit 3a4f870ff2d6bf4087609f343754dc436030e789
Author: Yavor Yanchev <ya...@yanchev.com>
Date: 2016-08-29T12:09:15Z
Improving error log handling
commit acfd95625da146da267435cde8a70e41f6ee8242
Author: Valentin Aitken <bo...@gmail.com>
Date: 2016-09-01T11:39:37Z
No https
commit a22d622b468f98b8c18465289c2a87dc1a2db724
Author: Yavor Yanchev <ya...@yanchev.com>
Date: 2016-09-02T16:49:55Z
Adding HttpExecutorImplTest
commit 2903a55ad3405993158ce66fcf0d81dbe379040f
Author: Yavor Yanchev <ya...@yanchev.com>
Date: 2016-09-02T17:40:39Z
Addressing PR comments
commit d71602c00e63435337146ed6288bfc4f5aca6e2a
Author: Valentin Aitken <bo...@gmail.com>
Date: 2016-09-02T19:27:10Z
Add more assertions in HttpExecutorImplTest
commit 3a1861ec37b8fda6aa99244effcd246bf26fcbcb
Author: Yavor Yanchev <ya...@yanchev.com>
Date: 2016-09-03T12:12:41Z
Lowering the log level to info
commit 26f0b5ebd14c3eca722eb575038d7cd9108486c8
Author: Yavor Yanchev <ya...@yanchev.com>
Date: 2016-09-05T10:39:10Z
Improving responseCode handling
commit 41dd9b5f69bb4b05a6ff1a0b2c9a96b871e74c0e
Author: Yavor Yanchev <ya...@yanchev.com>
Date: 2016-09-05T12:29:27Z
Addressing PR comments
commit 6e6ba99b71d5faf2525aadc4b78ef0449906d9fa
Author: Yavor Yanchev <ya...@yanchev.com>
Date: 2016-09-05T14:37:19Z
Addressing more PR comments
commit 413249177020f4ed2019c551717f3d51e42b8de8
Author: Yavor Yanchev <ya...@yanchev.com>
Date: 2016-09-05T16:27:27Z
Addressing PR comments
----
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by bostko <gi...@git.apache.org>.
Github user bostko commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r76582992
--- Diff: core/src/main/java/org/apache/brooklyn/util/http/executor/HttpExecutorFactoryImpl.java ---
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.util.http.executor;
+
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.apache.brooklyn.util.collections.MutableMap;
+import org.apache.brooklyn.util.core.ClassLoaderUtils;
+import org.apache.brooklyn.util.core.flags.TypeCoercions;
+import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.http.executor.apacheclient.HttpExecutorImpl;
+import org.apache.brooklyn.util.text.StringPredicates;
+import org.apache.brooklyn.util.text.Strings;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.collect.Maps;
+
+public class HttpExecutorFactoryImpl implements HttpExecutorFactory {
+ private static final Logger LOG = LoggerFactory.getLogger(HttpExecutorFactoryImpl.class);
+
+ public HttpExecutorFactoryImpl() {
+ // no-op
+ }
+
+ @Override
+ public HttpExecutor getHttpExecutor(Map<?, ?> props) {
+ HttpExecutor httpExecutor;
+
+ String httpExecutorClass = (String) props.get(HTTP_EXECUTOR_CLASS);
+ if (httpExecutorClass != null) {
+ Map<?, ?> executorProps = Maps.filterKeys(props, StringPredicates.isStringStartingWith(HTTP_EXECUTOR_CLASS_CONFIG_PREFIX));
+ if (executorProps.size() > 0) {
+ Map<String, String> httpExecutorProps = MutableMap.of();
+ for (Entry<?, ?> entry: executorProps.entrySet()) {
+ String keyName = Strings.removeFromStart((String)entry.getKey(), HTTP_EXECUTOR_CLASS_CONFIG_PREFIX);
+ httpExecutorProps.put(keyName, TypeCoercions.coerce(entry.getValue(), String.class));
+ }
+
+ try {
+ httpExecutor = (HttpExecutor) new ClassLoaderUtils(HttpExecutorFactoryImpl.class).loadClass(httpExecutorClass).getConstructor(Map.class).newInstance(httpExecutorProps);
--- End diff --
You can benefit from using a factory pattern by implementing cache. May be better in a separate factory.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server issue #285: Adding HttpExecutor to be used for interchangeab...
Posted by ygy <gi...@git.apache.org>.
Github user ygy commented on the issue:
https://github.com/apache/brooklyn-server/pull/285
Jenkins, retest this please
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r77511829
--- Diff: utils/common/src/main/java/org/apache/brooklyn/util/http/executor/apacheclient/HttpExecutorImpl.java ---
@@ -0,0 +1,113 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.util.http.executor.apacheclient;
+
+import java.io.IOException;
+import java.util.Map;
+
+import org.apache.brooklyn.util.http.HttpTool;
+import org.apache.brooklyn.util.http.HttpTool.HttpClientBuilder;
+import org.apache.brooklyn.util.http.HttpToolResponse;
+import org.apache.brooklyn.util.http.executor.HttpConfig;
+import org.apache.brooklyn.util.http.executor.HttpExecutor;
+import org.apache.brooklyn.util.http.executor.HttpRequest;
+import org.apache.brooklyn.util.http.executor.HttpResponse;
+import org.apache.http.auth.Credentials;
+import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.client.HttpClient;
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.Optional;
+
+@Beta
+public class HttpExecutorImpl implements HttpExecutor {
+
+ private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
+
+ private static final HttpConfig DEFAULT_CONFIG = HttpConfig.builder()
+ .laxRedirect(false)
+ .trustAll(false)
+ .trustSelfSigned(false)
+ .build();
+
+ public static HttpExecutorImpl newInstance() {
+ return newInstance(HttpTool.httpClientBuilder());
+ }
+
+ /**
+ * This {@code baseBuilder} is used to construct a new {@link HttpClient} for each call to {@link #execute(HttpRequest)}.
+ * Callers are strongly discouraged from modifying the baseBuilder after passing it in!
+ */
+ public static HttpExecutorImpl newInstance(HttpTool.HttpClientBuilder baseBuilder) {
+ return new HttpExecutorImpl(baseBuilder);
+ }
+
+ private final HttpClientBuilder baseBuilder;
+
+ /**
+ * A must have constructor.
+ */
+ public HttpExecutorImpl(Map<?, ?> props) {
+ this(HttpTool.httpClientBuilder());
+ }
+
+ protected HttpExecutorImpl(HttpClientBuilder baseBuilder) {
+ this.baseBuilder = baseBuilder;
+ }
+
+ @Override
+ public HttpResponse execute(HttpRequest request) throws IOException {
+ HttpConfig config = (request.config() != null) ? request.config() : DEFAULT_CONFIG;
+ Credentials creds = (request.credentials() != null) ? new UsernamePasswordCredentials(request.credentials().username(), request.credentials().password()) : null;
+ HttpClient httpClient = HttpTool.HttpClientBuilder.fromBuilder(baseBuilder)
+ .uri(request.uri())
+ .credential(Optional.fromNullable(creds))
+ .laxRedirect(config.laxRedirect())
+ .trustSelfSigned(config.trustSelfSigned())
+ .trustAll(config.trustAll())
+ .build();
+
+ HttpToolResponse response;
+
+ switch (request.method().toLowerCase()) {
+ case "get":
+ response = HttpTool.httpGet(httpClient, request.uri(), request.headers());
+ break;
+ case "head":
+ response = HttpTool.httpHead(httpClient, request.uri(), request.headers());
+ break;
+ case "post":
+ response = HttpTool.httpPost(httpClient, request.uri(), request.headers(), orEmpty(request.body()));
+ break;
+ case "put":
+ response = HttpTool.httpPut(httpClient, request.uri(), request.headers(), orEmpty(request.body()));
+ break;
+ case "delete":
--- End diff --
Add as upper-case constants to the interface.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by ygy <gi...@git.apache.org>.
Github user ygy closed the pull request at:
https://github.com/apache/brooklyn-server/pull/285
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r75687758
--- Diff: core/src/main/java/org/apache/brooklyn/feed/http/HttpFeed.java ---
@@ -275,7 +293,23 @@ public HttpFeed() {
protected HttpFeed(Builder builder) {
setConfig(ONLY_IF_SERVICE_UP, builder.onlyIfServiceUp);
Map<String,String> baseHeaders = ImmutableMap.copyOf(checkNotNull(builder.headers, "headers"));
-
+
+ HttpExecutor httpExecutor = null;
+ if (builder.httpExecutor != null) {
+ httpExecutor = builder.httpExecutor;
+ } else {
+ HttpExecutorFactory httpExecutorFactory = null;
+ Collection<Location> locations = builder.entity.getLocations();
+ Maybe<MachineLocation> location = Machines.findUniqueMachineLocation(locations, MachineLocation.class);
+ if (location.isPresent() && location.get().hasExtension(HttpExecutorFactory.class)) {
+ httpExecutorFactory = location.get().getExtension(HttpExecutorFactory.class);
+ Map<String, Object> httpExecutorProps = location.get().getAllConfig(true);
+ httpExecutor = httpExecutorFactory.getHttpExecutor(httpExecutorProps);
--- End diff --
Minor, could pass the location and the implementation will then decide whether to do `.getAllConfig(true);` or somthing simpler.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by ygy <gi...@git.apache.org>.
Github user ygy commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r76594668
--- Diff: utils/common/src/main/java/org/apache/brooklyn/util/http/executor/apacheclient/HttpExecutorImpl.java ---
@@ -0,0 +1,106 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.util.http.executor.apacheclient;
+
+import java.io.IOException;
+
+import org.apache.brooklyn.util.http.HttpTool;
+import org.apache.brooklyn.util.http.HttpTool.HttpClientBuilder;
+import org.apache.brooklyn.util.http.HttpToolResponse;
+import org.apache.brooklyn.util.http.executor.HttpConfig;
+import org.apache.brooklyn.util.http.executor.HttpExecutor;
+import org.apache.brooklyn.util.http.executor.HttpRequest;
+import org.apache.brooklyn.util.http.executor.HttpResponse;
+import org.apache.http.auth.Credentials;
+import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.client.HttpClient;
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.Optional;
+
+@Beta
+public class HttpExecutorImpl implements HttpExecutor {
+
+ private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
+
+ private static final HttpConfig DEFAULT_CONFIG = HttpConfig.builder()
+ .laxRedirect(false)
+ .trustAll(false)
+ .trustSelfSigned(false)
+ .build();
+
+ public static HttpExecutorImpl newInstance() {
+ return newInstance(HttpTool.httpClientBuilder());
+ }
+
+ /**
+ * This {@code baseBuilder} is used to construct a new {@link HttpClient} for each call to {@link #execute(HttpRequest)}.
+ * Callers are strongly discouraged from modifying the baseBuilder after passing it in!
+ */
+ public static HttpExecutorImpl newInstance(HttpTool.HttpClientBuilder baseBuilder) {
+ return new HttpExecutorImpl(baseBuilder);
+ }
+
+ private final HttpClientBuilder baseBuilder;
+
+ protected HttpExecutorImpl(HttpClientBuilder baseBuilder) {
+ this.baseBuilder = baseBuilder;
+ }
+
+ @Override
+ public HttpResponse execute(HttpRequest request) throws IOException {
+ HttpConfig config = (request.config() != null) ? request.config() : DEFAULT_CONFIG;
+ Credentials creds = (request.credentials() != null) ? new UsernamePasswordCredentials(request.credentials().username(), request.credentials().password()) : null;
+ HttpClient httpClient = HttpTool.HttpClientBuilder.fromBuilder(baseBuilder)
+ .uri(request.uri())
+ .https(request.isHttps())
+ .credential(Optional.fromNullable(creds))
+ .laxRedirect(config.laxRedirect())
+ .trustSelfSigned(config.trustSelfSigned())
+ .trustAll(config.trustAll())
+ .build();
--- End diff --
The baseBuilder provides configuration for several more parameters which are implementation dependent (e.g. apache client)
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r77536131
--- Diff: core/src/test/java/org/apache/brooklyn/util/http/executor/HttpExecutorImplTest.java ---
@@ -0,0 +1,210 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.util.http.executor;
+
+import static org.testng.AssertJUnit.assertTrue;
+import static org.testng.Assert.assertEquals;
+
+import java.net.URL;
+import java.util.Map;
+import java.util.Random;
+
+import com.google.common.base.Function;
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.Collections2;
+import org.apache.brooklyn.util.core.http.BetterMockWebServer;
+import org.apache.commons.codec.binary.Base64;
+import org.bouncycastle.util.Strings;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.common.io.ByteStreams;
+import com.google.mockwebserver.MockResponse;
+import com.google.mockwebserver.RecordedRequest;
+
+import javax.annotation.Nullable;
+
+public class HttpExecutorImplTest {
+ protected BetterMockWebServer server;
+ protected URL baseUrl;
+ protected HttpExecutorFactoryImpl factory;
+
+ protected String HTTP_RESPONSE_HEADER_KEY = "content-type";
+ protected String HTTP_RESPONSE_HEADER_VALUE = "application/json";
+ protected String HTTP_BODY = "{\"foo\":\"myfoo\"}";
+
+ @BeforeMethod(alwaysRun=true)
+ public void setUp() throws Exception {
+ factory = new HttpExecutorFactoryImpl();
+ server = BetterMockWebServer.newInstanceLocalhost();
+ server.play();
+ baseUrl = server.getUrl("/");
+ }
+
+ @AfterMethod
--- End diff --
Personal preference to include `alwaysRuns=true`
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: [WIP] Adding HttpExecutor to be used for ...
Posted by aledsage <gi...@git.apache.org>.
Github user aledsage commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r73674575
--- Diff: utils/common/src/main/java/org/apache/brooklyn/util/http/HttpTool.java ---
@@ -319,7 +318,7 @@ public HttpClientBuilder trustSelfSigned() {
return this;
}
public HttpClient build() {
- final DefaultHttpClient httpClient = new DefaultHttpClient(clientConnectionManager);
+ final HttpExecutor httpClient = new HttpExecutorImpl(clientConnectionManager);
--- End diff --
I assumed we'd use the factory, rather than calling the constructor directly.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r77507051
--- Diff: utils/common/src/main/java/org/apache/brooklyn/util/http/executor/HttpResponseImpl.java ---
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.util.http.executor;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.brooklyn.util.stream.Streams;
+
+import com.google.common.annotations.Beta;
+import com.google.common.collect.Multimap;
+
+@Beta
+public class HttpResponseImpl implements HttpResponse {
+
+ private final int code;
+ private final String reasonPhrase;
+ private final Multimap<String, String> headers;
+ private final long contentLength;
+ private final InputStream content;
+
+ protected HttpResponseImpl(HttpResponse.Builder builder) {
+ code = builder.code;
+ reasonPhrase = builder.reasonPhrase;
+ headers = checkNotNull(builder.headers, "headers");
+ contentLength = builder.contentLength;
+ content = builder.content;
+ }
+
+ @Override
+ public void close() throws IOException {
+ if (content != null) {
+ Streams.closeQuietly(content);
+ }
+ }
+
+ @Override
+ public int code() {
+ return code;
+ }
+
+ @Override
+ public String reasonPhrase() {
+ return reasonPhrase;
+ }
+
+ @Override
+ public Multimap<String, String> headers() {
+ return headers;
+ }
+
+ @Override
+ public long getContentLength() {
+ return contentLength;
--- End diff --
In the streaming case could have `Content-Length` set in the headers.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by ygy <gi...@git.apache.org>.
Github user ygy commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r77378577
--- Diff: core/src/test/java/org/apache/brooklyn/feed/http/HttpFeedTest.java ---
@@ -71,12 +71,12 @@
private static final long TIMEOUT_MS = 10*1000;
- private BetterMockWebServer server;
- private URL baseUrl;
+ protected BetterMockWebServer server;
--- End diff --
Addressed in 4d905a5120a263c41e7cb3d3e69c3f2cd1a7ccaa
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r77350751
--- Diff: core/src/main/java/org/apache/brooklyn/feed/http/HttpFeed.java ---
@@ -275,7 +294,23 @@ public HttpFeed() {
protected HttpFeed(Builder builder) {
setConfig(ONLY_IF_SERVICE_UP, builder.onlyIfServiceUp);
Map<String,String> baseHeaders = ImmutableMap.copyOf(checkNotNull(builder.headers, "headers"));
-
+
+ HttpExecutor httpExecutor;
+ if (builder.httpExecutor != null) {
+ httpExecutor = builder.httpExecutor;
+ } else {
+ HttpExecutorFactory httpExecutorFactory = null;
+ Collection<? extends Location> locations = Locations.getLocationsCheckingAncestors(builder.entity.getLocations(), builder.entity);
+ Maybe<Location> location = Machines.findUniqueElement(locations, Location.class);
--- End diff --
Better get the `SshMachineLocation` here (there might or might not be another location) and get the config from it. Inheritance should take care of getting the config from the right location.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r77512748
--- Diff: utils/common/src/main/java/org/apache/brooklyn/util/http/executor/apacheclient/HttpResponseWrapper.java ---
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.util.http.executor.apacheclient;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.brooklyn.util.guava.Maybe;
+import org.apache.brooklyn.util.http.HttpToolResponse;
+import org.apache.brooklyn.util.http.executor.HttpResponse;
+import org.apache.http.HttpEntity;
+import org.apache.http.util.EntityUtils;
+
+import com.google.common.annotations.Beta;
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.Multimap;
+import com.google.common.collect.Multimaps;
+
+@Beta
+public class HttpResponseWrapper implements HttpResponse {
+
+ private final HttpToolResponse delegate;
+ private transient volatile byte[] content;
+ private transient volatile Multimap<String, String> headers;
+
+ public HttpResponseWrapper(HttpToolResponse delegate) {
+ this.delegate = checkNotNull(delegate, "response");
+ }
+
+ @Override
+ public void close() throws IOException {
+ Maybe<org.apache.http.HttpResponse> apacheResponse = delegate.getResponse();
+ if (apacheResponse.isPresent()) {
+ HttpEntity entity = apacheResponse.get().getEntity();
+ if (entity != null) {
+ EntityUtils.consumeQuietly(apacheResponse.get().getEntity());
+ }
+ }
+ }
+
+ @Override
+ public int code() {
+ return delegate.getResponseCode();
+ }
+
+ @Override
+ public String reasonPhrase() {
+ return delegate.getReasonPhrase();
+ }
+
+ @Override
+ public Multimap<String, String> headers() {
+ return headersImpl();
+
+ }
+
+ @Override
+ public long getContentLength() {
+ byte[] content = getContentImpl();
+ return (content == null) ? -1 : content.length;
+ }
+
+ @Override
+ public InputStream getContent() {
+ byte[] content = getContentImpl();
+ return (content == null) ? null : new ByteArrayInputStream(content);
--- End diff --
Isn't there API to get the stream directly?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r77535728
--- Diff: core/src/main/java/org/apache/brooklyn/util/http/executor/HttpExecutorFactoryImpl.java ---
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.util.http.executor;
+
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.apache.brooklyn.util.collections.MutableMap;
+import org.apache.brooklyn.util.core.ClassLoaderUtils;
+import org.apache.brooklyn.util.core.flags.TypeCoercions;
+import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.http.executor.apacheclient.HttpExecutorImpl;
+import org.apache.brooklyn.util.text.StringPredicates;
+import org.apache.brooklyn.util.text.Strings;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.collect.Maps;
+
+public class HttpExecutorFactoryImpl implements HttpExecutorFactory {
+ private static final Logger LOG = LoggerFactory.getLogger(HttpExecutorFactoryImpl.class);
+
+ public HttpExecutorFactoryImpl() {
+ // no-op
+ }
+
+ @Override
+ public HttpExecutor getHttpExecutor(Map<?, ?> props) {
+ HttpExecutor httpExecutor;
+
+ String httpExecutorClass = (String) props.get(HTTP_EXECUTOR_CLASS_CONFIG);
+ if (httpExecutorClass != null) {
+ Map<String, String> httpExecutorProps = MutableMap.of();
+ Map<?, ?> executorProps = Maps.filterKeys(props, StringPredicates.isStringStartingWith(HTTP_EXECUTOR_CLASS_CONFIG_PREFIX));
+ if (executorProps.size() > 0) {
+
+ for (Entry<?, ?> entry: executorProps.entrySet()) {
+ String keyName = Strings.removeFromStart((String)entry.getKey(), HTTP_EXECUTOR_CLASS_CONFIG_PREFIX);
+ httpExecutorProps.put(keyName, TypeCoercions.coerce(entry.getValue(), String.class));
--- End diff --
Why assume all keys are `String`s? This is implementation dependent.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r77500324
--- Diff: core/src/main/java/org/apache/brooklyn/feed/http/HttpFeed.java ---
@@ -331,52 +366,62 @@ protected void preStart() {
}
Callable<HttpToolResponse> pollJob;
-
- if (pollInfo.method.equals("get")) {
- pollJob = new Callable<HttpToolResponse>() {
- public HttpToolResponse call() throws Exception {
- if (log.isTraceEnabled()) log.trace("http polling for {} sensors at {}", entity, pollInfo);
- return HttpTool.httpGet(httpClient, pollInfo.uriProvider.get(), pollInfo.headers);
- }};
- } else if (pollInfo.method.equals("post")) {
- pollJob = new Callable<HttpToolResponse>() {
- public HttpToolResponse call() throws Exception {
- if (log.isTraceEnabled()) log.trace("http polling for {} sensors at {}", entity, pollInfo);
- return HttpTool.httpPost(httpClient, pollInfo.uriProvider.get(), pollInfo.headers, pollInfo.body);
- }};
- } else if (pollInfo.method.equals("head")) {
- pollJob = new Callable<HttpToolResponse>() {
- public HttpToolResponse call() throws Exception {
- if (log.isTraceEnabled()) log.trace("http polling for {} sensors at {}", entity, pollInfo);
- return HttpTool.httpHead(httpClient, pollInfo.uriProvider.get(), pollInfo.headers);
- }};
- } else {
- throw new IllegalStateException("Unexpected http method: "+pollInfo.method);
- }
-
- getPoller().scheduleAtFixedRate(pollJob, new DelegatingPollHandler<HttpToolResponse>(handlers), minPeriod);
- }
- }
+ pollJob = new Callable<HttpToolResponse>() {
+ public HttpToolResponse call() throws Exception {
+ if (log.isTraceEnabled()) log.trace("http polling for {} sensors at {}", entity, pollInfo);
- // TODO Should we really trustAll for https? Make configurable?
- private HttpClient createHttpClient(HttpPollIdentifier pollIdentifier) {
- URI uri = pollIdentifier.uriProvider.get();
- HttpClientBuilder builder = HttpTool.httpClientBuilder()
- .trustAll()
- .laxRedirect(true);
- if (uri != null) builder.uri(uri);
- if (uri != null) builder.credential(pollIdentifier.credentials);
- if (pollIdentifier.connectionTimeout != null) {
- builder.connectionTimeout(pollIdentifier.connectionTimeout);
- }
- if (pollIdentifier.socketTimeout != null) {
- builder.socketTimeout(pollIdentifier.socketTimeout);
+ BasicAuth creds = null;
+ if (pollInfo.credentials.isPresent()) {
+ creds = org.apache.brooklyn.util.http.executor.Credentials.basic(
+ pollInfo.credentials.get().getUserPrincipal().getName(),
+ pollInfo.credentials.get().getPassword());
+ }
+
+ HttpResponse response = pollInfo.httpExecutor.execute(new HttpRequest.Builder()
+ .headers(pollInfo.headers)
+ .uri(pollInfo.uriProvider.get())
+ .credentials(creds)
+ .method(pollInfo.method)
+ .body(pollInfo.body)
+ .config(HttpConfig.builder()
+ .trustSelfSigned(true)
+ .trustAll(true)
+ .laxRedirect(true)
+ .build())
+ .build());
+ return createHttpToolRespose(response);
+ }};
+ getPoller().scheduleAtFixedRate(pollJob, new DelegatingPollHandler(handlers), minPeriod);
}
- return builder.build();
}
@SuppressWarnings("unchecked")
protected Poller<HttpToolResponse> getPoller() {
- return (Poller<HttpToolResponse>) super.getPoller();
+ return (Poller<HttpToolResponse>) super.getPoller();
+ }
+
+ @SuppressWarnings("unchecked")
+ private HttpToolResponse createHttpToolRespose(HttpResponse response) throws IOException {
+ int responseCode = response.code();
+
+ /* From https://tools.ietf.org/html/rfc4918#section-11.2
+ The 422 (Unprocessable Entity) status code means the server
+ understands the content type of the request entity (hence a
+ 415(Unsupported Media Type) status code is inappropriate), and the
+ syntax of the request entity is correct (thus a 400 (Bad Request)
+ status code is inappropriate) but was unable to process the contained
+ instructions. */
+ if (responseCode == 422) {
+ throw new IOException(" Unprocessable Entity: " + response.reasonPhrase());
+ }
--- End diff --
Still don't see why we need special handling of 422? (to discuss).
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r75687462
--- Diff: core/src/main/java/org/apache/brooklyn/feed/http/HttpFeed.java ---
@@ -275,7 +293,23 @@ public HttpFeed() {
protected HttpFeed(Builder builder) {
setConfig(ONLY_IF_SERVICE_UP, builder.onlyIfServiceUp);
Map<String,String> baseHeaders = ImmutableMap.copyOf(checkNotNull(builder.headers, "headers"));
-
+
+ HttpExecutor httpExecutor = null;
+ if (builder.httpExecutor != null) {
+ httpExecutor = builder.httpExecutor;
+ } else {
+ HttpExecutorFactory httpExecutorFactory = null;
+ Collection<Location> locations = builder.entity.getLocations();
+ Maybe<MachineLocation> location = Machines.findUniqueMachineLocation(locations, MachineLocation.class);
--- End diff --
Use `Locations.getLocationsCheckingAncestors(locations, entity)` instead, the location could be coming from a parent entity.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r77535388
--- Diff: core/src/main/java/org/apache/brooklyn/feed/http/HttpFeed.java ---
@@ -331,52 +366,55 @@ protected void preStart() {
}
Callable<HttpToolResponse> pollJob;
-
- if (pollInfo.method.equals("get")) {
- pollJob = new Callable<HttpToolResponse>() {
- public HttpToolResponse call() throws Exception {
- if (log.isTraceEnabled()) log.trace("http polling for {} sensors at {}", entity, pollInfo);
- return HttpTool.httpGet(httpClient, pollInfo.uriProvider.get(), pollInfo.headers);
- }};
- } else if (pollInfo.method.equals("post")) {
- pollJob = new Callable<HttpToolResponse>() {
- public HttpToolResponse call() throws Exception {
- if (log.isTraceEnabled()) log.trace("http polling for {} sensors at {}", entity, pollInfo);
- return HttpTool.httpPost(httpClient, pollInfo.uriProvider.get(), pollInfo.headers, pollInfo.body);
- }};
- } else if (pollInfo.method.equals("head")) {
- pollJob = new Callable<HttpToolResponse>() {
- public HttpToolResponse call() throws Exception {
- if (log.isTraceEnabled()) log.trace("http polling for {} sensors at {}", entity, pollInfo);
- return HttpTool.httpHead(httpClient, pollInfo.uriProvider.get(), pollInfo.headers);
- }};
- } else {
- throw new IllegalStateException("Unexpected http method: "+pollInfo.method);
- }
-
- getPoller().scheduleAtFixedRate(pollJob, new DelegatingPollHandler<HttpToolResponse>(handlers), minPeriod);
- }
- }
+ pollJob = new Callable<HttpToolResponse>() {
+ public HttpToolResponse call() throws Exception {
+ if (log.isTraceEnabled()) log.trace("http polling for {} sensors at {}", entity, pollInfo);
- // TODO Should we really trustAll for https? Make configurable?
- private HttpClient createHttpClient(HttpPollIdentifier pollIdentifier) {
- URI uri = pollIdentifier.uriProvider.get();
- HttpClientBuilder builder = HttpTool.httpClientBuilder()
- .trustAll()
- .laxRedirect(true);
- if (uri != null) builder.uri(uri);
- if (uri != null) builder.credential(pollIdentifier.credentials);
- if (pollIdentifier.connectionTimeout != null) {
- builder.connectionTimeout(pollIdentifier.connectionTimeout);
- }
- if (pollIdentifier.socketTimeout != null) {
- builder.socketTimeout(pollIdentifier.socketTimeout);
+ UsernamePassword creds = null;
+ if (pollInfo.credentials.isPresent()) {
+ creds = new UsernamePassword(
+ pollInfo.credentials.get().getUserPrincipal().getName(),
+ pollInfo.credentials.get().getPassword());
+ }
+
+ HttpResponse response = pollInfo.httpExecutor.execute(new HttpRequest.Builder()
+ .headers(pollInfo.headers)
+ .uri(pollInfo.uriProvider.get())
+ .credentials(creds)
+ .method(pollInfo.method)
+ .body(pollInfo.body)
+ .config(HttpConfig.builder()
+ .trustSelfSigned(true)
+ .trustAll(true)
+ .laxRedirect(true)
+ .build())
+ .build());
+ return createHttpToolRespose(response);
+ }};
+ getPoller().scheduleAtFixedRate(pollJob, new DelegatingPollHandler(handlers), minPeriod);
}
- return builder.build();
}
@SuppressWarnings("unchecked")
protected Poller<HttpToolResponse> getPoller() {
- return (Poller<HttpToolResponse>) super.getPoller();
+ return (Poller<HttpToolResponse>) super.getPoller();
+ }
+
+ @SuppressWarnings("unchecked")
+ private HttpToolResponse createHttpToolRespose(HttpResponse response) throws IOException {
+ int responseCode = response.code();
+
+ Map<String,? extends List<String>> headers = (Map<String, List<String>>) (Map<?, ?>) response.headers().asMap();
+
+ byte[] content = null;
+ if (response.getContentLength() > 0) {
--- End diff --
Better copy the current logic from [HttpToolResponse](https://github.com/apache/brooklyn-server/blob/master/utils/common/src/main/java/org/apache/brooklyn/util/http/HttpToolResponse.java#L69-L81) as it's been battle-tested.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r77500994
--- Diff: core/src/main/java/org/apache/brooklyn/util/http/executor/HttpExecutorFactory.java ---
@@ -0,0 +1,29 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.util.http.executor;
+
+import java.util.Map;
+
+public interface HttpExecutorFactory {
+ public static final String HTTP_EXECUTOR_CLASS = "httpExecutorClass";
--- End diff --
Nit: `HTTP_EXECUTOR_CLASS_CONFIG` would be more appropriate (or `HTTP_EXECUTOR_CLASS_PROPERTY`), the former following the pattern of he next property.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r77546852
--- Diff: core/src/main/java/org/apache/brooklyn/feed/http/HttpFeed.java ---
@@ -407,14 +410,26 @@ private HttpToolResponse createHttpToolRespose(HttpResponse response) throws IOE
Map<String,? extends List<String>> headers = (Map<String, List<String>>) (Map<?, ?>) response.headers().asMap();
byte[] content = null;
- if (response.getContentLength() > 0) {
- content = ByteStreams.toByteArray(response.getContent());
+ final long durationMillisOfFirstResponse;
+ final long durationMillisOfFullContent;
+ final long startTime = System.currentTimeMillis();
+
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ durationMillisOfFirstResponse = Duration.sinceUtc(startTime).toMilliseconds();
+ try {
+ ByteStreams.copy(response.getContent(), out);
+ content = out.toByteArray();
+ } catch (IOException e) {
+ throw Throwables.propagate(e);
--- End diff --
In Brooklyn the recommended is `Exceptions.propagate`, but this works as well in this case.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by ygy <gi...@git.apache.org>.
GitHub user ygy reopened a pull request:
https://github.com/apache/brooklyn-server/pull/285
Adding HttpExecutor to be used for interchangeable implementations
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/ygy/brooklyn-server feature/http-executor
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/brooklyn-server/pull/285.patch
To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:
This closes #285
----
commit 776a923c229fab34039d73ab078bc264b0cb0c2f
Author: Aled Sage <al...@gmail.com>
Date: 2016-08-09T13:41:37Z
Adds HttpExecutor interface
commit e785c29e8b7d298f22c0d7d44e993264f4dca0a5
Author: Aled Sage <al...@gmail.com>
Date: 2016-08-09T13:41:58Z
Adds apache-httpclient impl of HttpExecutor
commit 5faa686bfc4752554eaf20facdffbce6d0850673
Author: Yavor Yanchev <ya...@yanchev.com>
Date: 2016-08-16T06:39:48Z
Refactoring HttpFeed to use the new HttpExecutor interface.
commit 3a4f870ff2d6bf4087609f343754dc436030e789
Author: Yavor Yanchev <ya...@yanchev.com>
Date: 2016-08-29T12:09:15Z
Improving error log handling
commit acfd95625da146da267435cde8a70e41f6ee8242
Author: Valentin Aitken <bo...@gmail.com>
Date: 2016-09-01T11:39:37Z
No https
commit a22d622b468f98b8c18465289c2a87dc1a2db724
Author: Yavor Yanchev <ya...@yanchev.com>
Date: 2016-09-02T16:49:55Z
Adding HttpExecutorImplTest
commit 2903a55ad3405993158ce66fcf0d81dbe379040f
Author: Yavor Yanchev <ya...@yanchev.com>
Date: 2016-09-02T17:40:39Z
Addressing PR comments
commit d71602c00e63435337146ed6288bfc4f5aca6e2a
Author: Valentin Aitken <bo...@gmail.com>
Date: 2016-09-02T19:27:10Z
Add more assertions in HttpExecutorImplTest
commit 3a1861ec37b8fda6aa99244effcd246bf26fcbcb
Author: Yavor Yanchev <ya...@yanchev.com>
Date: 2016-09-03T12:12:41Z
Lowering the log level to info
commit 26f0b5ebd14c3eca722eb575038d7cd9108486c8
Author: Yavor Yanchev <ya...@yanchev.com>
Date: 2016-09-05T10:39:10Z
Improving responseCode handling
commit 41dd9b5f69bb4b05a6ff1a0b2c9a96b871e74c0e
Author: Yavor Yanchev <ya...@yanchev.com>
Date: 2016-09-05T12:29:27Z
Addressing PR comments
commit 6e6ba99b71d5faf2525aadc4b78ef0449906d9fa
Author: Yavor Yanchev <ya...@yanchev.com>
Date: 2016-09-05T14:37:19Z
Addressing more PR comments
commit 413249177020f4ed2019c551717f3d51e42b8de8
Author: Yavor Yanchev <ya...@yanchev.com>
Date: 2016-09-05T16:27:27Z
Addressing PR comments
----
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r75688758
--- Diff: core/src/main/java/org/apache/brooklyn/feed/http/HttpFeed.java ---
@@ -275,7 +293,23 @@ public HttpFeed() {
protected HttpFeed(Builder builder) {
setConfig(ONLY_IF_SERVICE_UP, builder.onlyIfServiceUp);
Map<String,String> baseHeaders = ImmutableMap.copyOf(checkNotNull(builder.headers, "headers"));
-
+
+ HttpExecutor httpExecutor = null;
+ if (builder.httpExecutor != null) {
+ httpExecutor = builder.httpExecutor;
+ } else {
+ HttpExecutorFactory httpExecutorFactory = null;
+ Collection<Location> locations = builder.entity.getLocations();
+ Maybe<MachineLocation> location = Machines.findUniqueMachineLocation(locations, MachineLocation.class);
+ if (location.isPresent() && location.get().hasExtension(HttpExecutorFactory.class)) {
+ httpExecutorFactory = location.get().getExtension(HttpExecutorFactory.class);
+ Map<String, Object> httpExecutorProps = location.get().getAllConfig(true);
+ httpExecutor = httpExecutorFactory.getHttpExecutor(httpExecutorProps);
+ } else {
+ httpExecutor = HttpExecutorImpl.newInstance();
--- End diff --
Ignore.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by ygy <gi...@git.apache.org>.
Github user ygy commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r77385034
--- Diff: core/src/main/java/org/apache/brooklyn/feed/http/HttpFeed.java ---
@@ -275,7 +294,23 @@ public HttpFeed() {
protected HttpFeed(Builder builder) {
setConfig(ONLY_IF_SERVICE_UP, builder.onlyIfServiceUp);
Map<String,String> baseHeaders = ImmutableMap.copyOf(checkNotNull(builder.headers, "headers"));
-
+
+ HttpExecutor httpExecutor;
+ if (builder.httpExecutor != null) {
+ httpExecutor = builder.httpExecutor;
+ } else {
+ HttpExecutorFactory httpExecutorFactory = null;
+ Collection<? extends Location> locations = Locations.getLocationsCheckingAncestors(builder.entity.getLocations(), builder.entity);
+ Maybe<Location> location = Machines.findUniqueElement(locations, Location.class);
--- End diff --
Addressed. `MachineLocation` is used to cover windows entities too.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by bostko <gi...@git.apache.org>.
Github user bostko commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r75928110
--- Diff: core/src/main/java/org/apache/brooklyn/feed/http/HttpFeed.java ---
@@ -331,52 +366,45 @@ protected void preStart() {
}
Callable<HttpToolResponse> pollJob;
-
- if (pollInfo.method.equals("get")) {
- pollJob = new Callable<HttpToolResponse>() {
- public HttpToolResponse call() throws Exception {
- if (log.isTraceEnabled()) log.trace("http polling for {} sensors at {}", entity, pollInfo);
- return HttpTool.httpGet(httpClient, pollInfo.uriProvider.get(), pollInfo.headers);
- }};
- } else if (pollInfo.method.equals("post")) {
- pollJob = new Callable<HttpToolResponse>() {
- public HttpToolResponse call() throws Exception {
- if (log.isTraceEnabled()) log.trace("http polling for {} sensors at {}", entity, pollInfo);
- return HttpTool.httpPost(httpClient, pollInfo.uriProvider.get(), pollInfo.headers, pollInfo.body);
- }};
- } else if (pollInfo.method.equals("head")) {
- pollJob = new Callable<HttpToolResponse>() {
- public HttpToolResponse call() throws Exception {
- if (log.isTraceEnabled()) log.trace("http polling for {} sensors at {}", entity, pollInfo);
- return HttpTool.httpHead(httpClient, pollInfo.uriProvider.get(), pollInfo.headers);
- }};
- } else {
- throw new IllegalStateException("Unexpected http method: "+pollInfo.method);
- }
-
- getPoller().scheduleAtFixedRate(pollJob, new DelegatingPollHandler<HttpToolResponse>(handlers), minPeriod);
- }
- }
+ pollJob = new Callable<HttpToolResponse>() {
+ public HttpToolResponse call() throws Exception {
+ if (log.isTraceEnabled()) log.trace("http polling for {} sensors at {}", entity, pollInfo);
- // TODO Should we really trustAll for https? Make configurable?
- private HttpClient createHttpClient(HttpPollIdentifier pollIdentifier) {
- URI uri = pollIdentifier.uriProvider.get();
- HttpClientBuilder builder = HttpTool.httpClientBuilder()
- .trustAll()
- .laxRedirect(true);
- if (uri != null) builder.uri(uri);
- if (uri != null) builder.credential(pollIdentifier.credentials);
- if (pollIdentifier.connectionTimeout != null) {
- builder.connectionTimeout(pollIdentifier.connectionTimeout);
- }
- if (pollIdentifier.socketTimeout != null) {
- builder.socketTimeout(pollIdentifier.socketTimeout);
+ BasicAuth creds = null;
+ if (pollInfo.credentials.isPresent()) {
+ creds = org.apache.brooklyn.util.http.executor.Credentials.basic(
+ pollInfo.credentials.get().getUserPrincipal().getName(),
+ pollInfo.credentials.get().getPassword());
+ }
+
+ HttpResponse response = pollInfo.httpExecutor.execute(new HttpRequest.Builder()
+ .headers(pollInfo.headers)
+ .uri(pollInfo.uriProvider.get())
+ .credentials(creds)
+ .method(pollInfo.method)
+ .body(pollInfo.body)
+ .build());
+ return createHttpToolRespose(response);
+ }};
+ getPoller().scheduleAtFixedRate(pollJob, new DelegatingPollHandler(handlers), minPeriod);
--- End diff --
Wrong indentation here.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by ygy <gi...@git.apache.org>.
Github user ygy closed the pull request at:
https://github.com/apache/brooklyn-server/pull/285
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by bostko <gi...@git.apache.org>.
Github user bostko commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r76591742
--- Diff: utils/common/src/main/java/org/apache/brooklyn/util/http/executor/HttpRequest.java ---
@@ -0,0 +1,128 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.util.http.executor;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.net.URI;
+import java.util.Map;
+
+import javax.annotation.Nullable;
+
+import com.google.common.annotations.Beta;
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.Multimap;
+
+@Beta
+public interface HttpRequest {
+
+ // TODO Should we use InputStream for body (rather than byte[]). That would mean we don't have
+ // to hold the entire body in-memory, which might be important for really big payloads!
--- End diff --
I think the end consumer who is using HttpResponse and HttpRequest should take care for closing the Stream.
Please check `org.apache.http.HttpEntity`.
In essence HttpExecutor is a pattern for providing external httpClients.
For deciding what to return in HttpRequest and HttpResponse Stream or byte[], I think we should better take a look at exposing it in a way which is implemented in most of the httpClients like Apache Http Client.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by ygy <gi...@git.apache.org>.
Github user ygy commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r77386547
--- Diff: core/src/main/java/org/apache/brooklyn/location/byon/ByonLocationResolver.java ---
@@ -108,6 +109,7 @@ protected ConfigBag extractConfig(Map<?,?> locationFlags, String spec, LocationR
MutableMap<String, Object> defaultProps = MutableMap.of();
defaultProps.addIfNotNull("user", user);
defaultProps.addIfNotNull("port", port);
+ defaultProps.addIfNotNull(LocationConfigKeys.EXTENSIONS.getName(), config.get(LocationConfigKeys.EXTENSIONS));
--- End diff --
It is needed so the extensions are supported for BYON locations too.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by bostko <gi...@git.apache.org>.
Github user bostko commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r76590580
--- Diff: core/src/test/java/org/apache/brooklyn/feed/http/HttpFeedTest.java ---
@@ -71,12 +71,12 @@
private static final long TIMEOUT_MS = 10*1000;
- private BetterMockWebServer server;
- private URL baseUrl;
+ protected BetterMockWebServer server;
--- End diff --
It worth's adding in this PR tests for HttpExecutor but without HttpFeed.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r75684986
--- Diff: core/src/main/java/org/apache/brooklyn/core/location/LocationConfigKeys.java ---
@@ -35,7 +35,9 @@
public static final ConfigKey<String> DISPLAY_NAME = ConfigKeys.newStringConfigKey("displayName");
public static final ConfigKey<Boolean> ENABLED = ConfigKeys.newBooleanConfigKey("enabled", "Whether the location is enabled for listing and use "
+ "(only supported for selected locations)", true);
-
+
+ public static final ConfigKey<String> EXTENSION = ConfigKeys.newStringConfigKey("extension");
--- End diff --
Better make it Map<String, Object> to
* allow multiple extensions to be registered
* use $brooklyn:object to construct the extensions (though brooklyn DSL doesn't work too well in location config). If it's a string then could treat it as class name and instantiate it.
wdyt?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by ygy <gi...@git.apache.org>.
GitHub user ygy reopened a pull request:
https://github.com/apache/brooklyn-server/pull/285
Adding HttpExecutor to be used for interchangeable implementations
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/ygy/brooklyn-server feature/http-executor
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/brooklyn-server/pull/285.patch
To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:
This closes #285
----
commit 776a923c229fab34039d73ab078bc264b0cb0c2f
Author: Aled Sage <al...@gmail.com>
Date: 2016-08-09T13:41:37Z
Adds HttpExecutor interface
commit e785c29e8b7d298f22c0d7d44e993264f4dca0a5
Author: Aled Sage <al...@gmail.com>
Date: 2016-08-09T13:41:58Z
Adds apache-httpclient impl of HttpExecutor
commit 5faa686bfc4752554eaf20facdffbce6d0850673
Author: Yavor Yanchev <ya...@yanchev.com>
Date: 2016-08-16T06:39:48Z
Refactoring HttpFeed to use the new HttpExecutor interface.
commit 3a4f870ff2d6bf4087609f343754dc436030e789
Author: Yavor Yanchev <ya...@yanchev.com>
Date: 2016-08-29T12:09:15Z
Improving error log handling
commit acfd95625da146da267435cde8a70e41f6ee8242
Author: Valentin Aitken <bo...@gmail.com>
Date: 2016-09-01T11:39:37Z
No https
commit a22d622b468f98b8c18465289c2a87dc1a2db724
Author: Yavor Yanchev <ya...@yanchev.com>
Date: 2016-09-02T16:49:55Z
Adding HttpExecutorImplTest
commit 2903a55ad3405993158ce66fcf0d81dbe379040f
Author: Yavor Yanchev <ya...@yanchev.com>
Date: 2016-09-02T17:40:39Z
Addressing PR comments
commit d71602c00e63435337146ed6288bfc4f5aca6e2a
Author: Valentin Aitken <bo...@gmail.com>
Date: 2016-09-02T19:27:10Z
Add more assertions in HttpExecutorImplTest
commit 3a1861ec37b8fda6aa99244effcd246bf26fcbcb
Author: Yavor Yanchev <ya...@yanchev.com>
Date: 2016-09-03T12:12:41Z
Lowering the log level to info
commit 26f0b5ebd14c3eca722eb575038d7cd9108486c8
Author: Yavor Yanchev <ya...@yanchev.com>
Date: 2016-09-05T10:39:10Z
Improving responseCode handling
commit 41dd9b5f69bb4b05a6ff1a0b2c9a96b871e74c0e
Author: Yavor Yanchev <ya...@yanchev.com>
Date: 2016-09-05T12:29:27Z
Addressing PR comments
commit 6e6ba99b71d5faf2525aadc4b78ef0449906d9fa
Author: Yavor Yanchev <ya...@yanchev.com>
Date: 2016-09-05T14:37:19Z
Addressing more PR comments
commit 413249177020f4ed2019c551717f3d51e42b8de8
Author: Yavor Yanchev <ya...@yanchev.com>
Date: 2016-09-05T16:27:27Z
Addressing PR comments
----
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server issue #285: Adding HttpExecutor to be used for interchangeab...
Posted by ygy <gi...@git.apache.org>.
Github user ygy commented on the issue:
https://github.com/apache/brooklyn-server/pull/285
@nakomis @neykov Can you review this PR?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server issue #285: Adding HttpExecutor to be used for interchangeab...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on the issue:
https://github.com/apache/brooklyn-server/pull/285
Thanks @ygy, merging.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r75685265
--- Diff: utils/common/src/main/java/org/apache/brooklyn/util/http/executor/HttpRequest.java ---
@@ -0,0 +1,128 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.util.http.executor;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.net.URI;
+import java.util.Map;
+
+import javax.annotation.Nullable;
+
+import com.google.common.annotations.Beta;
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.Multimap;
+
+@Beta
+public interface HttpRequest {
+
+ // TODO Should we use InputStream for body (rather than byte[]). That would mean we don't have
+ // to hold the entire body in-memory, which might be important for really big payloads!
+
+ @Beta
+ public static class Builder {
+ protected boolean isHttps;
--- End diff --
The protocol is included in the `URI`, better skip this one.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r75684207
--- Diff: utils/common/src/main/java/org/apache/brooklyn/util/http/HttpExecutorFactory.java ---
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.util.http;
+
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.apache.brooklyn.util.collections.MutableMap;
+import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.http.executor.HttpExecutor;
+import org.apache.brooklyn.util.javalang.Reflections;
+import org.apache.brooklyn.util.text.StringPredicates;
+import org.apache.brooklyn.util.text.Strings;
+
+import com.google.common.base.Optional;
+import com.google.common.collect.Iterables;
+
+public class HttpExecutorFactory {
+ public static final String HTTP_EXECUTOR_CLASS = "httpExecutorClass";
+
+ public static final String HTTP_EXECUTOR_CLASS_PROPERTIES_PREFIX = HTTP_EXECUTOR_CLASS + ".";
+
+ private HttpExecutorFactory() {
+
+ }
+
+ public <T, S> HttpExecutor getHttpExecutor(Map<?, ?> props) {
+ HttpExecutor httpExecutor;
+
+ Optional<Map<?,?>> executorProps = (Optional<Map<?, ?>>) Iterables.tryFind(props.keySet(), StringPredicates.isStringStartingWith(HTTP_EXECUTOR_CLASS_PROPERTIES_PREFIX));
+
+ if (executorProps.isPresent()) {
+ Map<?, ?> instanceProps = executorProps.get();
+
+ String httpExecutorClass = (String) instanceProps.get(HTTP_EXECUTOR_CLASS);
+ Map<String, String> httpExecutorProps = MutableMap.of();
+ for (Entry<?, ?> entry: instanceProps.entrySet()) {
+ String keyName = Strings.removeFromStart((String)entry.getKey(), HTTP_EXECUTOR_CLASS_PROPERTIES_PREFIX);
+ httpExecutorProps.put(keyName, (String)entry.getValue());
+ }
+
+ Reflections reflections = new Reflections(HttpExecutorFactory.class.getClassLoader());
--- End diff --
Better use `ClassLoaderUtils` instead of `Reflections` (here and elsewhere in the PR) to be able to use some classloading goodies in OSGi land.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r75687134
--- Diff: utils/common/src/main/java/org/apache/brooklyn/util/http/executor/HttpRequest.java ---
@@ -0,0 +1,128 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.util.http.executor;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.net.URI;
+import java.util.Map;
+
+import javax.annotation.Nullable;
+
+import com.google.common.annotations.Beta;
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.Multimap;
+
+@Beta
+public interface HttpRequest {
+
+ // TODO Should we use InputStream for body (rather than byte[]). That would mean we don't have
+ // to hold the entire body in-memory, which might be important for really big payloads!
--- End diff --
One reason not to do it is that consuming the stream might be async (?) so it's not clear when to close the stream.
Whatever we end up doing it's best to be consistent - why accept byte[] in the request but return stream in the response?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r75689880
--- Diff: utils/common/src/main/java/org/apache/brooklyn/util/http/executor/apacheclient/HttpExecutorImpl.java ---
@@ -0,0 +1,106 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.util.http.executor.apacheclient;
+
+import java.io.IOException;
+
+import org.apache.brooklyn.util.http.HttpTool;
+import org.apache.brooklyn.util.http.HttpTool.HttpClientBuilder;
+import org.apache.brooklyn.util.http.HttpToolResponse;
+import org.apache.brooklyn.util.http.executor.HttpConfig;
+import org.apache.brooklyn.util.http.executor.HttpExecutor;
+import org.apache.brooklyn.util.http.executor.HttpRequest;
+import org.apache.brooklyn.util.http.executor.HttpResponse;
+import org.apache.http.auth.Credentials;
+import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.client.HttpClient;
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.Optional;
+
+@Beta
+public class HttpExecutorImpl implements HttpExecutor {
+
+ private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
+
+ private static final HttpConfig DEFAULT_CONFIG = HttpConfig.builder()
+ .laxRedirect(false)
+ .trustAll(false)
+ .trustSelfSigned(false)
+ .build();
+
+ public static HttpExecutorImpl newInstance() {
+ return newInstance(HttpTool.httpClientBuilder());
+ }
+
+ /**
+ * This {@code baseBuilder} is used to construct a new {@link HttpClient} for each call to {@link #execute(HttpRequest)}.
+ * Callers are strongly discouraged from modifying the baseBuilder after passing it in!
+ */
+ public static HttpExecutorImpl newInstance(HttpTool.HttpClientBuilder baseBuilder) {
+ return new HttpExecutorImpl(baseBuilder);
+ }
+
+ private final HttpClientBuilder baseBuilder;
+
+ protected HttpExecutorImpl(HttpClientBuilder baseBuilder) {
+ this.baseBuilder = baseBuilder;
+ }
+
+ @Override
+ public HttpResponse execute(HttpRequest request) throws IOException {
+ HttpConfig config = (request.config() != null) ? request.config() : DEFAULT_CONFIG;
+ Credentials creds = (request.credentials() != null) ? new UsernamePasswordCredentials(request.credentials().username(), request.credentials().password()) : null;
+ HttpClient httpClient = HttpTool.HttpClientBuilder.fromBuilder(baseBuilder)
+ .uri(request.uri())
+ .https(request.isHttps())
+ .credential(Optional.fromNullable(creds))
+ .laxRedirect(config.laxRedirect())
+ .trustSelfSigned(config.trustSelfSigned())
+ .trustAll(config.trustAll())
+ .build();
--- End diff --
The `baseBuilder` is always overridden by the properties here. Don't see a point of having it at all.
Executors are initialized with map of properties which are not directly related to the requests (i.e. HttpClientBuilder).
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r77348884
--- Diff: core/src/main/java/org/apache/brooklyn/feed/http/HttpFeed.java ---
@@ -331,52 +365,54 @@ protected void preStart() {
}
Callable<HttpToolResponse> pollJob;
-
- if (pollInfo.method.equals("get")) {
- pollJob = new Callable<HttpToolResponse>() {
- public HttpToolResponse call() throws Exception {
- if (log.isTraceEnabled()) log.trace("http polling for {} sensors at {}", entity, pollInfo);
- return HttpTool.httpGet(httpClient, pollInfo.uriProvider.get(), pollInfo.headers);
- }};
- } else if (pollInfo.method.equals("post")) {
- pollJob = new Callable<HttpToolResponse>() {
- public HttpToolResponse call() throws Exception {
- if (log.isTraceEnabled()) log.trace("http polling for {} sensors at {}", entity, pollInfo);
- return HttpTool.httpPost(httpClient, pollInfo.uriProvider.get(), pollInfo.headers, pollInfo.body);
- }};
- } else if (pollInfo.method.equals("head")) {
- pollJob = new Callable<HttpToolResponse>() {
- public HttpToolResponse call() throws Exception {
- if (log.isTraceEnabled()) log.trace("http polling for {} sensors at {}", entity, pollInfo);
- return HttpTool.httpHead(httpClient, pollInfo.uriProvider.get(), pollInfo.headers);
- }};
- } else {
- throw new IllegalStateException("Unexpected http method: "+pollInfo.method);
- }
-
- getPoller().scheduleAtFixedRate(pollJob, new DelegatingPollHandler<HttpToolResponse>(handlers), minPeriod);
- }
- }
+ pollJob = new Callable<HttpToolResponse>() {
+ public HttpToolResponse call() throws Exception {
+ if (log.isTraceEnabled()) log.trace("http polling for {} sensors at {}", entity, pollInfo);
- // TODO Should we really trustAll for https? Make configurable?
- private HttpClient createHttpClient(HttpPollIdentifier pollIdentifier) {
- URI uri = pollIdentifier.uriProvider.get();
- HttpClientBuilder builder = HttpTool.httpClientBuilder()
- .trustAll()
- .laxRedirect(true);
- if (uri != null) builder.uri(uri);
- if (uri != null) builder.credential(pollIdentifier.credentials);
- if (pollIdentifier.connectionTimeout != null) {
- builder.connectionTimeout(pollIdentifier.connectionTimeout);
- }
- if (pollIdentifier.socketTimeout != null) {
- builder.socketTimeout(pollIdentifier.socketTimeout);
+ BasicAuth creds = null;
+ if (pollInfo.credentials.isPresent()) {
+ creds = org.apache.brooklyn.util.http.executor.Credentials.basic(
+ pollInfo.credentials.get().getUserPrincipal().getName(),
+ pollInfo.credentials.get().getPassword());
+ }
+
+ HttpResponse response = pollInfo.httpExecutor.execute(new HttpRequest.Builder()
+ .headers(pollInfo.headers)
+ .uri(pollInfo.uriProvider.get())
+ .credentials(creds)
+ .method(pollInfo.method)
+ .body(pollInfo.body)
+ .config(HttpConfig.builder()
+ .trustSelfSigned(true)
+ .trustAll(true)
+ .laxRedirect(true)
+ .build())
+ .build());
+ return createHttpToolRespose(response);
+ }};
+ getPoller().scheduleAtFixedRate(pollJob, new DelegatingPollHandler(handlers), minPeriod);
}
- return builder.build();
}
@SuppressWarnings("unchecked")
protected Poller<HttpToolResponse> getPoller() {
- return (Poller<HttpToolResponse>) super.getPoller();
+ return (Poller<HttpToolResponse>) super.getPoller();
+ }
+
+ @SuppressWarnings("unchecked")
+ private HttpToolResponse createHttpToolRespose(HttpResponse response) throws IOException {
+ int responseCode = response.code();
+ if (responseCode == 400) { // Unprocessable Entity - https://stackoverflow.com/questions/6123425/rest-response-code-for-invalid-data
+ throw new IOException(" Unprocessable Entity: " + response.reasonPhrase());
+ }
--- End diff --
Why are you interpreting the response here? Let the caller do it - 400 is not any special than other responses. Don't understand the error message as well - which entity are you referring to?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r77511724
--- Diff: utils/common/src/main/java/org/apache/brooklyn/util/http/executor/apacheclient/HttpExecutorImpl.java ---
@@ -0,0 +1,106 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.util.http.executor.apacheclient;
+
+import java.io.IOException;
+
+import org.apache.brooklyn.util.http.HttpTool;
+import org.apache.brooklyn.util.http.HttpTool.HttpClientBuilder;
+import org.apache.brooklyn.util.http.HttpToolResponse;
+import org.apache.brooklyn.util.http.executor.HttpConfig;
+import org.apache.brooklyn.util.http.executor.HttpExecutor;
+import org.apache.brooklyn.util.http.executor.HttpRequest;
+import org.apache.brooklyn.util.http.executor.HttpResponse;
+import org.apache.http.auth.Credentials;
+import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.client.HttpClient;
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.Optional;
+
+@Beta
+public class HttpExecutorImpl implements HttpExecutor {
+
+ private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
+
+ private static final HttpConfig DEFAULT_CONFIG = HttpConfig.builder()
+ .laxRedirect(false)
+ .trustAll(false)
+ .trustSelfSigned(false)
+ .build();
+
+ public static HttpExecutorImpl newInstance() {
+ return newInstance(HttpTool.httpClientBuilder());
+ }
+
+ /**
+ * This {@code baseBuilder} is used to construct a new {@link HttpClient} for each call to {@link #execute(HttpRequest)}.
+ * Callers are strongly discouraged from modifying the baseBuilder after passing it in!
+ */
+ public static HttpExecutorImpl newInstance(HttpTool.HttpClientBuilder baseBuilder) {
+ return new HttpExecutorImpl(baseBuilder);
+ }
+
+ private final HttpClientBuilder baseBuilder;
+
+ protected HttpExecutorImpl(HttpClientBuilder baseBuilder) {
+ this.baseBuilder = baseBuilder;
+ }
+
+ @Override
+ public HttpResponse execute(HttpRequest request) throws IOException {
+ HttpConfig config = (request.config() != null) ? request.config() : DEFAULT_CONFIG;
+ Credentials creds = (request.credentials() != null) ? new UsernamePasswordCredentials(request.credentials().username(), request.credentials().password()) : null;
+ HttpClient httpClient = HttpTool.HttpClientBuilder.fromBuilder(baseBuilder)
+ .uri(request.uri())
+ .https(request.isHttps())
+ .credential(Optional.fromNullable(creds))
+ .laxRedirect(config.laxRedirect())
+ .trustSelfSigned(config.trustSelfSigned())
+ .trustAll(config.trustAll())
+ .build();
--- End diff --
Who is supposed to pass the builder to the executor? The contract is to pass a map of options to the constructor. The `baseBuilder` is always empty.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by ygy <gi...@git.apache.org>.
Github user ygy closed the pull request at:
https://github.com/apache/brooklyn-server/pull/285
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by bostko <gi...@git.apache.org>.
Github user bostko commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r75928906
--- Diff: core/src/main/java/org/apache/brooklyn/feed/http/HttpFeed.java ---
@@ -222,15 +238,17 @@ protected void finalize() {
}
private static class HttpPollIdentifier {
+ final HttpExecutor httpExecutor;
--- End diff --
@ygy you do not need it here.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r75689508
--- Diff: utils/common/src/main/java/org/apache/brooklyn/util/http/HttpExecutorFactory.java ---
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.util.http;
+
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.apache.brooklyn.util.collections.MutableMap;
+import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.http.executor.HttpExecutor;
+import org.apache.brooklyn.util.javalang.Reflections;
+import org.apache.brooklyn.util.text.StringPredicates;
+import org.apache.brooklyn.util.text.Strings;
+
+import com.google.common.base.Optional;
+import com.google.common.collect.Iterables;
+
+public class HttpExecutorFactory {
+ public static final String HTTP_EXECUTOR_CLASS = "httpExecutorClass";
+
+ public static final String HTTP_EXECUTOR_CLASS_PROPERTIES_PREFIX = HTTP_EXECUTOR_CLASS + ".";
+
+ private HttpExecutorFactory() {
+
+ }
+
+ public <T, S> HttpExecutor getHttpExecutor(Map<?, ?> props) {
+ HttpExecutor httpExecutor;
+
+ Optional<Map<?,?>> executorProps = (Optional<Map<?, ?>>) Iterables.tryFind(props.keySet(), StringPredicates.isStringStartingWith(HTTP_EXECUTOR_CLASS_PROPERTIES_PREFIX));
+
+ if (executorProps.isPresent()) {
+ Map<?, ?> instanceProps = executorProps.get();
+
+ String httpExecutorClass = (String) instanceProps.get(HTTP_EXECUTOR_CLASS);
+ Map<String, String> httpExecutorProps = MutableMap.of();
+ for (Entry<?, ?> entry: instanceProps.entrySet()) {
+ String keyName = Strings.removeFromStart((String)entry.getKey(), HTTP_EXECUTOR_CLASS_PROPERTIES_PREFIX);
+ httpExecutorProps.put(keyName, (String)entry.getValue());
+ }
+
+ Reflections reflections = new Reflections(HttpExecutorFactory.class.getClassLoader());
+ httpExecutor = (HttpExecutor) reflections.loadInstance(httpExecutorClass, httpExecutorProps);
--- End diff --
Don't see this constructor in `HttpExecutorImpl`
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r77347841
--- Diff: core/src/main/java/org/apache/brooklyn/feed/http/HttpFeed.java ---
@@ -275,7 +294,23 @@ public HttpFeed() {
protected HttpFeed(Builder builder) {
setConfig(ONLY_IF_SERVICE_UP, builder.onlyIfServiceUp);
Map<String,String> baseHeaders = ImmutableMap.copyOf(checkNotNull(builder.headers, "headers"));
-
+
+ HttpExecutor httpExecutor;
+ if (builder.httpExecutor != null) {
+ httpExecutor = builder.httpExecutor;
+ } else {
+ HttpExecutorFactory httpExecutorFactory = null;
+ Collection<? extends Location> locations = Locations.getLocationsCheckingAncestors(builder.entity.getLocations(), builder.entity);
+ Maybe<Location> location = Machines.findUniqueElement(locations, Location.class);
--- End diff --
All of the elements here are of type `Location`. It's possible to have two locations per entity - one coming from `location: xx` property in YAML (which is the description of how to provision a new machine) and another `SshMachineLocation` (which points to a specific VM with IPs created from the previous one). I believe only the former will have the extensions when coming from yaml (they will not be inherited). Looking for the first location to have the required extension should be good enough.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r77535007
--- Diff: core/src/main/java/org/apache/brooklyn/feed/http/HttpFeed.java ---
@@ -331,52 +366,55 @@ protected void preStart() {
}
Callable<HttpToolResponse> pollJob;
-
- if (pollInfo.method.equals("get")) {
- pollJob = new Callable<HttpToolResponse>() {
- public HttpToolResponse call() throws Exception {
- if (log.isTraceEnabled()) log.trace("http polling for {} sensors at {}", entity, pollInfo);
- return HttpTool.httpGet(httpClient, pollInfo.uriProvider.get(), pollInfo.headers);
- }};
- } else if (pollInfo.method.equals("post")) {
- pollJob = new Callable<HttpToolResponse>() {
- public HttpToolResponse call() throws Exception {
- if (log.isTraceEnabled()) log.trace("http polling for {} sensors at {}", entity, pollInfo);
- return HttpTool.httpPost(httpClient, pollInfo.uriProvider.get(), pollInfo.headers, pollInfo.body);
- }};
- } else if (pollInfo.method.equals("head")) {
- pollJob = new Callable<HttpToolResponse>() {
- public HttpToolResponse call() throws Exception {
- if (log.isTraceEnabled()) log.trace("http polling for {} sensors at {}", entity, pollInfo);
- return HttpTool.httpHead(httpClient, pollInfo.uriProvider.get(), pollInfo.headers);
- }};
- } else {
- throw new IllegalStateException("Unexpected http method: "+pollInfo.method);
- }
-
- getPoller().scheduleAtFixedRate(pollJob, new DelegatingPollHandler<HttpToolResponse>(handlers), minPeriod);
- }
- }
+ pollJob = new Callable<HttpToolResponse>() {
+ public HttpToolResponse call() throws Exception {
+ if (log.isTraceEnabled()) log.trace("http polling for {} sensors at {}", entity, pollInfo);
- // TODO Should we really trustAll for https? Make configurable?
- private HttpClient createHttpClient(HttpPollIdentifier pollIdentifier) {
- URI uri = pollIdentifier.uriProvider.get();
- HttpClientBuilder builder = HttpTool.httpClientBuilder()
- .trustAll()
- .laxRedirect(true);
- if (uri != null) builder.uri(uri);
- if (uri != null) builder.credential(pollIdentifier.credentials);
- if (pollIdentifier.connectionTimeout != null) {
- builder.connectionTimeout(pollIdentifier.connectionTimeout);
- }
- if (pollIdentifier.socketTimeout != null) {
- builder.socketTimeout(pollIdentifier.socketTimeout);
+ UsernamePassword creds = null;
+ if (pollInfo.credentials.isPresent()) {
+ creds = new UsernamePassword(
+ pollInfo.credentials.get().getUserPrincipal().getName(),
+ pollInfo.credentials.get().getPassword());
+ }
+
+ HttpResponse response = pollInfo.httpExecutor.execute(new HttpRequest.Builder()
+ .headers(pollInfo.headers)
+ .uri(pollInfo.uriProvider.get())
+ .credentials(creds)
+ .method(pollInfo.method)
+ .body(pollInfo.body)
+ .config(HttpConfig.builder()
+ .trustSelfSigned(true)
+ .trustAll(true)
+ .laxRedirect(true)
+ .build())
+ .build());
+ return createHttpToolRespose(response);
+ }};
+ getPoller().scheduleAtFixedRate(pollJob, new DelegatingPollHandler(handlers), minPeriod);
}
- return builder.build();
}
@SuppressWarnings("unchecked")
protected Poller<HttpToolResponse> getPoller() {
- return (Poller<HttpToolResponse>) super.getPoller();
+ return (Poller<HttpToolResponse>) super.getPoller();
+ }
+
+ @SuppressWarnings("unchecked")
+ private HttpToolResponse createHttpToolRespose(HttpResponse response) throws IOException {
+ int responseCode = response.code();
+
+ Map<String,? extends List<String>> headers = (Map<String, List<String>>) (Map<?, ?>) response.headers().asMap();
+
+ byte[] content = null;
+ if (response.getContentLength() > 0) {
--- End diff --
Do we always know the content length? Chunked response doesn't set the content-length in the headers.
Can we get the content always, regardless of content length?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by ygy <gi...@git.apache.org>.
Github user ygy commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r76594227
--- Diff: utils/common/src/main/java/org/apache/brooklyn/util/http/executor/HttpRequest.java ---
@@ -0,0 +1,128 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.util.http.executor;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.net.URI;
+import java.util.Map;
+
+import javax.annotation.Nullable;
+
+import com.google.common.annotations.Beta;
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.Multimap;
+
+@Beta
+public interface HttpRequest {
+
+ // TODO Should we use InputStream for body (rather than byte[]). That would mean we don't have
+ // to hold the entire body in-memory, which might be important for really big payloads!
--- End diff --
That's a very good idea. I'll create a new ticket for the subsequent extending the API.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server issue #285: Adding HttpExecutor to be used for interchangeab...
Posted by bostko <gi...@git.apache.org>.
Github user bostko commented on the issue:
https://github.com/apache/brooklyn-server/pull/285
A few fixes in here https://github.com/ygy/brooklyn-server/pull/3
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r75685586
--- Diff: utils/common/src/main/java/org/apache/brooklyn/util/http/executor/HttpRequest.java ---
@@ -0,0 +1,128 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.util.http.executor;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.net.URI;
+import java.util.Map;
+
+import javax.annotation.Nullable;
+
+import com.google.common.annotations.Beta;
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.Multimap;
+
+@Beta
+public interface HttpRequest {
+
+ // TODO Should we use InputStream for body (rather than byte[]). That would mean we don't have
+ // to hold the entire body in-memory, which might be important for really big payloads!
--- End diff --
+1
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r77499318
--- Diff: core/src/main/java/org/apache/brooklyn/core/location/AbstractLocation.java ---
@@ -718,6 +719,35 @@ public boolean removeChild(Location child) {
return removed;
}
+ public void init() {
+ super.init();
+ loadExtension();
+ }
+
+ public void rebind() {
+ super.rebind();
+ loadExtension();
+ }
+
+ private void loadExtension() {
+ try {
+ Map<String, String> extensions = getConfig(LocationConfigKeys.EXTENSIONS);
+ if (extensions != null) {
+ for (Map.Entry<String, String> extension: extensions.entrySet()) {
+ Class<?> extensionClassType = new ClassLoaderUtils(this, getManagementContext()).loadClass(extension.getKey());
+
+ if (!hasExtension(extensionClassType)) {
+ Object extensionClass = new ClassLoaderUtils(this, getManagementContext()).loadClass(extension.getValue()).newInstance();
+ addExtension((Class)extensionClassType, extensionClass);
+ }
+ }
+ }
+ } catch (Exception e) {
+ LOG.error("Location extension can not be loaded (rethrowing): {}", new Object[] {e});
--- End diff --
Could catch + rethrow inside the loop to get access to the failing extension pair and log it.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by ygy <gi...@git.apache.org>.
GitHub user ygy reopened a pull request:
https://github.com/apache/brooklyn-server/pull/285
Adding HttpExecutor to be used for interchangeable implementations
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/ygy/brooklyn-server feature/http-executor
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/brooklyn-server/pull/285.patch
To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:
This closes #285
----
commit 776a923c229fab34039d73ab078bc264b0cb0c2f
Author: Aled Sage <al...@gmail.com>
Date: 2016-08-09T13:41:37Z
Adds HttpExecutor interface
commit e785c29e8b7d298f22c0d7d44e993264f4dca0a5
Author: Aled Sage <al...@gmail.com>
Date: 2016-08-09T13:41:58Z
Adds apache-httpclient impl of HttpExecutor
commit 5faa686bfc4752554eaf20facdffbce6d0850673
Author: Yavor Yanchev <ya...@yanchev.com>
Date: 2016-08-16T06:39:48Z
Refactoring HttpFeed to use the new HttpExecutor interface.
commit 3a4f870ff2d6bf4087609f343754dc436030e789
Author: Yavor Yanchev <ya...@yanchev.com>
Date: 2016-08-29T12:09:15Z
Improving error log handling
commit acfd95625da146da267435cde8a70e41f6ee8242
Author: Valentin Aitken <bo...@gmail.com>
Date: 2016-09-01T11:39:37Z
No https
commit a22d622b468f98b8c18465289c2a87dc1a2db724
Author: Yavor Yanchev <ya...@yanchev.com>
Date: 2016-09-02T16:49:55Z
Adding HttpExecutorImplTest
commit 2903a55ad3405993158ce66fcf0d81dbe379040f
Author: Yavor Yanchev <ya...@yanchev.com>
Date: 2016-09-02T17:40:39Z
Addressing PR comments
commit d71602c00e63435337146ed6288bfc4f5aca6e2a
Author: Valentin Aitken <bo...@gmail.com>
Date: 2016-09-02T19:27:10Z
Add more assertions in HttpExecutorImplTest
commit 3a1861ec37b8fda6aa99244effcd246bf26fcbcb
Author: Yavor Yanchev <ya...@yanchev.com>
Date: 2016-09-03T12:12:41Z
Lowering the log level to info
----
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] brooklyn-server pull request #285: Adding HttpExecutor to be used for interc...
Posted by bostko <gi...@git.apache.org>.
Github user bostko commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/285#discussion_r76590901
--- Diff: utils/common/src/main/java/org/apache/brooklyn/util/http/HttpTool.java ---
@@ -250,6 +254,21 @@ public static HttpClientBuilder httpClientBuilder() {
private boolean trustAll;
private boolean trustSelfSigned;
+ public static HttpClientBuilder fromBuilder(HttpClientBuilder other) {
+ HttpClientBuilder result = httpClientBuilder();
+ result.clientConnectionManager = other.clientConnectionManager;
+ result.httpParams = other.httpParams;
--- End diff --
HttpClient should be configured with `clientConnectionManager` possibly the `HttpExecutorFactory` and other similar client properties.
A client can be used against multiple endpoints so including `uri`, `port` and `credentials` is not appropriate for the HttpClientBulder in my opinion.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---