示例#1
0
 public ArmoredPBEResult(byte[] enc, byte[] salt) {
   super();
   try {
     this.base64Enc = Base64.encodeBytes(enc, Base64.URL_SAFE);
     this.base64Salt = Base64.encodeBytes(salt, Base64.URL_SAFE);
   } catch (Exception x) {
     throw new RuntimeException(x);
   }
 }
示例#2
0
 public static String encode(byte[] bytes) {
   String s = Base64.encodeBytes(bytes);
   s = s.split("=")[0]; // Remove any trailing '='s
   s = s.replace('+', '-'); // 62nd char of encoding
   s = s.replace('/', '_'); // 63rd char of encoding
   return s;
 }
示例#3
0
 public byte[] getSaltBytes() {
   try {
     return Base64.decode(base64Salt, Base64.URL_SAFE);
   } catch (IOException e) {
     throw new RuntimeException(e);
   }
 }
  /**
   * {@inheritDoc}
   *
   * @return GridCoverageReader.
   */
  @Override
  public GridCoverageReader convert(final ReferenceType source, final Map<String, Object> params)
      throws UnconvertibleObjectException {

    final InputStream stream = getInputStreamFromReference(source);

    String encoding = null;
    if (params != null && params.get(ENCODING) != null) {
      encoding = (String) params.get(ENCODING);
    }
    ImageInputStream imageStream = null;
    try {

      // decode form base64 stream
      if (encoding != null && encoding.equals(WPSEncoding.BASE64.getValue())) {
        final String encodedImage = IOUtilities.toString(stream);
        final byte[] byteData = Base64.decode(encodedImage.trim());
        if (byteData != null && byteData.length > 0) {
          try (InputStream is = new ByteArrayInputStream(byteData)) {
            imageStream = ImageIO.createImageInputStream(is);
          }
        }

      } else {
        imageStream = ImageIO.createImageInputStream(stream);
      }

      if (imageStream != null) {
        final ImageReader reader;
        if (source.getMimeType() != null) {
          reader = XImageIO.getReaderByMIMEType(source.getMimeType(), imageStream, null, null);
        } else {
          reader = XImageIO.getReader(imageStream, null, Boolean.FALSE);
        }
        return CoverageIO.createSimpleReader(reader);
      } else {
        throw new UnconvertibleObjectException("Error during image stream acquisition.");
      }

    } catch (MalformedURLException ex) {
      throw new UnconvertibleObjectException(
          "Reference grid coverage invalid input : Malformed url", ex);
    } catch (CoverageStoreException ex) {
      throw new UnconvertibleObjectException(
          "Reference grid coverage invalid input : Can't read coverage", ex);
    } catch (IOException ex) {
      throw new UnconvertibleObjectException("Reference grid coverage invalid input : IO", ex);
    } finally {
      if (imageStream != null) {
        try {
          imageStream.close();
        } catch (IOException ex) {
          LOGGER.log(Level.WARNING, "Error during release the image stream.", ex);
        }
      }
    }
  }
示例#5
0
 @Override
 public void doGet(HttpServletRequest request, HttpServletResponse response)
     throws ServletException, IOException {
   String ctlExportKeyBase64 =
       URLDecoder.decode(request.getParameter(CTL_EXPORT_KEY_PARAMETER), "UTF-8");
   try {
     CtlSchemaExportKey key =
         (CtlSchemaExportKey) Base64.decodeToObject(ctlExportKeyBase64, Base64.URL_SAFE, null);
     FileData ctlExportData = cacheService.getExportedCtlSchema(key);
     ServletUtils.prepareDisposition(request, response, ctlExportData.getFileName());
     response.setContentType(ctlExportData.getContentType());
     response.setContentLength(ctlExportData.getFileData().length);
     response.setBufferSize(BUFFER);
     response.getOutputStream().write(ctlExportData.getFileData());
     response.flushBuffer();
   } catch (Exception e) {
     LOG.error("Unexpected error in CtlExportServlet.doGet: ", e);
     response.sendError(
         HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Unable to get file: " + e.getMessage());
   }
 }
示例#6
0
 public static byte[] decode(String s) {
   s = s.replace('-', '+'); // 62nd char of encoding
   s = s.replace('_', '/'); // 63rd char of encoding
   switch (s.length() % 4) // Pad with trailing '='s
   {
     case 0:
       break; // No pad chars in this case
     case 2:
       s += "==";
       break; // Two pad chars
     case 3:
       s += "=";
       break; // One pad char
     default:
       throw new RuntimeException("Illegal base64url string!");
   }
   try {
     return Base64.decode(s);
   } catch (Exception e) {
     throw new RuntimeException(e);
   }
 }
  public void fromJson(ObjectNode node, IndexDefinitionImpl index) {
    IndexGeneralState state = IndexGeneralState.valueOf(JsonUtil.getString(node, "generalState"));
    IndexUpdateState updateState =
        IndexUpdateState.valueOf(JsonUtil.getString(node, "updateState"));
    IndexBatchBuildState buildState =
        IndexBatchBuildState.valueOf(JsonUtil.getString(node, "batchBuildState"));

    String queueSubscriptionId = JsonUtil.getString(node, "queueSubscriptionId", null);

    byte[] configuration;
    try {
      String configurationAsString = JsonUtil.getString(node, "configuration");
      configuration = Base64.decode(configurationAsString);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }

    byte[] shardingConfiguration = null;
    if (node.get("shardingConfiguration") != null) {
      String shardingConfAsString = JsonUtil.getString(node, "shardingConfiguration");
      try {
        shardingConfiguration = Base64.decode(shardingConfAsString);
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    }

    Map<String, String> solrShards = new HashMap<String, String>();
    ArrayNode shardsArray = JsonUtil.getArray(node, "solrShards");
    for (int i = 0; i < shardsArray.size(); i++) {
      ObjectNode shardNode = (ObjectNode) shardsArray.get(i);
      String shardName = JsonUtil.getString(shardNode, "name");
      String address = JsonUtil.getString(shardNode, "address");
      solrShards.put(shardName, address);
    }

    ActiveBatchBuildInfo activeBatchBuild = null;
    if (node.get("activeBatchBuild") != null) {
      ObjectNode buildNode = JsonUtil.getObject(node, "activeBatchBuild");
      activeBatchBuild = new ActiveBatchBuildInfo();
      activeBatchBuild.setJobId(JsonUtil.getString(buildNode, "jobId"));
      activeBatchBuild.setSubmitTime(JsonUtil.getLong(buildNode, "submitTime"));
      activeBatchBuild.setTrackingUrl(JsonUtil.getString(buildNode, "trackingUrl", null));
      activeBatchBuild.setBatchIndexConfiguration(
          serializeJsonNode(JsonUtil.getObject(buildNode, "batchIndexConfiguration")));
    }

    BatchBuildInfo lastBatchBuild = null;
    if (node.get("lastBatchBuild") != null) {
      ObjectNode buildNode = JsonUtil.getObject(node, "lastBatchBuild");
      lastBatchBuild = new BatchBuildInfo();
      lastBatchBuild.setJobId(JsonUtil.getString(buildNode, "jobId"));
      lastBatchBuild.setSubmitTime(JsonUtil.getLong(buildNode, "submitTime"));
      lastBatchBuild.setSuccess(JsonUtil.getBoolean(buildNode, "success"));
      lastBatchBuild.setJobState(JsonUtil.getString(buildNode, "jobState"));
      lastBatchBuild.setTrackingUrl(JsonUtil.getString(buildNode, "trackingUrl", null));
      ObjectNode countersNode = JsonUtil.getObject(buildNode, "counters");
      Iterator<String> it = countersNode.getFieldNames();
      while (it.hasNext()) {
        String key = it.next();
        long value = JsonUtil.getLong(countersNode, key);
        lastBatchBuild.addCounter(key, value);
      }
      lastBatchBuild.setBatchIndexConfiguration(
          serializeJsonNode(JsonUtil.getObject(buildNode, "batchIndexConfiguration")));
    }
    byte[] batchIndexConfiguration = null;
    if (node.get("batchIndexConfiguration") != null) {
      batchIndexConfiguration =
          serializeJsonNode(JsonUtil.getObject(node, "batchIndexConfiguration"));
    }
    byte[] defaultBatchIndexConfiguration = null;
    if (node.get("defaultBatchIndexConfiguration") != null) {
      defaultBatchIndexConfiguration =
          serializeJsonNode(JsonUtil.getObject(node, "defaultBatchIndexConfiguration"));
    }

    index.setGeneralState(state);
    index.setUpdateState(updateState);
    index.setBatchBuildState(buildState);
    index.setQueueSubscriptionId(queueSubscriptionId);
    index.setConfiguration(configuration);
    index.setSolrShards(solrShards);
    index.setShardingConfiguration(shardingConfiguration);
    index.setActiveBatchBuildInfo(activeBatchBuild);
    index.setLastBatchBuildInfo(lastBatchBuild);
    index.setBatchIndexConfiguration(batchIndexConfiguration);
    index.setDefaultBatchIndexConfiguration(defaultBatchIndexConfiguration);
  }
  public ObjectNode toJson(IndexDefinition index) {
    ObjectNode node = JsonNodeFactory.instance.objectNode();

    node.put("name", index.getName());
    node.put("generalState", index.getGeneralState().toString());
    node.put("batchBuildState", index.getBatchBuildState().toString());
    node.put("updateState", index.getUpdateState().toString());

    node.put("zkDataVersion", index.getZkDataVersion());

    if (index.getQueueSubscriptionId() != null)
      node.put("queueSubscriptionId", index.getQueueSubscriptionId());

    String configurationAsString;
    try {
      configurationAsString = Base64.encodeBytes(index.getConfiguration(), Base64.GZIP);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }

    node.put("configuration", configurationAsString);

    if (index.getShardingConfiguration() != null) {
      String shardingConfAsString;
      try {
        shardingConfAsString = Base64.encodeBytes(index.getShardingConfiguration(), Base64.GZIP);
      } catch (IOException e) {
        throw new RuntimeException(e);
      }

      node.put("shardingConfiguration", shardingConfAsString);
    }

    ArrayNode shardsNode = node.putArray("solrShards");
    for (Map.Entry<String, String> shard : index.getSolrShards().entrySet()) {
      ObjectNode shardNode = shardsNode.addObject();
      shardNode.put("name", shard.getKey());
      shardNode.put("address", shard.getValue());
    }

    if (index.getActiveBatchBuildInfo() != null) {
      ActiveBatchBuildInfo buildInfo = index.getActiveBatchBuildInfo();
      ObjectNode buildNode = node.putObject("activeBatchBuild");
      buildNode.put("jobId", buildInfo.getJobId());
      buildNode.put("submitTime", buildInfo.getSubmitTime());
      buildNode.put("trackingUrl", buildInfo.getTrackingUrl());
      buildNode.put(
          "batchIndexConfiguration", deserializeByteArray(buildInfo.getBatchIndexConfiguration()));
    }

    if (index.getLastBatchBuildInfo() != null) {
      BatchBuildInfo buildInfo = index.getLastBatchBuildInfo();
      ObjectNode buildNode = node.putObject("lastBatchBuild");
      buildNode.put("jobId", buildInfo.getJobId());
      buildNode.put("submitTime", buildInfo.getSubmitTime());
      buildNode.put("success", buildInfo.getSuccess());
      buildNode.put("jobState", buildInfo.getJobState());
      if (buildInfo.getTrackingUrl() != null)
        buildNode.put("trackingUrl", buildInfo.getTrackingUrl());
      ObjectNode countersNode = buildNode.putObject("counters");
      for (Map.Entry<String, Long> counter : buildInfo.getCounters().entrySet()) {
        countersNode.put(counter.getKey(), counter.getValue());
      }
      buildNode.put(
          "batchIndexConfiguration", deserializeByteArray(buildInfo.getBatchIndexConfiguration()));
    }
    if (index.getBatchIndexConfiguration() != null) {
      node.put(
          "batchIndexConfiguration", this.deserializeByteArray(index.getBatchIndexConfiguration()));
    }
    if (index.getDefaultBatchIndexConfiguration() != null) {
      node.put(
          "defaultBatchIndexConfiguration",
          this.deserializeByteArray(index.getDefaultBatchIndexConfiguration()));
    }

    return node;
  }
示例#9
0
 /**
  * Decode a PEM string to DER format
  *
  * @param pem
  * @return
  * @throws java.io.IOException
  */
 public static byte[] pemToDer(String pem) throws IOException {
   pem = removeBeginEnd(pem);
   return Base64.decode(pem);
 }
  /**
   * The actual method that does the real send and connection handling
   *
   * @param url the URL to use for the HTTP POST request.
   * @param jsonPayloadObject the JSON payload of the POST request
   * @param pushApplicationId the registered applications identifier.
   * @param masterSecret the master secret for the push server.
   * @param callback the {@link MessageResponseCallback} that will be called once the POST request
   *     completes.
   * @param redirectUrls a list containing the previous redirectUrls, used to detect an infinite
   *     loop
   * @throws {@link org.jboss.aerogear.unifiedpush.exception.PushSenderHttpException} when
   *     delivering push message to Unified Push Server fails.
   * @throws {@link org.jboss.aerogear.unifiedpush.exception.PushSenderException} when generic error
   *     during sending occurs, such as an infinite redirect loop.
   */
  private void submitPayload(
      String url,
      String jsonPayloadObject,
      String pushApplicationId,
      String masterSecret,
      MessageResponseCallback callback,
      List<String> redirectUrls) {
    if (redirectUrls.contains(url)) {
      throw new PushSenderException(
          "The site contains an infinite redirect loop! Duplicate url: " + url);
    } else {
      redirectUrls.add(url);
    }

    HttpURLConnection httpURLConnection = null;
    try {
      final String credentials = pushApplicationId + ':' + masterSecret;
      final String encoded = Base64.encodeBytes(credentials.getBytes(UTF_8));

      // POST the payload to the UnifiedPush Server
      httpURLConnection =
          (HttpURLConnection)
              HttpRequestUtil.post(url, encoded, jsonPayloadObject, UTF_8, proxy, customTrustStore);

      final int statusCode = httpURLConnection.getResponseCode();
      logger.log(
          Level.INFO, String.format("HTTP Response code from UnifiedPush Server: %s", statusCode));

      // if we got a redirect, let's extract the 'Location' header from the response
      // and submit the payload again
      if (isRedirect(statusCode)) {
        String redirectURL = httpURLConnection.getHeaderField("Location");
        logger.log(Level.INFO, String.format("Performing redirect to '%s'", redirectURL));
        // execute the 'redirect'
        submitPayload(
            redirectURL,
            jsonPayloadObject,
            pushApplicationId,
            masterSecret,
            callback,
            redirectUrls);
      } else if (statusCode >= 400) {
        // treating any 400/500 error codes an an exception to a sending attempt:
        logger.log(Level.SEVERE, "The Unified Push Server returned status code: " + statusCode);
        throw new PushSenderHttpException(statusCode);
      } else {
        if (callback != null) {
          callback.onComplete();
        }
      }
    } catch (PushSenderHttpException pshe) {
      throw pshe;
    } catch (Exception e) {
      logger.log(Level.INFO, "Error happening while trying to send the push delivery request", e);

      throw new PushSenderException(e.getMessage(), e);
    } finally {
      // tear down
      if (httpURLConnection != null) {
        httpURLConnection.disconnect();
      }
    }
  }