예제 #1
0
  private String getContentCharset(byte[] content, Metadata metadata) {
    String charset = null;

    // check if the server specified a charset
    String specifiedContentType = metadata.getFirstValue(HttpHeaders.CONTENT_TYPE);
    try {
      if (specifiedContentType != null) {
        ContentType parsedContentType = ContentType.parse(specifiedContentType);
        charset = parsedContentType.getCharset().name();
      }
    } catch (Exception e) {
      charset = null;
    }

    // filter HTML tags
    CharsetDetector detector = new CharsetDetector();
    detector.enableInputFilter(true);
    // give it a hint
    detector.setDeclaredEncoding(charset);
    detector.setText(content);
    try {
      CharsetMatch charsetMatch = detector.detect();
      if (charsetMatch != null) {
        charset = charsetMatch.getName();
      }
    } catch (Exception e) {
      // ignore and leave the charset as-is
    }
    return charset;
  }
예제 #2
0
 private ContentType receiveWithCharsetParameter(ContentType contentType, Charset charset) {
   if (contentType.getCharset() != null) {
     return contentType;
   }
   final String mimeType = contentType.getMimeType();
   if (mimeType.equals(ContentType.TEXT_PLAIN.getMimeType())
       || AbstractFutureCallback.ODATA_MIME_TYPE.matcher(mimeType).matches()) {
     return contentType.withCharset(charset);
   }
   return contentType;
 }
 private CoverallsResponse parseResponse(final HttpResponse response)
     throws ProcessingException, IOException {
   HttpEntity entity = response.getEntity();
   ContentType contentType = ContentType.getOrDefault(entity);
   InputStreamReader reader = null;
   try {
     reader = new InputStreamReader(entity.getContent(), contentType.getCharset());
     CoverallsResponse cr = objectMapper.readValue(reader, CoverallsResponse.class);
     if (cr.isError()) {
       throw new ProcessingException(getResponseErrorMessage(response, cr.getMessage()));
     }
     return cr;
   } catch (JsonProcessingException ex) {
     throw new ProcessingException(getResponseErrorMessage(response, ex.getMessage()), ex);
   } catch (IOException ex) {
     throw new IOException(getResponseErrorMessage(response, ex.getMessage()), ex);
   } finally {
     IOUtil.close(reader);
   }
 }
예제 #4
0
  public static Reader createReaderFromResponse(HttpResponse theResponse)
      throws IllegalStateException, IOException {
    HttpEntity entity = theResponse.getEntity();
    if (entity == null) {
      return new StringReader("");
    }
    Charset charset = null;
    if (entity.getContentType() != null
        && entity.getContentType().getElements() != null
        && entity.getContentType().getElements().length > 0) {
      ContentType ct = ContentType.get(entity);
      charset = ct.getCharset();
    }
    if (charset == null) {
      if (Constants.STATUS_HTTP_204_NO_CONTENT != theResponse.getStatusLine().getStatusCode()) {
        ourLog.warn("Response did not specify a charset.");
      }
      charset = Charset.forName("UTF-8");
    }

    Reader reader = new InputStreamReader(theResponse.getEntity().getContent(), charset);
    return reader;
  }
예제 #5
0
  @Test
  public void testHttpContent() throws Exception {

    final String[] patterns = {
      "0123456789ABCDEF",
      "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-"
          + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-"
          + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-"
          + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-"
          + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-"
          + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-"
          + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-"
          + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-"
          + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-"
          + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-"
          + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-"
          + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-"
          + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-"
          + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that-"
          + "yadayada-blahblah-this-and-that-yadayada-blahblah-this-and-that"
    };

    // Initialize the server-side request handler
    this.server.registerHandler(
        "*",
        new HttpRequestHandler() {

          public void handle(
              final HttpRequest request, final HttpResponse response, final HttpContext context)
              throws HttpException, IOException {

            if (request instanceof HttpEntityEnclosingRequest) {
              int n = 1;
              String s = request.getRequestLine().getUri();
              if (s.startsWith("/?n=")) {
                s = s.substring(4);
                try {
                  n = Integer.parseInt(s);
                  if (n <= 0) {
                    throw new HttpException(
                        "Invalid request: " + "number of repetitions cannot be negative or zero");
                  }
                } catch (final NumberFormatException ex) {
                  throw new HttpException("Invalid request: " + "number of repetitions is invalid");
                }
              }

              final HttpEntity incoming = ((HttpEntityEnclosingRequest) request).getEntity();
              final String line = EntityUtils.toString(incoming);
              final ContentType contentType = ContentType.getOrDefault(incoming);
              Charset charset = contentType.getCharset();
              if (charset == null) {
                charset = HTTP.DEF_CONTENT_CHARSET;
              }
              final RepeatingEntity outgoing = new RepeatingEntity(line, charset, n);
              outgoing.setChunked(n % 2 == 0);
              response.setEntity(outgoing);
            } else {
              throw new HttpException("Invalid request: POST request expected");
            }
          }
        });

    this.server.start();
    final DefaultBHttpClientConnection conn = client.createConnection();
    final HttpHost host = new HttpHost("localhost", this.server.getPort());

    try {
      for (final String pattern : patterns) {
        for (int n = 1000; n < 1020; n++) {
          if (!conn.isOpen()) {
            client.connect(host, conn);
          }

          final BasicHttpEntityEnclosingRequest post =
              new BasicHttpEntityEnclosingRequest("POST", "/?n=" + n);
          final StringEntity outgoing = new StringEntity(pattern);
          outgoing.setChunked(n % 2 == 0);
          post.setEntity(outgoing);

          final HttpResponse response = this.client.execute(post, host, conn);
          final HttpEntity incoming = response.getEntity();
          Assert.assertNotNull(incoming);
          final InputStream instream = incoming.getContent();
          final ContentType contentType = ContentType.getOrDefault(incoming);
          Charset charset = contentType.getCharset();
          if (charset == null) {
            charset = HTTP.DEF_CONTENT_CHARSET;
          }
          Assert.assertNotNull(instream);
          final BufferedReader reader =
              new BufferedReader(new InputStreamReader(instream, charset));

          String line;
          int count = 0;
          while ((line = reader.readLine()) != null) {
            Assert.assertEquals(pattern, line);
            count++;
          }
          Assert.assertEquals(n, count);
          if (!this.client.keepAlive(response)) {
            conn.close();
          }
        }
      }
    } finally {
      conn.close();
      this.server.shutdown();
    }
  }
  private Document getHttpMessageContent(HttpMessage message) {
    HttpEntity entity = null;

    if (message instanceof HttpEntityEnclosingRequest) {
      entity = ((HttpEntityEnclosingRequest) message).getEntity();
    } else if (message instanceof HttpResponse) {
      entity = ((HttpResponse) message).getEntity();
    }

    Document doc = null;

    // redirecting reply to and fault to nodes if present
    if (entity != null) {
      try (InputStream is = entity.getContent()) {
        ContentType contentType = null;
        for (Header header : message.getHeaders("Content-Type")) {
          try {
            contentType = ContentType.parse(header.getValue());
          } catch (ParseException | UnsupportedCharsetException ex) {
          }
        }

        if (contentType != null) {
          Charset charset = contentType.getCharset();
          if (charset == null) {
            charset = Charset.forName("UTF-8");
          }
          String mimeType = contentType.getMimeType();

          if (mimeType.contains("xml")) {
            String content = IOUtils.toString(is, charset.name());

            Document xmlDocument = XMLUtils.parseXML(content);
            if (xmlDocument != null) {
              XPath xpath = XPathFactory.newInstance().newXPath();
              xpath.setNamespaceContext(new FimsNamespaceContext());

              NodeList nodes =
                  (NodeList)
                      xpath.evaluate(
                          "/S:Envelope/S:Body/S:Fault/detail/*",
                          xmlDocument.getDocumentElement(),
                          XPathConstants.NODESET);

              if (nodes.getLength() == 0) {
                nodes =
                    (NodeList)
                        xpath.evaluate(
                            "/S:Envelope/S:Body/*",
                            xmlDocument.getDocumentElement(),
                            XPathConstants.NODESET);
              }

              doc = XMLUtils.newEmptyDocument();

              if (nodes.getLength() > 0) {
                Node node = nodes.item(0);
                Node newNode = doc.importNode(node, true);
                doc.appendChild(newNode);
              }
            }
          }
        }
      } catch (IOException | UnsupportedOperationException | XPathExpressionException ex) {
        Logger.getLogger(ValidationModuleImpl.class.getName()).log(Level.SEVERE, null, ex);
      }
    }

    return doc;
  }
예제 #7
0
  private Olingo2BatchResponse parseResponse(
      Edm edm,
      Map<String, String> contentIdLocationMap,
      Olingo2BatchRequest request,
      BatchSingleResponse response)
      throws EntityProviderException, ODataApplicationException {

    // validate HTTP status
    final int statusCode = Integer.parseInt(response.getStatusCode());
    final String statusInfo = response.getStatusInfo();

    final BasicHttpResponse httpResponse =
        new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, statusCode, statusInfo));
    final Map<String, String> headers = response.getHeaders();
    for (Map.Entry<String, String> entry : headers.entrySet()) {
      httpResponse.setHeader(entry.getKey(), entry.getValue());
    }

    ByteArrayInputStream content = null;
    try {
      if (response.getBody() != null) {
        final ContentType partContentType =
            receiveWithCharsetParameter(
                ContentType.parse(headers.get(HttpHeaders.CONTENT_TYPE)), Consts.UTF_8);
        final String charset = partContentType.getCharset().toString();

        final String body = response.getBody();
        content = body != null ? new ByteArrayInputStream(body.getBytes(charset)) : null;

        httpResponse.setEntity(new StringEntity(body, charset));
      }

      AbstractFutureCallback.checkStatus(httpResponse);
    } catch (ODataApplicationException e) {
      return new Olingo2BatchResponse(
          statusCode, statusInfo, response.getContentId(), response.getHeaders(), e);
    } catch (UnsupportedEncodingException e) {
      return new Olingo2BatchResponse(
          statusCode, statusInfo, response.getContentId(), response.getHeaders(), e);
    }

    // resolve resource path and query params and parse batch part uri
    final String resourcePath = request.getResourcePath();
    final String resolvedResourcePath;
    if (resourcePath.startsWith("$")
        && !(METADATA.equals(resourcePath) || BATCH.equals(resourcePath))) {
      resolvedResourcePath = findLocation(resourcePath, contentIdLocationMap);
    } else {
      final String resourceLocation = response.getHeader(HttpHeaders.LOCATION);
      resolvedResourcePath =
          resourceLocation != null ? resourceLocation.substring(serviceUri.length()) : resourcePath;
    }
    final Map<String, String> resolvedQueryParams =
        request instanceof Olingo2BatchQueryRequest
            ? ((Olingo2BatchQueryRequest) request).getQueryParams()
            : null;
    final UriInfoWithType uriInfo = parseUri(edm, resolvedResourcePath, resolvedQueryParams);

    // resolve response content
    final Object resolvedContent = content != null ? readContent(uriInfo, content) : null;

    return new Olingo2BatchResponse(
        statusCode, statusInfo, response.getContentId(), response.getHeaders(), resolvedContent);
  }
  @Override
  public ProblemAndTestCaseList importExercise(Course course, String exerciseHash)
      throws CloudCoderAuthenticationException {
    if (course == null || exerciseHash == null) {
      throw new IllegalArgumentException();
    }

    // Make sure a user is authenticated
    User user =
        ServletUtil.checkClientIsAuthenticated(
            getThreadLocalRequest(), GetCoursesAndProblemsServiceImpl.class);

    // Find user's registration in the course: if user is not instructor,
    // import is not allowed
    CourseRegistrationList reg = Database.getInstance().findCourseRegistrations(user, course);
    if (!reg.isInstructor()) {
      throw new CloudCoderAuthenticationException(
          "Only an instructor can import a problem in a course");
    }

    // Attempt to load the problem from the exercise repository.
    ConfigurationSetting repoUrlSetting =
        Database.getInstance().getConfigurationSetting(ConfigurationSettingName.PUB_REPOSITORY_URL);
    if (repoUrlSetting == null) {
      logger.error("Repository URL configuration setting is not set");
      return null;
    }

    // GET the exercise from the repository
    HttpGet get = new HttpGet(repoUrlSetting.getValue() + "/exercisedata/" + exerciseHash);
    ProblemAndTestCaseList exercise = null;

    HttpClient client = new DefaultHttpClient();
    try {
      HttpResponse response = client.execute(get);

      HttpEntity entity = response.getEntity();

      ContentType contentType = ContentType.getOrDefault(entity);
      Reader reader = new InputStreamReader(entity.getContent(), contentType.getCharset());

      exercise = new ProblemAndTestCaseList();
      exercise.setTestCaseList(new TestCase[0]);
      JSONConversion.readProblemAndTestCaseData(
          exercise,
          ReflectionFactory.forClass(Problem.class),
          ReflectionFactory.forClass(TestCase.class),
          reader);

      // Set the course id
      exercise.getProblem().setCourseId(course.getId());
    } catch (IOException e) {
      logger.error("Error importing exercise from repository", e);
      return null;
    } finally {
      client.getConnectionManager().shutdown();
    }

    // Set "when assigned" and "when due" to reasonable default values
    long now = System.currentTimeMillis();
    exercise.getProblem().setWhenAssigned(now);
    exercise.getProblem().setWhenDue(now + 48L * 60L * 60L * 1000L);

    // Set problem authorship as IMPORTED
    exercise.getProblem().setProblemAuthorship(ProblemAuthorship.IMPORTED);

    // For IMPORTED problems, parent_hash is actually the hash of the problem
    // itself.  If the problem is modified (and the authorship changed
    // to IMPORTED_AND_MODIFIED), then the (unchanged) parent_hash value
    // really does reflect the "parent" problem.
    exercise.getProblem().setParentHash(exerciseHash);

    // Store the exercise in the database
    exercise = Database.getInstance().storeProblemAndTestCaseList(exercise, course, user);

    return exercise;
  }
예제 #9
0
 /** @since 4.3 */
 public StringBody(final String text, final ContentType contentType) {
   super(contentType);
   Args.notNull(text, "Text");
   final Charset charset = contentType.getCharset();
   this.content = text.getBytes(charset != null ? charset : Consts.ASCII);
 }