/** * Format the given parameter object into string. * * @param param Parameter * @return String representation of the parameter */ public String parameterToString(Object param) { if (param == null) { return ""; } else if (param instanceof Date || param instanceof DateTime || param instanceof LocalDate) { //Serialize to json string and remove the " enclosing characters String jsonStr = json.serialize(param); return jsonStr.substring(1, jsonStr.length() - 1); } else if (param instanceof Collection) { StringBuilder b = new StringBuilder(); for (Object o : (Collection)param) { if (b.length() > 0) { b.append(","); } b.append(String.valueOf(o)); } return b.toString(); } else { return String.valueOf(param); } }
/** * Format the given parameter object into string. * * @param param Parameter * @return String representation of the parameter */ public String parameterToString(Object param) { if (param == null) { return ""; } else if (param instanceof Date || param instanceof DateTime || param instanceof LocalDate) { //Serialize to json string and remove the " enclosing characters String jsonStr = json.serialize(param); return jsonStr.substring(1, jsonStr.length() - 1); } else if (param instanceof Collection) { StringBuilder b = new StringBuilder(); for (Object o : (Collection)param) { if (b.length() > 0) { b.append(","); } b.append(String.valueOf(o)); } return b.toString(); } else { return String.valueOf(param); } }
@Test public void testSerializeByteArray() { final JSON json = new JSON(); final String plainText = "string that contains '=' when encoded"; final String base64String = json.serialize(plainText.getBytes()); //serialize returns string surrounded by quotes: "\"[base64]\"" final String pureString = base64String.replaceAll("^\"|\"$", ""); final ByteString byteStr = ByteString.decodeBase64(pureString); //Check encoded to valid base64 assertNotNull(byteStr); //Check encoded string correctly final String decodedText = new String(byteStr.toByteArray()); assertThat(decodedText, is(plainText)); } }
/** * Serialize the given Java object into request body according to the object's * class and the request Content-Type. * * @param obj The Java object * @param contentType The request Content-Type * @return The serialized request body * @throws ApiException If fail to serialize the given object */ public RequestBody serialize(Object obj, String contentType) throws ApiException { if (obj instanceof byte[]) { // Binary (byte array) body parameter support. return RequestBody.create(MediaType.parse(contentType), (byte[]) obj); } else if (obj instanceof File) { // File body parameter support. return RequestBody.create(MediaType.parse(contentType), (File) obj); } else if (isJsonMime(contentType)) { String content; if (obj != null) { content = json.serialize(obj); } else { content = null; } return RequestBody.create(MediaType.parse(contentType), content); } else { throw new ApiException("Content type \"" + contentType + "\" is not supported"); } }
/** * Serialize the given Java object into request body according to the object's * class and the request Content-Type. * * @param obj The Java object * @param contentType The request Content-Type * @return The serialized request body * @throws ApiException If fail to serialize the given object */ public RequestBody serialize(Object obj, String contentType) throws ApiException { if (obj instanceof byte[]) { // Binary (byte array) body parameter support. return RequestBody.create(MediaType.parse(contentType), (byte[]) obj); } else if (obj instanceof File) { // File body parameter support. return RequestBody.create(MediaType.parse(contentType), (File) obj); } else if (isJsonMime(contentType)) { String content; if (obj != null) { content = json.serialize(obj); } else { content = null; } return RequestBody.create(MediaType.parse(contentType), content); } else { throw new ApiException("Content type \"" + contentType + "\" is not supported"); } }
private void submitStatefulSet() throws Exception { final V1StatefulSet statefulSet = createStatefulSet(); log.info("Submitting the following spec to k8 {}", appsClient.getApiClient().getJSON().serialize(statefulSet)); final Response response = appsClient.createNamespacedStatefulSetCall(jobNamespace, statefulSet, null, null, null).execute(); if (!response.isSuccessful()) { if (response.code() == HTTP_CONFLICT) { log.warn("Statefulset already present for function {}/{}/{}", instanceConfig.getFunctionDetails().getTenant(), instanceConfig.getFunctionDetails().getNamespace(), instanceConfig.getFunctionDetails().getName()); } else { log.error("Error creating statefulset for function {}/{}/{}:- {}", instanceConfig.getFunctionDetails().getTenant(), instanceConfig.getFunctionDetails().getNamespace(), instanceConfig.getFunctionDetails().getName(), response.message()); // construct a message based on the k8s api server response throw new IllegalStateException(response.message()); } } else { log.info("Successfully created statefulset for function {}/{}/{}", instanceConfig.getFunctionDetails().getTenant(), instanceConfig.getFunctionDetails().getNamespace(), instanceConfig.getFunctionDetails().getName()); } }
private void submitService() throws Exception { final V1Service service = createService(); log.info("Submitting the following service to k8 {}", coreClient.getApiClient().getJSON().serialize(service)); final Response response = coreClient.createNamespacedServiceCall(jobNamespace, service, null, null, null).execute(); if (!response.isSuccessful()) { if (response.code() == HTTP_CONFLICT) { log.warn("Service already created for function {}/{}/{}", instanceConfig.getFunctionDetails().getTenant(), instanceConfig.getFunctionDetails().getNamespace(), instanceConfig.getFunctionDetails().getName()); } else { log.error("Error creating Service for function {}/{}/{}:- {}", instanceConfig.getFunctionDetails().getTenant(), instanceConfig.getFunctionDetails().getNamespace(), instanceConfig.getFunctionDetails().getName(), response.message()); // construct a message based on the k8s api server response throw new IllegalStateException(response.message()); } } else { log.info("Service Created Successfully for function {}/{}/{}", instanceConfig.getFunctionDetails().getTenant(), instanceConfig.getFunctionDetails().getNamespace(), instanceConfig.getFunctionDetails().getName()); } }