@Override public IFcrepoResponse execute() throws FcrepoOperationFailedException { final HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) new HttpPatch(uri); if (ifMatch != null) { request.setHeader("If-Match", ifMatch); } if (ifUnmodifiedSince != null) { request.setHeader("If-Unmodified-Since", ifUnmodifiedSince); } if (contentLocation != null) { request.setHeader("Content-Location", contentLocation); } if (body != null) { request.setEntity(new InputStreamEntity(body)); } return executeRequest(request); }
private <T> void writeContent( final Edm edm, HttpEntityEnclosingRequestBase httpEntityRequest, final UriInfoWithType uriInfo, final Object content, final Olingo2ResponseHandler<T> responseHandler) { try { // process resource by UriType final ODataResponse response = writeContent(edm, uriInfo, content); // copy all response headers for (String header : response.getHeaderNames()) { httpEntityRequest.setHeader(header, response.getHeader(header)); } // get (http) entity which is for default Olingo2 implementation an InputStream if (response.getEntity() instanceof InputStream) { httpEntityRequest.setEntity(new InputStreamEntity((InputStream) response.getEntity())); /* // avoid sending it without a header field set if (!httpEntityRequest.containsHeader(HttpHeaders.CONTENT_TYPE)) { httpEntityRequest.addHeader(HttpHeaders.CONTENT_TYPE, getContentType()); } */ } // execute HTTP request final Header requestContentTypeHeader = httpEntityRequest.getFirstHeader(HttpHeaders.CONTENT_TYPE); final ContentType requestContentType = requestContentTypeHeader != null ? ContentType.parse(requestContentTypeHeader.getValue()) : contentType; execute( httpEntityRequest, requestContentType, new AbstractFutureCallback<T>(responseHandler) { @SuppressWarnings("unchecked") @Override public void onCompleted(HttpResponse result) throws IOException, EntityProviderException, BatchException, ODataApplicationException { // if a entity is created (via POST request) the response body contains the new // created entity HttpStatusCodes statusCode = HttpStatusCodes.fromStatusCode(result.getStatusLine().getStatusCode()); // look for no content, or no response body!!! final boolean noEntity = result.getEntity() == null || result.getEntity().getContentLength() == 0; if (statusCode == HttpStatusCodes.NO_CONTENT || noEntity) { responseHandler.onResponse( (T) HttpStatusCodes.fromStatusCode(result.getStatusLine().getStatusCode())); } else { switch (uriInfo.getUriType()) { case URI9: // $batch final List<BatchSingleResponse> singleResponses = EntityProvider.parseBatchResponse( result.getEntity().getContent(), result.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue()); // parse batch response bodies final List<Olingo2BatchResponse> responses = new ArrayList<Olingo2BatchResponse>(); Map<String, String> contentIdLocationMap = new HashMap<String, String>(); final List<Olingo2BatchRequest> batchRequests = (List<Olingo2BatchRequest>) content; final Iterator<Olingo2BatchRequest> iterator = batchRequests.iterator(); for (BatchSingleResponse response : singleResponses) { final Olingo2BatchRequest request = iterator.next(); if (request instanceof Olingo2BatchChangeRequest && ((Olingo2BatchChangeRequest) request).getContentId() != null) { contentIdLocationMap.put( "$" + ((Olingo2BatchChangeRequest) request).getContentId(), response.getHeader(HttpHeaders.LOCATION)); } try { responses.add(parseResponse(edm, contentIdLocationMap, request, response)); } catch (Exception e) { // report any parsing errors as error response responses.add( new Olingo2BatchResponse( Integer.parseInt(response.getStatusCode()), response.getStatusInfo(), response.getContentId(), response.getHeaders(), new ODataApplicationException( "Error parsing response for " + request + ": " + e.getMessage(), Locale.ENGLISH, e))); } } responseHandler.onResponse((T) responses); break; case URI4: case URI5: // simple property // get the response content as Object for $value or Map<String, Object> // otherwise final List<EdmProperty> simplePropertyPath = uriInfo.getPropertyPath(); final EdmProperty simpleProperty = simplePropertyPath.get(simplePropertyPath.size() - 1); if (uriInfo.isValue()) { responseHandler.onResponse( (T) EntityProvider.readPropertyValue( simpleProperty, result.getEntity().getContent())); } else { responseHandler.onResponse( (T) EntityProvider.readProperty( getContentType(), simpleProperty, result.getEntity().getContent(), EntityProviderReadProperties.init().build())); } break; case URI3: // complex property // get the response content as Map<String, Object> final List<EdmProperty> complexPropertyPath = uriInfo.getPropertyPath(); final EdmProperty complexProperty = complexPropertyPath.get(complexPropertyPath.size() - 1); responseHandler.onResponse( (T) EntityProvider.readProperty( getContentType(), complexProperty, result.getEntity().getContent(), EntityProviderReadProperties.init().build())); break; case URI7A: // $links with 0..1 cardinality property // get the response content as String final EdmEntitySet targetLinkEntitySet = uriInfo.getTargetEntitySet(); responseHandler.onResponse( (T) EntityProvider.readLink( getContentType(), targetLinkEntitySet, result.getEntity().getContent())); break; case URI7B: // $links with * cardinality property // get the response content as java.util.List<String> final EdmEntitySet targetLinksEntitySet = uriInfo.getTargetEntitySet(); responseHandler.onResponse( (T) EntityProvider.readLinks( getContentType(), targetLinksEntitySet, result.getEntity().getContent())); break; case URI1: case URI2: case URI6A: case URI6B: // Entity // get the response content as an ODataEntry object responseHandler.onResponse( (T) EntityProvider.readEntry( response.getContentHeader(), uriInfo.getTargetEntitySet(), result.getEntity().getContent(), EntityProviderReadProperties.init().build())); break; default: throw new ODataApplicationException( "Unsupported resource type " + uriInfo.getTargetType(), Locale.ENGLISH); } } } }); } catch (ODataException e) { responseHandler.onException(e); } catch (URISyntaxException e) { responseHandler.onException(e); } catch (UnsupportedEncodingException e) { responseHandler.onException(e); } catch (IOException e) { responseHandler.onException(e); } }
/** Deploys an application with (optionally) a defined app name and app version */ protected HttpResponse deploy( Class<?> application, @Nullable String apiVersion, @Nullable String namespace, @Nullable String appName, @Nullable String appVersion, @Nullable Config appConfig) throws Exception { namespace = namespace == null ? Constants.DEFAULT_NAMESPACE : namespace; apiVersion = apiVersion == null ? Constants.Gateway.API_VERSION_3_TOKEN : apiVersion; Manifest manifest = new Manifest(); manifest.getMainAttributes().put(ManifestFields.MANIFEST_VERSION, "1.0"); manifest.getMainAttributes().put(ManifestFields.MAIN_CLASS, application.getName()); if (appVersion != null) { manifest.getMainAttributes().put(ManifestFields.BUNDLE_VERSION, appVersion); } ByteArrayOutputStream bos = new ByteArrayOutputStream(); final JarOutputStream jarOut = new JarOutputStream(bos, manifest); final String pkgName = application.getPackage().getName(); // Grab every classes under the application class package. try { ClassLoader classLoader = application.getClassLoader(); if (classLoader == null) { classLoader = ClassLoader.getSystemClassLoader(); } Dependencies.findClassDependencies( classLoader, new ClassAcceptor() { @Override public boolean accept(String className, URL classUrl, URL classPathUrl) { try { if (className.startsWith(pkgName)) { jarOut.putNextEntry(new JarEntry(className.replace('.', '/') + ".class")); InputStream in = classUrl.openStream(); try { ByteStreams.copy(in, jarOut); } finally { in.close(); } return true; } return false; } catch (Exception e) { throw Throwables.propagate(e); } } }, application.getName()); // Add webapp jarOut.putNextEntry(new ZipEntry("webapp/default/netlens/src/1.txt")); ByteStreams.copy(new ByteArrayInputStream("dummy data".getBytes(Charsets.UTF_8)), jarOut); } finally { jarOut.close(); } HttpEntityEnclosingRequestBase request; String versionedApiPath = getVersionedAPIPath("apps/", apiVersion, namespace); if (appName == null) { request = getPost(versionedApiPath); } else { request = getPut(versionedApiPath + appName); } request.setHeader(Constants.Gateway.API_KEY, "api-key-example"); request.setHeader("X-Archive-Name", application.getSimpleName() + ".jar"); if (appConfig != null) { request.setHeader("X-App-Config", GSON.toJson(appConfig)); } request.setEntity(new ByteArrayEntity(bos.toByteArray())); return execute(request); }