-
Notifications
You must be signed in to change notification settings - Fork 528
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Docker push works with Podman REST API
Signed-off-by: Marc Nuri <[email protected]>
- Loading branch information
Showing
5 changed files
with
137 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...org/eclipse/jkube/kit/build/service/docker/access/hc/HcChunkedResponseHandlerWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* Copyright (c) 2019 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at: | ||
* | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.jkube.kit.build.service.docker.access.hc; | ||
|
||
import org.apache.http.HttpResponse; | ||
import org.apache.http.client.ResponseHandler; | ||
import org.eclipse.jkube.kit.build.service.docker.access.chunked.EntityStreamReaderUtil; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.stream.Stream; | ||
|
||
class HcChunkedResponseHandlerWrapper implements ResponseHandler<Object> { | ||
|
||
private final EntityStreamReaderUtil.JsonEntityResponseHandler handler; | ||
|
||
HcChunkedResponseHandlerWrapper(EntityStreamReaderUtil.JsonEntityResponseHandler handler) { | ||
this.handler = handler; | ||
} | ||
|
||
@Override | ||
public Object handleResponse(HttpResponse response) throws IOException { | ||
try (InputStream stream = response.getEntity().getContent()) { | ||
// Parse text as json | ||
if (isJson(response)) { | ||
EntityStreamReaderUtil.processJsonStream(handler, stream); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private static boolean isJson(HttpResponse response) { | ||
return Stream.of(response.getAllHeaders()) | ||
.filter(h -> h.getName().equalsIgnoreCase("Content-Type")) | ||
.anyMatch(h -> h.getValue().toLowerCase().startsWith("application/json")); | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
...eclipse/jkube/kit/build/service/docker/access/hc/HcChunkedResponseHandlerWrapperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* Copyright (c) 2019 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at: | ||
* | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.jkube.kit.build.service.docker.access.hc; | ||
|
||
import java.io.IOException; | ||
|
||
import org.eclipse.jkube.kit.build.service.docker.access.chunked.EntityStreamReaderUtil; | ||
|
||
import mockit.Expectations; | ||
import mockit.Mocked; | ||
import mockit.Verifications; | ||
import org.apache.http.Header; | ||
import org.apache.http.HttpResponse; | ||
import org.apache.http.message.BasicHeader; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
@SuppressWarnings("unused") | ||
public class HcChunkedResponseHandlerWrapperTest { | ||
|
||
@Mocked | ||
private EntityStreamReaderUtil.JsonEntityResponseHandler handler; | ||
@Mocked | ||
private HttpResponse response; | ||
@Mocked | ||
private EntityStreamReaderUtil entityStreamReaderUtil; | ||
|
||
private Header[] headers; | ||
private HcChunkedResponseHandlerWrapper hcChunkedResponseHandlerWrapper; | ||
|
||
@Before | ||
public void setUp() { | ||
hcChunkedResponseHandlerWrapper = new HcChunkedResponseHandlerWrapper(handler); | ||
|
||
} | ||
|
||
@Test | ||
public void handleResponseWithJsonResponse() throws IOException { | ||
givenResponseHeaders(new BasicHeader("ConTenT-Type", "application/json; charset=UTF-8")); | ||
hcChunkedResponseHandlerWrapper.handleResponse(response); | ||
verifyProcessJsonStream(1); | ||
} | ||
|
||
@Test | ||
public void handleResponseWithTextPlainResponse() throws IOException { | ||
givenResponseHeaders(new BasicHeader("Content-Type", "text/plain")); | ||
hcChunkedResponseHandlerWrapper.handleResponse(response); | ||
verifyProcessJsonStream(0); | ||
} | ||
|
||
@Test | ||
public void handleResponseWithNoContentType() throws IOException { | ||
givenResponseHeaders(); | ||
hcChunkedResponseHandlerWrapper.handleResponse(response); | ||
verifyProcessJsonStream(0); | ||
} | ||
|
||
private void givenResponseHeaders(Header... headers) { | ||
// @formatter:off | ||
new Expectations() {{ | ||
response.getAllHeaders(); result = headers; | ||
}}; | ||
// @formatter:on | ||
} | ||
|
||
@SuppressWarnings("AccessStaticViaInstance") | ||
private void verifyProcessJsonStream(int timesCalled) throws IOException { | ||
// @formatter:off | ||
new Verifications() {{ | ||
entityStreamReaderUtil.processJsonStream(handler, response.getEntity().getContent()); times = timesCalled; | ||
}}; | ||
// @formatter:on | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters