private boolean isJsonContent(final String contentType) { return Str.isEmpty(contentType) || contentType.equals("application/json") || contentType.equals("application/json;charset=utf-8") || contentType.startsWith("application/json"); } }
/** * Finds the admin port. * Searches under environment variables, * {@code QBIT_ADMIN_PORT}, {@code ADMIN_PORT}, {@code PORT1}. * * @return admin port */ public String findAdminPort() { String qbitAdminPort = System.getenv("QBIT_ADMIN_PORT"); if (Str.isEmpty(qbitAdminPort)) { qbitAdminPort = System.getenv("ADMIN_PORT"); } /* Uses PORT1 for admin port for Mesosphere / Heroku like environments. */ if (Str.isEmpty(qbitAdminPort)) { qbitAdminPort = System.getenv("PORT1"); } return qbitAdminPort; }
/** * Get the public port for service meta generation (Swagger) * * @return public port */ public int getPublicPort() { if (publicPort == -1) { String sport = System.getenv("PUBLIC_WEB_PORT"); if (!Str.isEmpty(sport)) { publicPort = Integer.parseInt(sport); } } if (publicPort == -1) { publicPort = getPort(); } return publicPort; }
private String readAddressFromAnnotation(Annotated annotated) { String address = getAddress("RequestMapping", annotated); if (Str.isEmpty(address)) { address = getAddress("Name", annotated); } if (Str.isEmpty(address)) { address = getAddress("Service", annotated); } if (Str.isEmpty(address)) { address = getAddress("ServiceMethod", annotated); } return address == null ? "" : address; }
public static String readServiceName(Annotated annotated) { String name = readValue("Service", annotated); if (Str.isEmpty(name)) { name = readValue("ServiceName", annotated); } if (Str.isEmpty(name)) { name = readValue("Named", annotated); } if (Str.isEmpty(name)) { name = readValue("Name", annotated); } return name == null ? "" : name; }
private String readNameFromAnnotation(Annotated annotated) { String name = getAddress("Named", annotated); if (Str.isEmpty(name)) { name = getAddress("Name", annotated); } if (Str.isEmpty(name)) { name = getAddress("Service", annotated); } if (Str.isEmpty(name)) { name = getAddress("ServiceName", annotated); } return name == null ? "" : name; }
public static <T> String getClassEventChannelName(ClassMeta<T> classMeta, AnnotationData classAnnotation) { //They could even use enum as we are getting a string value String classEventBusName = classAnnotation != null && classAnnotation.getValues().get("value") != null ? classAnnotation.getValues().get("value").toString() : null; if (Str.isEmpty(classEventBusName) && classAnnotation != null) { classEventBusName = classMeta.longName(); } return classEventBusName; }
/** * Extract headersToAddToLoggingMappingDiagnosticsContext data and put them into the logging mapping diagnostics context. * * @param httpRequest httpRequest */ private void extractHeaders(final HttpRequest httpRequest) { if (headersToAddToLoggingMappingDiagnosticsContext.size() > 0) { final MultiMap<String, String> headers = httpRequest.getHeaders(); headersToAddToLoggingMappingDiagnosticsContext.forEach(header -> { String value = headers.getFirst(header); if (!Str.isEmpty(value)) { MDC.put(REQUEST_HEADER_PREFIX + header, value); } }); } }
private RequestMetaData doGet(final String path) { RequestMetaData requestMetaData = metaDataMap.get(path); if (requestMetaData == null) { Map.Entry<String, NavigableMap<Integer, RequestMetaData>> uriParamNumMapEntry = treeMap.lowerEntry(path); if (uriParamNumMapEntry == null) { return null; } final String requestURI = Str.isEmpty(rootURI) ? path : StringScanner.substringAfter(path, rootURI); int count = Str.split(requestURI, '/').length - 1; NavigableMap<Integer, RequestMetaData> uriParamMap = uriParamNumMapEntry.getValue(); requestMetaData = uriParamMap.get(count); if (requestMetaData != null && path.startsWith(requestMetaData.getPath())) { return requestMetaData; } else { return null; } } else { return requestMetaData; } }
private String getServiceName(ClassMeta<?> classMeta) { String name = AnnotationUtils.readServiceName(classMeta); if (Str.isEmpty(name)) { name = Str.camelCaseLower(classMeta.name()); } return name; }
/** * @return http server builder */ public HttpServerBuilder getHttpServerBuilder() { if (httpServerBuilder == null) { httpServerBuilder = HttpServerBuilder.httpServerBuilder(); int port = getPort(); if (!Str.isEmpty(port)) { httpServerBuilder.setPort(port); } } return httpServerBuilder; }
private void extractEventListenerFromMethod(final Object listener, final MethodAccess methodAccess, final AnnotationData listen, final ServiceQueue serviceQueue) { logger.info("EventManager {} ::extractEventListenerFromMethod :: " + "{} is listening with method {} using annotation data {} ", name, serviceQueue, methodAccess.name(), listen.getValues()); final String channel = listen.getValues().get("value").toString(); if (Str.isEmpty(channel)) { return; } final boolean consume = (boolean) listen.getValues().get("consume"); if (serviceQueue == null) { extractListenerForRegularObject(listener, methodAccess, channel, consume); } else { extractListenerForService(serviceQueue, channel, consume); } }
public static <T> ConsulResponse<T> consulResponse(final Class<T> responseType, final HTTP.Response response) { T responseObject = null; if (response.code() == 200) { if (!Str.isEmpty(response.body())) { responseObject = fromJson(response.body(), responseType); } } else { die("Unable to read response", response.code(), response.body()); } List<String> strings = response.headers().get("X-Consul-Index"); int index = Integer.valueOf(strings.get(0)); strings = response.headers().get("X-Consul-Lastcontact"); long lastContact = Long.valueOf(strings.get(0)); strings = response.headers().get("X-Consul-Knownleader"); boolean knownLeader = Boolean.valueOf(strings.get(0)); //noinspection UnnecessaryLocalVariable @SuppressWarnings("UnnecessaryLocalVariable") ConsulResponse<T> consulResponse = new ConsulResponse<>(responseObject, lastContact, knownLeader, index); return consulResponse; }
private <T> Map<String, String> createMethodToChannelMap(final String channelPrefix, final Class<T> eventBusProxyInterface) { final Map<String, String> methodToChannelMap = new ConcurrentHashMap<>(20); final ClassMeta<T> classMeta = classMeta(eventBusProxyInterface); final AnnotationData classAnnotation = classMeta.annotation(AnnotationUtils.EVENT_CHANNEL_ANNOTATION_NAME); final String classEventBusName = AnnotationUtils.getClassEventChannelName(classMeta, classAnnotation); classMeta.methods().forEach(methodAccess -> { AnnotationData methodAnnotation = methodAccess.annotation(AnnotationUtils.EVENT_CHANNEL_ANNOTATION_NAME); String methodEventBusName = methodAnnotation != null && methodAnnotation.getValues().get("value") != null ? methodAnnotation.getValues().get("value").toString() : null; if (Str.isEmpty(methodEventBusName)) { methodEventBusName = methodAccess.name(); } final String channelName = createChannelName(channelPrefix, classEventBusName, methodEventBusName); methodToChannelMap.put(methodAccess.method().toString(), channelName); }); return methodToChannelMap; }
public static <T> ConsulResponse<List<T>> consulResponseList(final Class<T> responseType, final HTTP.Response response) { List<T> responseObject = null; if (response.code() == 200) { if (!Str.isEmpty(response.body())) { responseObject = fromJsonArray(response.body(), responseType); } } else { die("Unable to read response", response.code(), response.body()); } List<String> strings = response.headers().get("X-Consul-Index"); int index = Integer.valueOf(strings.get(0)); strings = response.headers().get("X-Consul-Lastcontact"); long lastContact = Long.valueOf(strings.get(0)); strings = response.headers().get("X-Consul-Knownleader"); boolean knownLeader = Boolean.valueOf(strings.get(0)); //noinspection UnnecessaryLocalVariable @SuppressWarnings("UnnecessaryLocalVariable") ConsulResponse<List<T>> consulResponse = new ConsulResponse<>(responseObject, lastContact, knownLeader, index); return consulResponse; }
private Response<Object> parseResponseFromChars(char[] args, final String returnAddress) { final char[][] chars = CharScanner.splitFromStartWithLimit(args, (char) PROTOCOL_SEPARATOR, 0, RESPONSE_RETURN); String messageId = FastStringUtils.noCopyStringFromChars(chars[MESSAGE_ID_POS]); long id = 0L; if (!Str.isEmpty(messageId)) { id = Long.parseLong(messageId); } String address = FastStringUtils.noCopyStringFromChars(chars[ADDRESS_POS]); String stime = FastStringUtils.noCopyStringFromChars(chars[TIMESTAMP_POS]); long timestamp = 0L; if (!Str.isEmpty(stime)) { timestamp = Long.parseLong(stime); } char[] wasErrorsStr = chars[WAS_ERRORS_POS]; boolean wasErrors = wasErrorsStr != null && wasErrorsStr.length == 1 && wasErrorsStr[0] == '1'; char[] messageBodyChars = chars[RESPONSE_RETURN]; Object messageBody; if (!Chr.isEmpty(messageBodyChars)) { messageBody = jsonParser.parse(messageBodyChars); } else { messageBody = null; } return new ResponseImpl<>(id, timestamp, address, returnAddress, null, messageBody, null, wasErrors); }
public MultiMap<String, String> parseHeaders(String header) { if (Str.isEmpty(header)) { return null; } MultiMap<String, String> params = new MultiMapImpl<>(); final char[][] split = CharScanner.split(FastStringUtils.toCharArray(header), (char) Protocol.PROTOCOL_ENTRY_HEADER_DELIM); for (char[] entry : split) { final char[][] kvSplit = CharScanner.split(entry, (char) PROTOCOL_KEY_HEADER_DELIM); if (kvSplit.length > 1) { char[] ckey = kvSplit[0]; char[] valuesAsOne = kvSplit[1]; final char[][] values = CharScanner.split(valuesAsOne, (char) PROTOCOL_VALUE_HEADER_DELIM); String key = new String(ckey); for (char[] value : values) { params.add(key, new String(value)); } } } return params; }
/** * Checks in with Consul. * * @param checkId The Check ID to check in. * @param status The current state of the Check. * @param note Any note to associate with the Check. */ public void check(String checkId, Status status, String note) { final URI uri = createURI("/check/" + status.getUri() + "/" + checkId); final HTTP.Response httpResponse = Str.isEmpty(note) ? HTTP.getResponse(uri.toString()) : HTTP.getResponse(uri.toString() + "?note=" + note); if (httpResponse.code() != 200) { notRegistered("Unable to perform check", uri, httpResponse.code(), httpResponse.statusMessageAsString(), httpResponse.body()); } }
public static <T> ConsulResponse<T> consulResponse(final Class<T> responseType, final HttpTextResponse response) { T responseObject = null; if (response.code() == 200) { if (!Str.isEmpty(response.body())) { responseObject = fromJson(response.body(), responseType); } } else { die("Unable to read response", response.code(), response.body()); } int index = Integer.valueOf(response.headers().getFirst("X-Consul-Index")); long lastContact = Long.valueOf(response.headers().getFirst("X-Consul-Lastcontact")); boolean knownLeader = Boolean.valueOf(response.headers().getFirst("X-Consul-Knownleader")); //noinspection UnnecessaryLocalVariable @SuppressWarnings("UnnecessaryLocalVariable") ConsulResponse<T> consulResponse = new ConsulResponse<>(responseObject, lastContact, knownLeader, index); return consulResponse; }
public static <T> ConsulResponse<List<T>> consulResponseList(final Class<T> responseType, final HttpTextResponse response) { List<T> responseObject = null; if (response.code() == 200) { if (!Str.isEmpty(response.body())) { responseObject = fromJsonArray(response.body(), responseType); } } else { die("Unable to read response", response.code(), response.body()); } int index = Integer.valueOf(response.headers().getFirst("X-Consul-Index")); long lastContact = Long.valueOf(response.headers().getFirst("X-Consul-Lastcontact")); boolean knownLeader = Boolean.valueOf(response.headers().getFirst("X-Consul-Knownleader")); //noinspection UnnecessaryLocalVariable @SuppressWarnings("UnnecessaryLocalVariable") ConsulResponse<List<T>> consulResponse = new ConsulResponse<>(responseObject, lastContact, knownLeader, index); return consulResponse; }