コード例 #1
0
ファイル: SystemUtils.java プロジェクト: fin09pcap/Raigad
  public static String runHttpPostCommand(String url, String jsonBody) throws IOException {
    String return_;
    DefaultHttpClient client = new DefaultHttpClient();
    InputStream isStream = null;
    try {
      HttpParams httpParameters = new BasicHttpParams();
      int timeoutConnection = 1000;
      int timeoutSocket = 1000;
      HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
      HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
      client.setParams(httpParameters);

      HttpPost postRequest = new HttpPost(url);
      if (StringUtils.isNotEmpty(jsonBody))
        postRequest.setEntity(new StringEntity(jsonBody, StandardCharsets.UTF_8));
      postRequest.setHeader("Content-type", "application/json");

      HttpResponse resp = client.execute(postRequest);

      if (resp == null || resp.getEntity() == null) {
        throw new ESHttpException(
            "Unable to execute POST URL ("
                + url
                + ") Exception Message: < Null Response or Null HttpEntity >");
      }

      isStream = resp.getEntity().getContent();

      if (resp.getStatusLine().getStatusCode() != 200) {

        throw new ESHttpException(
            "Unable to execute POST URL ("
                + url
                + ") Exception Message: ("
                + IOUtils.toString(isStream, StandardCharsets.UTF_8.toString())
                + ")");
      }

      return_ = IOUtils.toString(isStream, StandardCharsets.UTF_8.toString());
      logger.debug("POST URL API: {} with JSONBody {} returns: {}", url, jsonBody, return_);
    } catch (Exception e) {
      throw new ESHttpException(
          "Caught an exception during execution of URL ("
              + url
              + ")Exception Message: ("
              + e
              + ")");
    } finally {
      if (isStream != null) isStream.close();
    }
    return return_;
  }
コード例 #2
0
 protected MultivaluedMap<String, String> toRequestState(ContainerRequestContext rc, UriInfo ui) {
   MultivaluedMap<String, String> requestState = new MetadataMap<String, String>();
   requestState.putAll(ui.getQueryParameters(decodeRequestParameters));
   if (MediaType.APPLICATION_FORM_URLENCODED_TYPE.isCompatible(rc.getMediaType())) {
     String body = FormUtils.readBody(rc.getEntityStream(), StandardCharsets.UTF_8.name());
     FormUtils.populateMapFromString(
         requestState,
         JAXRSUtils.getCurrentMessage(),
         body,
         StandardCharsets.UTF_8.name(),
         decodeRequestParameters);
   }
   return requestState;
 }
コード例 #3
0
 /**
  * same as {@link #getWordSet(ResourceLoader, String, boolean)}, except the input is in snowball
  * format.
  */
 protected final CharArraySet getSnowballWordSet(
     ResourceLoader loader, String wordFiles, boolean ignoreCase) throws IOException {
   List<String> files = splitFileNames(wordFiles);
   CharArraySet words = null;
   if (files.size() > 0) {
     // default stopwords list has 35 or so words, but maybe don't make it that
     // big to start
     words = new CharArraySet(files.size() * 10, ignoreCase);
     for (String file : files) {
       InputStream stream = null;
       Reader reader = null;
       try {
         stream = loader.openResource(file.trim());
         CharsetDecoder decoder =
             StandardCharsets.UTF_8
                 .newDecoder()
                 .onMalformedInput(CodingErrorAction.REPORT)
                 .onUnmappableCharacter(CodingErrorAction.REPORT);
         reader = new InputStreamReader(stream, decoder);
         WordlistLoader.getSnowballWordSet(reader, words);
       } finally {
         IOUtils.closeWhileHandlingException(reader, stream);
       }
     }
   }
   return words;
 }
コード例 #4
0
  /** Make sure we won't pass invalid characters to XML serializer. */
  public void testWriteReadNoCrash() throws Exception {
    ByteArrayOutputStream os = new ByteArrayOutputStream();

    XmlSerializer serializer = Xml.newSerializer();
    serializer.setOutput(os, StandardCharsets.UTF_8.name());
    serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
    serializer.startDocument(null, true);

    for (int ch = 0; ch < 0x10000; ch++) {
      checkWriteSingleSetting(
          "char=0x" + Integer.toString(ch, 16), serializer, "key", String.valueOf((char) ch));
    }
    checkWriteSingleSetting(serializer, "k", "");
    checkWriteSingleSetting(serializer, "x", "abc");
    checkWriteSingleSetting(serializer, "abc", CRAZY_STRING);
    checkWriteSingleSetting(serializer, "def", null);

    // Invlid input, but shouoldn't crash.
    checkWriteSingleSetting(serializer, null, null);
    checkWriteSingleSetting(serializer, CRAZY_STRING, null);
    SettingsState.writeSingleSetting(
        SettingsState.SETTINGS_VERSOIN_NEW_ENCODING, serializer, null, "k", "v", "package");
    SettingsState.writeSingleSetting(
        SettingsState.SETTINGS_VERSOIN_NEW_ENCODING, serializer, "1", "k", "v", null);
  }
コード例 #5
0
 /**
  * the usage of getResponseByParams is disencouraged for the embedded Solr connector. Please use
  * request(SolrParams) instead. Reason: Solr makes a very complex folding/unfolding including data
  * compression for SolrQueryResponses.
  */
 @Override
 public QueryResponse getResponseByParams(ModifiableSolrParams params) throws IOException {
   if (this.server == null) throw new IOException("server disconnected");
   // during the solr query we set the thread name to the query string to get more debugging info
   // in thread dumps
   String threadname = Thread.currentThread().getName();
   String ql = "";
   try {
     ql = URLDecoder.decode(params.toString(), StandardCharsets.UTF_8.name());
   } catch (UnsupportedEncodingException e) {
   }
   Thread.currentThread().setName("solr query: q=" + ql);
   ConcurrentLog.info("EmbeddedSolrConnector.getResponseByParams", "QUERY: " + ql);
   // System.out.println("EmbeddedSolrConnector.getResponseByParams * QUERY: " + ql);
   // System.out.println("STACKTRACE: " + ConcurrentLog.stackTrace());
   QueryResponse rsp;
   try {
     rsp = this.server.query(params);
     Thread.currentThread().setName(threadname);
     if (rsp != null)
       if (log.isFine()) log.fine(rsp.getResults().getNumFound() + " results for " + ql);
     return rsp;
   } catch (final SolrServerException e) {
     throw new IOException(e);
   } catch (final Throwable e) {
     throw new IOException("Error executing query", e);
   }
 }
コード例 #6
0
  @Test
  public void testDualExpMismatchError() throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    helper.redirectLog(out, LogLevel.ERROR);
    JsonInputField isbn = new JsonInputField("isbn");
    isbn.setPath("$..book[?(@.isbn)].isbn");
    isbn.setType(ValueMetaInterface.TYPE_STRING);
    JsonInputField price = new JsonInputField("price");
    price.setPath("$..book[*].price");
    price.setType(ValueMetaInterface.TYPE_NUMBER);

    try (LocaleChange enUS = new LocaleChange(Locale.US)) {
      JsonInputMeta meta = createSimpleMeta("json", isbn, price);
      JsonInput jsonInput = createJsonInput("json", meta, new Object[] {getBasicTestJson()});

      processRows(jsonInput, 3);

      Assert.assertEquals("error", 1, jsonInput.getErrors());
      Assert.assertEquals("rows written", 0, jsonInput.getLinesWritten());
      String errors =
          IOUtils.toString(
              new ByteArrayInputStream(out.toByteArray()), StandardCharsets.UTF_8.name());
      String expectedError =
          "The data structure is not the same inside the resource!"
              + " We found 4 values for json path [$..book[*].price],"
              + " which is different that the number returned for path [$..book[?(@.isbn)].isbn] (2 values)."
              + " We MUST have the same number of values for all paths.";
      Assert.assertTrue("expected error", errors.contains(expectedError));
    }
  }
コード例 #7
0
 private String urlEncode(final String input) {
   try {
     return URLEncoder.encode(input, StandardCharsets.UTF_8.toString());
   } catch (final UnsupportedEncodingException e) {
     throw new RuntimeException("Unable to encode search term, UTF-8 is unsupported", e);
   }
 }
コード例 #8
0
 @Override
 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
     throws ServletException, IOException {
   resp.setCharacterEncoding(StandardCharsets.UTF_8.toString());
   resp.setContentType(PLAIN_TEXT_UTF_8);
   Resources.asCharSource(Resources.getResource("assets/banner.txt"), StandardCharsets.UTF_8)
       .copyTo(resp.getWriter());
 }
コード例 #9
0
ファイル: FileIO.java プロジェクト: Ohohcakester/NDProofs
 public static String readFile(File file) {
   try {
     byte[] encoded = Files.readAllBytes(Paths.get(file.getCanonicalPath()));
     return StandardCharsets.UTF_8.decode(ByteBuffer.wrap(encoded)).toString();
   } catch (IOException e) {
     return null;
   }
 }
  /**
   * function to get the string of the last "*.json"-file within the modulePathOnUSB
   *
   * @return string with the contents of the last "*.json"-file within the modulePathOnUSB (in
   *     UTF-8)
   * @throws Exception FileNotFoundException will occur if the given folder does not exist Exception
   *     will occur if there was no file with the given extension found in the given folder
   */
  public String getJsonFileAsString() throws Exception {

    try {
      byte[] encoded = fileToByteArray(modulePathOnUSB + "ModuleData/", "json");
      return StandardCharsets.UTF_8.decode(ByteBuffer.wrap(encoded)).toString();
    } catch (Exception e) {
      throw e;
    }
  }
コード例 #11
0
  @Test
  public void testParse() throws Exception {
    String inputHtml =
        new Scanner(
                this.getClass().getResourceAsStream("Motel6_1.json"),
                StandardCharsets.UTF_8.toString())
            .useDelimiter("\\Z")
            .next();

    ArrayList<RoomInfo> roomInfos =
        parser.parse(
            inputHtml,
            new RoomPhotoDownloader() {
              @Override
              public Path download(String url) {
                return NetworkUtils.srcImageToSavePath(url, Paths.get(""));
              }
            });

    Iterator<RoomInfo> iter = roomInfos.iterator();

    // 1.
    assertTrue(iter.hasNext());
    {
      RoomInfo roomInfo = iter.next();
      // assertEquals("m6_0000_single1.jpg", roomInfo.roomPhotoPath);
      assertEquals("43.99USD", roomInfo.rate);
      assertEquals(
          "32\" LCD TV WITH AV CONNECTIONS FOR GAMING, WOOD-EFFECT FLOORS, BATHROOM WITH GRANITE COUNTERTOPS AND A RAISED WASH BASIN.",
          roomInfo.description);
      assertEquals("1 QUEEN BED |", roomInfo.amenities);
    }

    // 2.
    assertTrue(iter.hasNext());
    {
      RoomInfo roomInfo = iter.next();
      // assertEquals("m6_0000_double2.jpg", roomInfo.roomPhotoPath);
      assertEquals("43.99USD", roomInfo.rate);
      assertEquals(
          "32\" LCD TV WITH AV CONNECTIONS FOR GAMING, WOOD-EFFECT FLOORS, BATHROOM WITH GRANITE COUNTERTOPS AND A RAISED WASH BASIN.",
          roomInfo.description);
      assertEquals("2 FULL BEDS |", roomInfo.amenities);
    }

    // 3.
    assertTrue(iter.hasNext());
    {
      RoomInfo roomInfo = iter.next();
      // assertEquals("m6_0000_double2.jpg", roomInfo.roomPhotoPath);
      assertEquals("43.99USD", roomInfo.rate);
      assertEquals(
          "32\" LCD TV WITH AV CONNECTIONS FOR GAMING, WOOD-EFFECT FLOORS, BATHROOM WITH GRANITE COUNTERTOPS AND A RAISED WASH BASIN.",
          roomInfo.description);
      assertEquals("1 FULL BED | ADA ACCESSIBLE", roomInfo.amenities);
    }
  }
コード例 #12
0
  @Test
  public final void givenUsingCommonsIo_whenConvertingAnInputStreamToAString_thenCorrect()
      throws IOException {
    final String originalString = randomAlphabetic(DEFAULT_SIZE);
    final InputStream inputStream = new ByteArrayInputStream(originalString.getBytes());

    // When
    final String text = IOUtils.toString(inputStream, StandardCharsets.UTF_8.name());
    assertThat(text, equalTo(originalString));
  }
コード例 #13
0
  /** {@inheritDoc} */
  @Override
  public boolean execute(IActionHandler actionHandler, final Map<String, Object> context) {
    String chartUrl =
        ResourceProviderServlet.computeLocalResourceDownloadUrl(
            getChartDescriptor().getUrl(), true);
    String chartData =
        getJdbcTemplate()
            .execute(
                new ConnectionCallback<String>() {

                  @Override
                  public String doInConnection(Connection con) throws SQLException {
                    return getChartDescriptor()
                        .getData(
                            getChartModel(context),
                            con,
                            getTranslationProvider(context),
                            getLocale(context));
                  }
                });
    IResource resource;
    try {
      resource =
          new MemoryResource(
              null,
              "text/xml",
              StringUtils.prependUtf8Bom(chartData).getBytes(StandardCharsets.UTF_8.name()));
    } catch (UnsupportedEncodingException ex) {
      throw new ActionException(ex);
    }
    String resourceId = ResourceManager.getInstance().register(resource);
    Map<String, String> flashContext = new LinkedHashMap<>();
    Dimension d = getChartDescriptor().getDimension();
    flashContext.put("chartWidth", Integer.toString(d.getWidth() - 20));
    flashContext.put("chartHeight", Integer.toString(d.getHeight() - 100));
    flashContext.put("dataURL", ResourceProviderServlet.computeDownloadUrl(resourceId));
    List<G> chartActions = new ArrayList<>();
    for (IDisplayableAction action : getActions()) {
      IView<E> view = getView(context);
      chartActions.add(
          getActionFactory(context).createAction(action, actionHandler, view, getLocale(context)));
    }
    getController(context)
        .displayFlashObject(
            chartUrl,
            flashContext,
            chartActions,
            getTranslationProvider(context)
                .getTranslation(getChartDescriptor().getTitle(), getLocale(context)),
            getSourceComponent(context),
            context,
            d,
            false);
    return super.execute(actionHandler, context);
  }
コード例 #14
0
  @Bean
  public ServletRegistrationBean servletRegistrationBean() {
    ServletRegistrationBean result =
        new ServletRegistrationBean(new CGIServlet(), this.cgiPathMapping);

    result.addInitParameter("executable", "");
    result.addInitParameter("cgiPathPrefix", this.cgiPathPrefix);
    result.addInitParameter("parameterEncoding", StandardCharsets.UTF_8.displayName());
    result.addInitParameter("passShellEnvironment", "true");

    return result;
  }
コード例 #15
0
 @Test
 public void testCanHandle_ReturnFalseForNodeNotGlobalEndpoint() throws Exception {
   Element nodeToHandle =
       DocumentBuilderFactory.newInstance()
           .newDocumentBuilder()
           .parse(
               new ByteArrayInputStream(
                   "<bindy type=\"KeyValue\"/>".getBytes(StandardCharsets.UTF_8.name())))
           .getDocumentElement();
   doReturn(nodeToHandle).when(cme).getXmlNode();
   assertThat(new GlobalEndpointContributor().canHandle(cme)).isFalse();
 }
コード例 #16
0
  @Test
  public final void givenUsingCommonsIoWithCopy_whenConvertingAnInputStreamToAString_thenCorrect()
      throws IOException {
    final String originalString = randomAlphabetic(DEFAULT_SIZE);
    final InputStream inputStream = new ByteArrayInputStream(originalString.getBytes());

    // When
    final StringWriter writer = new StringWriter();
    final String encoding = StandardCharsets.UTF_8.name();
    IOUtils.copy(inputStream, writer, encoding);

    assertThat(writer.toString(), equalTo(originalString));
  }
コード例 #17
0
 @Test
 public void testCanHandle_GlobalEndpointWithPrefix() throws Exception {
   Element nodeToHandle =
       DocumentBuilderFactory.newInstance()
           .newDocumentBuilder()
           .parse(
               new ByteArrayInputStream(
                   "<myPrefix:endpoint id=\"aaa\" uri=\"dozer:aaa?sourceModel=com.mycompany.camel.spring.sss&amp;targetModel=com.mycompany.camel.spring.sss&amp;mappingFile=transformation.xml\"/>"
                       .getBytes(StandardCharsets.UTF_8.name())))
           .getDocumentElement();
   doReturn(nodeToHandle).when(cme).getXmlNode();
   assertThat(new GlobalEndpointContributor().canHandle(cme)).isTrue();
 }
コード例 #18
0
 private String toJson(final NormalizedNodeContext readData) throws IOException {
   final NormalizedNodeJsonBodyWriter writer = new NormalizedNodeJsonBodyWriter();
   final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
   writer.writeTo(
       readData,
       NormalizedNodeContext.class,
       null,
       EMPTY_ANNOTATIONS,
       MediaType.APPLICATION_JSON_TYPE,
       null,
       outputStream);
   return outputStream.toString(StandardCharsets.UTF_8.name());
 }
コード例 #19
0
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {

    String path = req.getRequestURI().substring(req.getContextPath().length());

    InputStream reqInputStream = req.getInputStream();
    String bodyContent = IOUtils.toString(reqInputStream, StandardCharsets.UTF_8.name());
    if (bodyContent != null && !bodyContent.isEmpty()) {
      LOGGER.info("request body content:{}", bodyContent);
    }

    String outputMime = null;
    OutputStream out = resp.getOutputStream();

    InputStream is = null;

    if (!req.getMethod().equalsIgnoreCase(HttpMethod.GET)) {
      LOGGER.info("checking for path:/{}{}.json", req.getMethod(), path);
      is = this.getClass().getResourceAsStream("/" + req.getMethod() + path + ".json");
    }

    if (is == null) {
      LOGGER.info("checking for path:{}.json", path);
      is = this.getClass().getResourceAsStream(path + ".json");
    }

    if (is != null) {
      outputMime = MediaType.APPLICATION_JSON;
    } else {
      LOGGER.info("checking for path:{}.txt", path);
      is = this.getClass().getResourceAsStream(path + ".txt");
      if (is != null) {
        outputMime = MediaType.APPLICATION_OCTET_STREAM;
      }
    }

    if (outputMime != null) {
      resp.setContentType(outputMime);
    }

    if (is == null) {
      resp.sendError(HttpServletResponse.SC_NOT_FOUND);
    } else {
      IOUtils.copy(is, out);
      is.close();
    }

    out.close();
  }
コード例 #20
0
 /**
  * @param query The search term to query JStor for.
  * @return a list of IDs
  * @throws java.io.IOException
  */
 private static List<String> getCitations(String query) throws IOException {
   String urlQuery;
   List<String> ids = new ArrayList<>();
   urlQuery =
       ScienceDirectFetcher.SEARCH_URL + URLEncoder.encode(query, StandardCharsets.UTF_8.name());
   int count = 1;
   String nextPage;
   while (((nextPage = getCitationsFromUrl(urlQuery, ids)) != null)
       && (count < ScienceDirectFetcher.MAX_PAGES_TO_LOAD)) {
     urlQuery = nextPage;
     count++;
   }
   return ids;
 }
コード例 #21
0
 public Promise<List<SpatialAddress>, Throwable, Float> geocode(Address address) {
   RequestLocation loc = new RequestLocation();
   String location = loc.toJSONString(address);
   try {
     location =
         "&json={\"location\":" + URLEncoder.encode(location, StandardCharsets.UTF_8.name());
     String url =
         serviceUrl.toString() + "/address?key=" + this.apiKey + "&maxResults=1" + location;
     return this.sendAsyncSvcRequest(url);
   } catch (UnsupportedEncodingException e) {
     LOG.warn(e.getMessage(), e);
   }
   return null; // TODO
 }
コード例 #22
0
ファイル: WebWiring.java プロジェクト: c0c0n3/ome-smuggler
  @Bean
  public UndertowEmbeddedServletContainerFactory embeddedServletContainerFactory(
      ConfigProvider<UndertowConfig> cfg) {

    int port = cfg.defaultReadConfig().findFirst().get().getPort();

    UndertowEmbeddedServletContainerFactory factory =
        new UndertowEmbeddedServletContainerFactory(port);

    factory.addBuilderCustomizers(
        (UndertowBuilderCustomizer)
            builder -> { // (*)
              builder.setServerOption(UndertowOptions.DECODE_URL, true);
              builder.setServerOption(UndertowOptions.URL_CHARSET, StandardCharsets.UTF_8.name());
            });

    factory.addDeploymentInfoCustomizers(
        (UndertowDeploymentInfoCustomizer)
            deployment -> { // (*)
              deployment.setDefaultEncoding(StandardCharsets.UTF_8.name());
            });

    return factory;
  }
コード例 #23
0
  @Test
  public final void givenUsingJava7_whenConvertingAnInputStreamToAString_thenCorrect()
      throws IOException {
    final String originalString = randomAlphabetic(DEFAULT_SIZE);
    final InputStream inputStream =
        new ByteArrayInputStream(
            originalString.getBytes()); // exampleString.getBytes(StandardCharsets.UTF_8);

    // When
    String text = null;
    try (Scanner scanner = new Scanner(inputStream, StandardCharsets.UTF_8.name())) {
      text = scanner.useDelimiter("\\A").next();
    }

    assertThat(text, equalTo(originalString));
  }
コード例 #24
0
  @Test
  public final void givenUsingJava5_whenConvertingAnInputStreamToAString_thenCorrect()
      throws IOException {
    final String originalString = randomAlphabetic(DEFAULT_SIZE);
    final InputStream inputStream = new ByteArrayInputStream(originalString.getBytes());

    final StringBuilder textBuilder = new StringBuilder();
    try (Reader reader =
        new BufferedReader(
            new InputStreamReader(inputStream, Charset.forName(StandardCharsets.UTF_8.name())))) {
      int c = 0;
      while ((c = reader.read()) != -1) {
        textBuilder.append((char) c);
      }
    }
    assertEquals(textBuilder.toString(), originalString);
  }
コード例 #25
0
 public Long getRxcuiByUnii(String unii) throws IOException {
   UriComponentsBuilder builder =
       UriComponentsBuilder.fromHttpUrl(this.nlmRxnavUrl + ".json")
           .queryParam("idtype", "UNII_CODE")
           .queryParam("id", URLEncoder.encode(unii, StandardCharsets.UTF_8.name()));
   String result = rest.getForObject(builder.build().toUri(), String.class);
   FieldFinder finder = new FieldFinder("rxnormId");
   for (String id : finder.find(result)) {
     try {
       return Long.parseLong(id);
     } catch (NumberFormatException nfe) {
       // ignore invalid ids
       log.warn("Got invalid rxnormId from Rxnav query on unii" + unii);
     }
   }
   return null;
 }
コード例 #26
0
 @Override
 public Promise<List<SpatialAddress>, Throwable, Float> geocodeBatch(List<Address> addresses) {
   try {
     GeocodeRequest req = new GeocodeRequest(addresses);
     String url =
         serviceUrl.toString()
             + "/batch?key="
             + this.apiKey
             + "&maxResults=1"
             + "&json="
             + URLEncoder.encode(req.getLocations(), StandardCharsets.UTF_8.name());
     return this.sendAsyncSvcRequest(url);
   } catch (UnsupportedEncodingException e) {
     LOG.warn(e.getMessage(), e);
   }
   return null;
 }
コード例 #27
0
  /** {@inheritDoc} */
  @Override
  public URI getAuthorizationRequestURI(String clientID, String callback, String state) {
    URI uri = null;
    String uriTpl = uriStrings.getProperty(PROP_AUTHORIZATION_REDIRECT);
    try {
      String utf8 = StandardCharsets.UTF_8.name();
      String clientIDEnc = URLEncoder.encode(clientID, utf8);
      String callbackEnc = URLEncoder.encode(callback, utf8);
      String stateEnc = URLEncoder.encode(state, utf8);

      String uriString = String.format(uriTpl, clientIDEnc, callbackEnc, stateEnc);
      uri = URI.create(uriString);
    } catch (UnsupportedEncodingException e) {
      // Encoding name is taken from a system constant. This can never happen.
    }

    return uri;
  }
コード例 #28
0
 @NotNull
 public HttpUriRequest createRequest(
     @NotNull Descriptors.MethodDescriptor method, @NotNull Message request)
     throws IOException, URISyntaxException {
   final HttpPost post =
       new HttpPost(
           baseUri.resolve(
               method.getService().getName().toLowerCase()
                   + "/"
                   + method.getName().toLowerCase()
                   + format.getSuffix()));
   post.setHeader(HttpHeaders.CONTENT_TYPE, format.getMimeType());
   post.setHeader(HttpHeaders.CONTENT_ENCODING, StandardCharsets.UTF_8.name());
   final ByteArrayOutputStream stream = new ByteArrayOutputStream();
   format.write(request, stream, StandardCharsets.UTF_8);
   post.setEntity(new ByteArrayEntity(stream.toByteArray()));
   return post;
 }
コード例 #29
0
 /** Gets String contents from channel and closes it. */
 public static String getStringContents(ReadableByteChannel channel) throws IOException {
   // TODO Checks if a supplier would be nice
   try {
     ByteBuffer buffer = ByteBuffer.allocate(1024 * 8);
     StringBuilder sb = new StringBuilder();
     int bytesRead = channel.read(buffer);
     while (bytesRead != -1) {
       buffer.flip();
       CharBuffer charBuffer = StandardCharsets.UTF_8.decode(buffer);
       sb.append(charBuffer.toString());
       buffer.clear();
       bytesRead = channel.read(buffer);
     }
     return sb.toString();
   } finally {
     channel.close();
   }
 }
コード例 #30
0
  @Override
  public void handle(RoutingContext context) {
    HttpServerRequest req = context.request();
    String remainingPath = Utils.pathOffset(req.path(), context);

    if (req.method() != HttpMethod.GET && req.method() != HttpMethod.POST) {
      context.next();
    }

    JSONAware json = null;
    try {
      // Check access policy
      requestHandler.checkAccess(
          req.remoteAddress().host(), req.remoteAddress().host(), getOriginOrReferer(req));
      if (req.method() == HttpMethod.GET) {
        json = requestHandler.handleGetRequest(req.uri(), remainingPath, getParams(req.params()));
      } else {
        Arguments.require(
            context.getBody() != null, "Missing body, make sure that BodyHandler is used before");
        // TODO how to get Stream ?
        InputStream inputStream = new ByteBufInputStream(context.getBody().getByteBuf());
        json =
            requestHandler.handlePostRequest(
                req.uri(), inputStream, StandardCharsets.UTF_8.name(), getParams(req.params()));
      }
    } catch (Throwable exp) {
      json =
          requestHandler.handleThrowable(
              exp instanceof RuntimeMBeanException
                  ? ((RuntimeMBeanException) exp).getTargetException()
                  : exp);
    } finally {
      if (json == null)
        json =
            requestHandler.handleThrowable(
                new Exception("Internal error while handling an exception"));

      context
          .response()
          .setStatusCode(getStatusCode(json))
          .putHeader(HttpHeaders.CONTENT_TYPE, contentType)
          .end(json.toJSONString());
    }
  }