Пример #1
0
  public static void writeXML(SolrInputDocument doc, Writer writer) throws IOException {
    writer.write("<doc boost=\"" + doc.getDocumentBoost() + "\">");

    for (SolrInputField field : doc) {
      float boost = field.getBoost();
      String name = field.getName();
      for (Object v : field) {
        if (v instanceof Date) {
          v = DateUtil.getThreadLocalDateFormat().format((Date) v);
        } else if (v instanceof byte[]) {
          byte[] bytes = (byte[]) v;
          v = Base64.byteArrayToBase64(bytes, 0, bytes.length);
        } else if (v instanceof ByteBuffer) {
          ByteBuffer bytes = (ByteBuffer) v;
          v =
              Base64.byteArrayToBase64(
                  bytes.array(), bytes.position(), bytes.limit() - bytes.position());
        }

        if (boost != 1.0f) {
          XML.writeXML(writer, "field", v.toString(), "name", name, "boost", boost);
        } else if (v != null) {
          XML.writeXML(writer, "field", v.toString(), "name", name);
        }

        // only write the boost for the first multi-valued field
        // otherwise, the used boost is the product of all the boost values
        boost = 1.0f;
      }
    }
    writer.write("</doc>");
  }
Пример #2
0
 public static void main(String[] args) throws Exception {
   RSAKeyPair keyPair = new RSAKeyPair();
   System.out.println(keyPair.getPublicKeyStr());
   PublicKey pk = deserializeX509PublicKey(keyPair.getPublicKeyStr());
   byte[] payload = "Hello World!".getBytes(StandardCharsets.UTF_8);
   byte[] encrypted = keyPair.encrypt(ByteBuffer.wrap(payload));
   String cipherBase64 = Base64.byteArrayToBase64(encrypted);
   System.out.println("encrypted: " + cipherBase64);
   System.out.println("signed: " + Base64.byteArrayToBase64(keyPair.signSha256(payload)));
   System.out.println(
       "decrypted " + new String(decryptRSA(encrypted, pk), StandardCharsets.UTF_8));
 }
Пример #3
0
 /** Marshals a binary field value. */
 protected static Object marshalBase64SortValue(Object value) {
   if (null == value) {
     return null;
   }
   final BytesRef val = (BytesRef) value;
   return Base64.byteArrayToBase64(val.bytes, val.offset, val.length);
 }
Пример #4
0
  private static SimpleOrderedMap<Object> getDocumentFieldsInfo(
      Document doc, int docId, IndexReader reader, IndexSchema schema) throws IOException {
    SimpleOrderedMap<Object> finfo = new SimpleOrderedMap<Object>();
    for (Object o : doc.getFields()) {
      Fieldable fieldable = (Fieldable) o;
      SimpleOrderedMap<Object> f = new SimpleOrderedMap<Object>();

      SchemaField sfield = schema.getFieldOrNull(fieldable.name());
      FieldType ftype = (sfield == null) ? null : sfield.getType();

      f.add("type", (ftype == null) ? null : ftype.getTypeName());
      f.add("schema", getFieldFlags(sfield));
      f.add("flags", getFieldFlags(fieldable));

      Term t =
          new Term(
              fieldable.name(),
              ftype != null ? ftype.storedToIndexed(fieldable) : fieldable.stringValue());

      f.add("value", (ftype == null) ? null : ftype.toExternal(fieldable));

      // TODO: this really should be "stored"
      f.add("internal", fieldable.stringValue()); // may be a binary number

      byte[] arr = fieldable.getBinaryValue();
      if (arr != null) {
        f.add("binary", Base64.byteArrayToBase64(arr, 0, arr.length));
      }
      f.add("boost", fieldable.getBoost());
      f.add(
          "docFreq",
          t.text() == null ? 0 : reader.docFreq(t)); // this can be 0 for non-indexed fields

      // If we have a term vector, return that
      if (fieldable.isTermVectorStored()) {
        try {
          TermFreqVector v = reader.getTermFreqVector(docId, fieldable.name());
          if (v != null) {
            SimpleOrderedMap<Integer> tfv = new SimpleOrderedMap<Integer>();
            for (int i = 0; i < v.size(); i++) {
              tfv.add(v.getTerms()[i], v.getTermFrequencies()[i]);
            }
            f.add("termVector", tfv);
          }
        } catch (Exception ex) {
          log.warn("error writing term vector", ex);
        }
      }

      finfo.add(fieldable.name(), f);
    }
    return finfo;
  }
Пример #5
0
    public RSAKeyPair() {
      KeyPairGenerator keyGen = null;
      try {
        keyGen = KeyPairGenerator.getInstance("RSA");
      } catch (NoSuchAlgorithmException e) {
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e);
      }
      keyGen.initialize(512);
      java.security.KeyPair keyPair = keyGen.genKeyPair();
      privateKey = keyPair.getPrivate();
      publicKey = keyPair.getPublic();

      X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded());

      pubKeyStr = Base64.byteArrayToBase64(x509EncodedKeySpec.getEncoded());
    }