Ejemplo n.º 1
0
  /** @param bioID */
  private boolean setBioID(String bioID) {
    boolean toRet = false;
    try {
      JsonFactory factory = new JsonFactory();
      JsonParser jparser = factory.createParser(bioID);

      String key = "";
      // Get StartObject for JSON, after first {
      jparser.nextToken();
      while (jparser.nextToken() != JsonToken.END_OBJECT) {
        key = jparser.getText();

        if (key.equals("id")) {
          key = jparser.nextTextValue();

          // Set bioID in Experiment
          selExp.setBioID(key);

          toRet = true;
        } else {
          jparser.skipChildren();
        }
      }
      jparser.close();

    } catch (JsonParseException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    return toRet;
  }
 protected ConstExprConstruct parseAnnotationConstExprConstruct(final JsonParser jp)
     throws IOException {
   final ConstExprConstruct constExpr = new ConstExprConstruct();
   constExpr.setType(ConstExprConstruct.Type.fromString(jp.getCurrentName()));
   constExpr.setValue(jp.nextTextValue());
   return constExpr;
 }
Ejemplo n.º 3
0
    @Override
    public Event deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
      ObjectMapper mapper = (ObjectMapper) jp.getCodec();
      String eventName = jp.nextTextValue();

      EventKey ek = new EventKey(namespaceClass.get(), eventName);
      if (!eventMapping.containsKey(ek)) {
        ek = new EventKey(Namespace.DEFAULT_NAME, eventName);
        if (!eventMapping.containsKey(ek)) {
          return new Event(eventName, Collections.emptyList());
        }
      }

      List<Object> eventArgs = new ArrayList<Object>();
      Event event = new Event(eventName, eventArgs);
      List<Class<?>> eventClasses = eventMapping.get(ek);
      int i = 0;
      while (true) {
        JsonToken token = jp.nextToken();
        if (token == JsonToken.END_ARRAY) {
          break;
        }
        if (i > eventClasses.size() - 1) {
          log.debug("Event {} has more args than declared in handler: {}", eventName, null);
          break;
        }
        Class<?> eventClass = eventClasses.get(i);
        Object arg = mapper.readValue(jp, eventClass);
        eventArgs.add(arg);
        i++;
      }
      return event;
    }
 private List<String> readPersons(JsonParser parser) throws IOException {
   if (parser.nextToken() != JsonToken.START_ARRAY) {
     reportIllegal(parser, JsonToken.START_ARRAY);
   }
   List<String> persons = new ArrayList<String>();
   String str;
   while ((str = parser.nextTextValue()) != null) {
     persons.add(str);
   }
   verifyCurrent(parser, JsonToken.END_ARRAY);
   return persons;
 }
 protected ReturnType parseReturnType(final JsonParser jp, final String elementName)
     throws IOException {
   ReturnType returnType;
   if (elementName.equals(((FromXmlParser) jp).getStaxReader().getLocalName())) {
     returnType = new ReturnType();
     returnType.setType(jp.nextTextValue());
   } else {
     jp.nextToken();
     returnType = jp.getCodec().readValue(jp, ReturnType.class);
   }
   return returnType;
 }
  /** Offlined version used when we do not use the default deserialization method. */
  protected final String[] _deserializeCustom(
      JsonParser p, DeserializationContext ctxt, String[] old) throws IOException {
    final ObjectBuffer buffer = ctxt.leaseObjectBuffer();
    int ix;
    Object[] chunk;

    if (old == null) {
      ix = 0;
      chunk = buffer.resetAndStart();
    } else {
      ix = old.length;
      chunk = buffer.resetAndStart(old, ix);
    }

    final JsonDeserializer<String> deser = _elementDeserializer;

    try {
      while (true) {
        /* 30-Dec-2014, tatu: This may look odd, but let's actually call method
         *   that suggest we are expecting a String; this helps with some formats,
         *   notably XML. Note, however, that while we can get String, we can't
         *   assume that's what we use due to custom deserializer
         */
        String value;
        if (p.nextTextValue() == null) {
          JsonToken t = p.getCurrentToken();
          if (t == JsonToken.END_ARRAY) {
            break;
          }
          // Ok: no need to convert Strings, but must recognize nulls
          value =
              (t == JsonToken.VALUE_NULL) ? deser.getNullValue(ctxt) : deser.deserialize(p, ctxt);
        } else {
          value = deser.deserialize(p, ctxt);
        }
        if (ix >= chunk.length) {
          chunk = buffer.appendCompletedChunk(chunk);
          ix = 0;
        }
        chunk[ix++] = value;
      }
    } catch (Exception e) {
      // note: pass String.class, not String[].class, as we need element type for error info
      throw JsonMappingException.wrapWithPath(e, String.class, ix);
    }
    String[] result = buffer.completeAndClearBuffer(chunk, ix, String.class);
    ctxt.returnObjectBuffer(buffer);
    return result;
  }
  @Override
  public String[] deserialize(JsonParser p, DeserializationContext ctxt, String[] intoValue)
      throws IOException {
    // Ok: must point to START_ARRAY (or equivalent)
    if (!p.isExpectedStartArrayToken()) {
      String[] arr = handleNonArray(p, ctxt);
      if (arr == null) {
        return intoValue;
      }
      final int offset = intoValue.length;
      String[] result = new String[offset + arr.length];
      System.arraycopy(intoValue, 0, result, 0, offset);
      System.arraycopy(arr, 0, result, offset, arr.length);
      return result;
    }

    if (_elementDeserializer != null) {
      return _deserializeCustom(p, ctxt, intoValue);
    }
    final ObjectBuffer buffer = ctxt.leaseObjectBuffer();
    int ix = intoValue.length;
    Object[] chunk = buffer.resetAndStart(intoValue, ix);

    try {
      while (true) {
        String value = p.nextTextValue();
        if (value == null) {
          JsonToken t = p.getCurrentToken();
          if (t == JsonToken.END_ARRAY) {
            break;
          }
          if (t != JsonToken.VALUE_NULL) {
            value = _parseString(p, ctxt);
          }
        }
        if (ix >= chunk.length) {
          chunk = buffer.appendCompletedChunk(chunk);
          ix = 0;
        }
        chunk[ix++] = value;
      }
    } catch (Exception e) {
      throw JsonMappingException.wrapWithPath(e, chunk, buffer.bufferedSize() + ix);
    }
    String[] result = buffer.completeAndClearBuffer(chunk, ix, String.class);
    ctxt.returnObjectBuffer(buffer);
    return result;
  }
Ejemplo n.º 8
0
  @Override
  public List<Video> fetchSource(String embedPageUrl) throws OnErrorThrowable {

    String body = GeneralUtils.getWebPage(embedPageUrl);

    String elementHtml =
        Jsoup.parse(body).select("div.progress").first().nextElementSibling().html();

    String onlyJson =
        elementHtml.substring(
            elementHtml.indexOf("var vars = ") + 11, elementHtml.indexOf("var fixed_player_size"));

    List<Video> videos = new ArrayList<>(1);

    try {
      String regex = "url\\d+";
      Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);

      JsonParser jsonParser = new JsonFactory().createParser(onlyJson);

      while (!jsonParser.isClosed()) {
        JsonToken jsonToken = jsonParser.nextToken();
        String currentName = jsonParser.getCurrentName();

        if (jsonToken != null && currentName != null) {
          Matcher matcher = pattern.matcher(currentName);

          if (matcher.find()) {
            String nextValue = jsonParser.nextTextValue();

            if (nextValue != null) {
              videos.add(new Video(currentName.substring(3) + "p", nextValue));
            }
          }
        }
      }

    } catch (IOException io) {
      throw OnErrorThrowable.from(new Throwable("Vk video retrieval failed.", io));
    }

    return videos;
  }
  @Override
  protected ReferentialConstraintRole doDeserialize(
      final JsonParser jp, final DeserializationContext ctxt)
      throws IOException, JsonProcessingException {

    final ReferentialConstraintRole refConstRole = new ReferentialConstraintRole();

    for (; jp.getCurrentToken() != JsonToken.END_OBJECT; jp.nextToken()) {
      final JsonToken token = jp.getCurrentToken();
      if (token == JsonToken.FIELD_NAME) {
        if ("Role".equals(jp.getCurrentName())) {
          refConstRole.setRole(jp.nextTextValue());
        } else if ("PropertyRef".equals(jp.getCurrentName())) {
          jp.nextToken();
          refConstRole.getPropertyRefs().add(jp.getCodec().readValue(jp, PropertyRef.class));
        }
      }
    }

    return refConstRole;
  }
  private Image readImage(JsonParser parser) throws IOException {
    boolean haveWidth = false;
    boolean haveHeight = false;
    Image image = new Image();
    if (parser.nextFieldName(FIELD_URI)) {
      image.uri = parser.nextTextValue();
      if (parser.nextFieldName(FIELD_TITLE)) {
        image.title = parser.nextTextValue();
        if (parser.nextFieldName(FIELD_WIDTH)) {
          image.width = parser.nextIntValue(-1);
          haveWidth = true;
          if (parser.nextFieldName(FIELD_HEIGHT)) {
            image.height = parser.nextIntValue(-1);
            haveHeight = true;
            if (parser.nextFieldName(FIELD_SIZE)) {
              image.size = Image.Size.valueOf(parser.nextTextValue());
              parser.nextToken();
              verifyCurrent(parser, JsonToken.END_OBJECT);
              return image;
            }
          }
        }
      }
    }

    for (; parser.getCurrentToken() == JsonToken.FIELD_NAME; parser.nextToken()) {
      String field = parser.getCurrentName();
      // read value token (or START_ARRAY)
      parser.nextToken();
      Integer I = fullFieldToIndex.get(field);
      if (I != null) {
        switch (I) {
          case FIELD_IX_URI:
            image.uri = parser.getText();
            continue;
          case FIELD_IX_TITLE:
            image.title = parser.getText();
            continue;
          case FIELD_IX_WIDTH:
            image.width = parser.getIntValue();
            haveWidth = true;
            continue;
          case FIELD_IX_HEIGHT:
            image.height = parser.getIntValue();
            haveHeight = true;
            continue;
          case FIELD_IX_SIZE:
            image.size = Image.Size.valueOf(parser.getText());
            continue;
        }
      }
      throw new IllegalStateException("Unexpected field '" + field + "'");
    }

    if (image.uri == null) throw new IllegalStateException("Missing field: " + FIELD_URI);
    if (!haveWidth) throw new IllegalStateException("Missing field: " + FIELD_WIDTH);
    if (!haveHeight) throw new IllegalStateException("Missing field: " + FIELD_HEIGHT);
    if (image.size == null) throw new IllegalStateException("Missing field: " + FIELD_SIZE);

    verifyCurrent(parser, JsonToken.END_OBJECT);

    return image;
  }
  private Media readMedia(JsonParser parser) throws IOException {
    if (parser.nextToken() != JsonToken.START_OBJECT) {
      reportIllegal(parser, JsonToken.START_OBJECT);
    }
    Media media = new Media();
    boolean haveWidth = false;
    boolean haveHeight = false;
    boolean haveDuration = false;
    boolean haveSize = false;

    // As with above, first fast path
    if (parser.nextFieldName(FIELD_PLAYER)) {
      media.player = Media.Player.find(parser.nextTextValue());
      if (parser.nextFieldName(FIELD_URI)) {
        media.uri = parser.nextTextValue();
        if (parser.nextFieldName(FIELD_TITLE)) {
          media.title = parser.nextTextValue();
          if (parser.nextFieldName(FIELD_WIDTH)) {
            haveWidth = true;
            media.width = parser.nextIntValue(-1);
            if (parser.nextFieldName(FIELD_HEIGHT)) {
              haveHeight = true;
              media.height = parser.nextIntValue(-1);
              if (parser.nextFieldName(FIELD_FORMAT)) {
                media.format = parser.nextTextValue();
                if (parser.nextFieldName(FIELD_DURATION)) {
                  haveDuration = true;
                  media.duration = parser.nextLongValue(-1L);
                  if (parser.nextFieldName(FIELD_SIZE)) {
                    haveSize = true;
                    media.size = parser.nextLongValue(-1L);
                    if (parser.nextFieldName(FIELD_BITRATE)) {
                      media.bitrate = parser.nextIntValue(-1);
                      media.hasBitrate = true;
                      if (parser.nextFieldName(FIELD_COPYRIGHT)) {
                        media.copyright = parser.nextTextValue();
                        if (parser.nextFieldName(FIELD_PERSONS)) {
                          media.persons = readPersons(parser);
                          parser.nextToken();
                          verifyCurrent(parser, JsonToken.END_OBJECT);
                          return media;
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }

    // and if something reorder or missing, general loop:

    for (; parser.getCurrentToken() == JsonToken.FIELD_NAME; parser.nextToken()) {
      String field = parser.getCurrentName();
      Integer I = fullFieldToIndex.get(field);
      if (I != null) {
        switch (I) {
          case FIELD_IX_PLAYER:
            media.player = Media.Player.find(parser.nextTextValue());
            continue;
          case FIELD_IX_URI:
            media.uri = parser.nextTextValue();
            continue;
          case FIELD_IX_TITLE:
            media.title = parser.nextTextValue();
            continue;
          case FIELD_IX_WIDTH:
            media.width = parser.nextIntValue(-1);
            haveWidth = true;
            continue;
          case FIELD_IX_HEIGHT:
            media.height = parser.nextIntValue(-1);
            haveHeight = true;
            continue;
          case FIELD_IX_FORMAT:
            media.format = parser.nextTextValue();
            continue;
          case FIELD_IX_DURATION:
            media.duration = parser.nextLongValue(-1L);
            haveDuration = true;
            continue;
          case FIELD_IX_SIZE:
            media.size = parser.nextLongValue(-1L);
            haveSize = true;
            continue;
          case FIELD_IX_BITRATE:
            media.bitrate = parser.nextIntValue(-1);
            media.hasBitrate = true;
            continue;
          case FIELD_IX_PERSONS:
            media.persons = readPersons(parser);
            continue;
          case FIELD_IX_COPYRIGHT:
            media.copyright = parser.nextTextValue();
            continue;
        }
      }
      throw new IllegalStateException("Unexpected field '" + field + "'");
    }
    verifyCurrent(parser, JsonToken.END_OBJECT);

    if (media.uri == null) throw new IllegalStateException("Missing field: " + FIELD_URI);
    if (!haveWidth) throw new IllegalStateException("Missing field: " + FIELD_WIDTH);
    if (!haveHeight) throw new IllegalStateException("Missing field: " + FIELD_HEIGHT);
    if (media.format == null) throw new IllegalStateException("Missing field: " + FIELD_FORMAT);
    if (!haveDuration) throw new IllegalStateException("Missing field: " + FIELD_DURATION);
    if (!haveSize) throw new IllegalStateException("Missing field: " + FIELD_SIZE);
    if (media.persons == null) media.persons = new ArrayList<String>();
    if (media.player == null) throw new IllegalStateException("Missing field: " + FIELD_PLAYER);

    return media;
  }
  @Override
  protected AbstractEntityType doDeserialize(final JsonParser jp, final DeserializationContext ctxt)
      throws IOException, JsonProcessingException {

    final AbstractEntityType entityType =
        ODataServiceVersion.V30 == version
            ? new org.apache.olingo.client.core.edm.xml.v3.EntityTypeImpl()
            : new org.apache.olingo.client.core.edm.xml.v4.EntityTypeImpl();

    for (; jp.getCurrentToken() != JsonToken.END_OBJECT; jp.nextToken()) {
      final JsonToken token = jp.getCurrentToken();
      if (token == JsonToken.FIELD_NAME) {
        if ("Name".equals(jp.getCurrentName())) {
          entityType.setName(jp.nextTextValue());
        } else if ("Abstract".equals(jp.getCurrentName())) {
          entityType.setAbstractEntityType(BooleanUtils.toBoolean(jp.nextTextValue()));
        } else if ("BaseType".equals(jp.getCurrentName())) {
          entityType.setBaseType(jp.nextTextValue());
        } else if ("OpenType".equals(jp.getCurrentName())) {
          entityType.setOpenType(BooleanUtils.toBoolean(jp.nextTextValue()));
        } else if ("HasStream".equals(jp.getCurrentName())) {
          entityType.setHasStream(BooleanUtils.toBoolean(jp.nextTextValue()));
        } else if ("Key".equals(jp.getCurrentName())) {
          jp.nextToken();
          entityType.setKey(jp.readValueAs(EntityKeyImpl.class));
        } else if ("Property".equals(jp.getCurrentName())) {
          jp.nextToken();
          if (entityType instanceof org.apache.olingo.client.core.edm.xml.v3.EntityTypeImpl) {
            ((org.apache.olingo.client.core.edm.xml.v3.EntityTypeImpl) entityType)
                .getProperties()
                .add(jp.readValueAs(org.apache.olingo.client.core.edm.xml.v3.PropertyImpl.class));
          } else {
            ((org.apache.olingo.client.core.edm.xml.v4.EntityTypeImpl) entityType)
                .getProperties()
                .add(jp.readValueAs(org.apache.olingo.client.core.edm.xml.v4.PropertyImpl.class));
          }
        } else if ("NavigationProperty".equals(jp.getCurrentName())) {
          jp.nextToken();
          if (entityType instanceof org.apache.olingo.client.core.edm.xml.v3.EntityTypeImpl) {
            ((org.apache.olingo.client.core.edm.xml.v3.EntityTypeImpl) entityType)
                .getNavigationProperties()
                .add(
                    jp.readValueAs(
                        org.apache.olingo.client.core.edm.xml.v3.NavigationPropertyImpl.class));
          } else {
            ((org.apache.olingo.client.core.edm.xml.v4.EntityTypeImpl) entityType)
                .getNavigationProperties()
                .add(
                    jp.readValueAs(
                        org.apache.olingo.client.core.edm.xml.v4.NavigationPropertyImpl.class));
          }
        } else if ("Annotation".equals(jp.getCurrentName())) {
          jp.nextToken();
          ((org.apache.olingo.client.core.edm.xml.v4.EntityTypeImpl) entityType)
              .setAnnotation(jp.readValueAs(AnnotationImpl.class));
        }
      }
    }

    return entityType;
  }
Ejemplo n.º 13
0
  /**
   * Query Watchman for file change events. If too many events are pending or an error occurs an
   * overflow event is posted to the EventBus signalling that events may have been lost (and so
   * typically caches must be cleared to avoid inconsistency). Interruptions and IOExceptions are
   * propagated to callers, but typically if overflow events are handled conservatively by
   * subscribers then no other remedial action is required.
   */
  @Override
  public void postEvents(BuckEventBus buckEventBus) throws IOException, InterruptedException {
    ProcessExecutor.LaunchedProcess watchmanProcess =
        processExecutor.launchProcess(
            ProcessExecutorParams.builder()
                .addCommand("watchman", "--server-encoding=json", "--no-pretty", "-j")
                .build());
    try {
      LOG.debug("Writing query to Watchman: %s", query);
      watchmanProcess.getOutputStream().write(query.getBytes(Charsets.US_ASCII));
      watchmanProcess.getOutputStream().close();
      LOG.debug("Parsing JSON output from Watchman");
      final long parseStartTimeMillis = clock.currentTimeMillis();
      InputStream jsonInput = watchmanProcess.getInputStream();
      if (LOG.isVerboseEnabled()) {
        byte[] fullResponse = ByteStreams.toByteArray(jsonInput);
        jsonInput.close();
        jsonInput = new ByteArrayInputStream(fullResponse);
        LOG.verbose("Full JSON: " + new String(fullResponse, Charsets.UTF_8).trim());
      }
      JsonParser jsonParser = objectMapper.getJsonFactory().createJsonParser(jsonInput);
      PathEventBuilder builder = new PathEventBuilder();
      JsonToken token = jsonParser.nextToken();
      /*
       * Watchman returns changes as an array of JSON objects with potentially unstable key
       * ordering:
       * {
       *     "files": [
       *     {
       *         "new": false,
       *         "exists": true,
       *         "name": "bin/buckd",
       *     },
       *     ]
       * }
       * A simple way to parse these changes is to collect the relevant values from each object
       * in a builder and then build an event when the end of a JSON object is reached. When the end
       * of the enclosing JSON object is processed the builder will not contain a complete event, so
       * the object end token will be ignored.
       */
      int eventCount = 0;
      while (token != null) {
        boolean shouldOverflow = false;
        if (eventCount > overflow) {
          LOG.warn(
              "Received too many events from Watchmen (%d > overflow max %d), posting overflow "
                  + "event and giving up.",
              eventCount, overflow);
          shouldOverflow = true;
        } else {
          long elapsedMillis = clock.currentTimeMillis() - parseStartTimeMillis;
          if (elapsedMillis >= timeoutMillis) {
            LOG.warn(
                "Parsing took too long (timeout %d ms), posting overflow event and giving up.",
                timeoutMillis);
            shouldOverflow = true;
          }
        }

        if (shouldOverflow) {
          postWatchEvent(createOverflowEvent());
          processExecutor.destroyLaunchedProcess(watchmanProcess);
          processExecutor.waitForLaunchedProcess(watchmanProcess);
          return;
        }

        switch (token) {
          case FIELD_NAME:
            String fieldName = jsonParser.getCurrentName();
            switch (fieldName) {
              case "is_fresh_instance":
                // Force caches to be invalidated --- we have no idea what's happening.
                Boolean newInstance = jsonParser.nextBooleanValue();
                if (newInstance) {
                  LOG.info(
                      "Fresh watchman instance detected. "
                          + "Posting overflow event to flush caches.");
                  postWatchEvent(createOverflowEvent());
                }
                break;

              case "name":
                builder.setPath(Paths.get(jsonParser.nextTextValue()));
                break;
              case "new":
                if (jsonParser.nextBooleanValue()) {
                  builder.setCreationEvent();
                }
                break;
              case "exists":
                if (!jsonParser.nextBooleanValue()) {
                  builder.setDeletionEvent();
                }
                break;
              case "error":
                WatchmanWatcherException e =
                    new WatchmanWatcherException(jsonParser.nextTextValue());
                LOG.error(
                    e, "Error in Watchman output. Posting an overflow event to flush the caches");
                postWatchEvent(createOverflowEvent());
                throw e;
              case "warning":
                String message = jsonParser.nextTextValue();
                buckEventBus.post(
                    ConsoleEvent.warning("Watchman has produced a warning: %s", message));
                LOG.warn("Watchman has produced a warning: %s", message);
                break;
            }
            break;
          case END_OBJECT:
            if (builder.canBuild()) {
              postWatchEvent(builder.build());
              ++eventCount;
            }
            builder = new PathEventBuilder();
            break;
            // $CASES-OMITTED$
          default:
            break;
        }
        token = jsonParser.nextToken();
      }
      int watchmanExitCode;
      LOG.debug("Posted %d Watchman events. Waiting for subprocess to exit...", eventCount);
      watchmanExitCode = processExecutor.waitForLaunchedProcess(watchmanProcess);
      if (watchmanExitCode != 0) {
        LOG.error("Watchman exited with error code %d", watchmanExitCode);
        postWatchEvent(createOverflowEvent()); // Events may have been lost, signal overflow.
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        ByteStreams.copy(watchmanProcess.getErrorStream(), buffer);
        throw new WatchmanWatcherException(
            "Watchman failed with exit code " + watchmanExitCode + ": " + buffer.toString());
      } else {
        LOG.debug("Watchman exited cleanly.");
      }
    } catch (InterruptedException e) {
      LOG.warn(e, "Killing Watchman process on interrupted exception");
      postWatchEvent(createOverflowEvent()); // Events may have been lost, signal overflow.
      processExecutor.destroyLaunchedProcess(watchmanProcess);
      processExecutor.waitForLaunchedProcess(watchmanProcess);
      Thread.currentThread().interrupt();
      throw e;
    } catch (IOException e) {
      LOG.error(e, "Killing Watchman process on I/O exception");
      postWatchEvent(createOverflowEvent()); // Events may have been lost, signal overflow.
      processExecutor.destroyLaunchedProcess(watchmanProcess);
      processExecutor.waitForLaunchedProcess(watchmanProcess);
      throw e;
    }
  }