public synchronized STATE onBodyPartReceived()

in src/main/java/org/apache/jenkins/gitpubsub/GitPubSubPoll.java [217:293]


        public synchronized STATE onBodyPartReceived(HttpResponseBodyPart content) throws Exception {
            lastTime = System.currentTimeMillis();
            byte[] body = content.getBodyPartBytes();
            if (body.length < 2 || body[body.length - 1] != 0x0a) {
                LOGGER.log(Level.FINE, "Stashing large message");
                if (partialBody == null) {
                    partialBody = body;
                } else {
                    int index = partialBody.length;
                    partialBody = Arrays.copyOf(partialBody, index + body.length);
                    System.arraycopy(body, 0, partialBody, index, body.length);
                }
                return STATE.CONTINUE;
            }
            if (partialBody != null) {
                LOGGER.log(Level.FINE, "Unstashing large message");
                int index = partialBody.length;
                partialBody = Arrays.copyOf(partialBody, index + body.length);
                System.arraycopy(body, 0, partialBody, index, body.length);
                body = partialBody;
                partialBody = null;
            }
            JsonNode json;
            try {
                json = mapper.readTree(body);
            } catch (JsonParseException e) {
                LOGGER.log(Level.INFO,
                        "Could not parse GitPubSub event: " + new String(body, StandardCharsets.UTF_8),
                        e
                );
                return STATE.CONTINUE;
            }
            if (LOGGER.isLoggable(Level.FINE)) {
                LOGGER.log(Level.FINE, "GitPubSub event {0}",
                        mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json));
            }
            allEvents.incrementAndGet();
            for (Iterator<Map.Entry<String, JsonNode>> it = json.fields(); it.hasNext(); ) {
                Map.Entry<String, JsonNode> field = it.next();
                String fieldName = field.getKey();
                final JsonNode fieldValue = field.getValue();
                try {
                    if ("stillalive".equals(fieldName)) {
                        aliveEvents.incrementAndGet();
                        lastTS = fieldValue.asLong();
                    } else if ("push".equals(fieldName)) {
                        pushEvents.incrementAndGet();
                        if ("git".equals(fieldValue.get("repository").textValue())
                                && fieldValue.has("project")
                                && !"tag".equals(fieldValue.get("type").textValue())
                                && fieldValue.get("ref").asText().startsWith(Constants.R_HEADS)) {
                            SCMEvent.Type type;
                            String typeStr = fieldValue.get("action").textValue();
                            if ("created".equals(typeStr)) {
                                type = CREATED;
                            } else if ("updated".equals(typeStr)) {
                                type = UPDATED;
                            } else if ("deleted".equals(typeStr)) {
                                type = REMOVED;
                            } else {
                                // unknown, so ignore
                                continue;
                            }
                            SCMHeadEvent.fireNow(new Push(type, fieldValue, GITPUBSUB_URL));
                        }
                    }
                } catch (Exception e) {
                    LOGGER.log(Level.WARNING, "Uncaught exception", e);
                }
            }
            if (requestRecycleMins == -1 || recycleAt - System.nanoTime() > 0) {
                return STATE.CONTINUE;
            } else {
                LOGGER.log(Level.FINE, "Recycling...");
                return STATE.ABORT;
            }
        }