Java Code Examples for javax.ws.rs.core.MediaType#TEXT_PLAIN_TYPE

The following examples show how to use javax.ws.rs.core.MediaType#TEXT_PLAIN_TYPE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractServingTest.java    From oryx with Apache License 2.0 6 votes vote down vote up
protected final Response getFormPostResponse(String data,
                                             String endpoint,
                                             Class<? extends OutputStream> compressingClass,
                                             String encoding) throws IOException {
  byte[] bytes;
  if (compressingClass == null) {
    bytes = data.getBytes(StandardCharsets.UTF_8);
  } else {
    bytes = compress(data, compressingClass);
  }
  MediaType type =
      encoding == null ? MediaType.TEXT_PLAIN_TYPE : new MediaType("application", encoding);
  InputStream in = new ByteArrayInputStream(bytes);
  StreamDataBodyPart filePart = new StreamDataBodyPart("data", in, "data", type);
  try (MultiPart multiPart = new MultiPart(MediaType.MULTIPART_FORM_DATA_TYPE)) {
    multiPart.getBodyParts().add(filePart);
    return target(endpoint).request().post(
        Entity.entity(multiPart, MediaType.MULTIPART_FORM_DATA_TYPE));
  }
}
 
Example 2
Source File: LocalStorageTest.java    From proarc with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testGetStreamProfile() throws Exception {
    LocalStorage storage = new LocalStorage();
    LocalObject lobject = storage.create();
    String dsID = BinaryEditor.FULL_ID;
    MediaType mime = MediaType.TEXT_PLAIN_TYPE;
    String label = "label";
    XmlStreamEditor editor = lobject.getEditor(FoxmlUtils.managedProfile(dsID, mime, label));
    assertNotNull(editor);
    assertNotNull(editor.getProfile());
    assertEquals(mime.toString(), editor.getProfile().getDsMIME());
    assertNull(editor.getProfile().getDsFormatURI());
    byte[] data = "data".getBytes("UTF-8");
    editor.write(data, 0, null);
    lobject.flush();

    List<DatastreamProfile> resultProfiles = lobject.getStreamProfile(null);
    assertNotNull(resultProfiles);
    assertEquals(1, resultProfiles.size());
    assertEquals(dsID, resultProfiles.get(0).getDsID());
}
 
Example 3
Source File: LocalStorageTest.java    From proarc with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testDatastreamEditorWriteBytes_ManagedAsInlined() throws Exception {
    LocalStorage storage = new LocalStorage();
    LocalObject lobject = storage.create();
    String dsID = "dsID";
    MediaType mime = MediaType.TEXT_PLAIN_TYPE;
    String label = "label";
    XmlStreamEditor editor = lobject.getEditor(FoxmlUtils.managedProfile(dsID, mime, label));
    assertNotNull(editor);
    assertNotNull(editor.getProfile());
    assertEquals(mime.toString(), editor.getProfile().getDsMIME());
    assertNull(editor.getProfile().getDsFormatURI());
    byte[] data = "data".getBytes("UTF-8");
    editor.write(data, 0, null);
    lobject.flush();

    // is managed
    DigitalObject dobj = lobject.getDigitalObject();
    DatastreamType ds = FoxmlUtils.findDatastream(dobj, dsID);
    assertNotNull(ds);
    assertEquals(ControlGroup.MANAGED.toExternal(), ds.getCONTROLGROUP());

    // is inlined
    DatastreamVersionType dsv = FoxmlUtils.findDataStreamVersion(dobj, dsID);
    assertNotNull(dsv);
    assertEquals(mime.toString(), dsv.getMIMETYPE());
    assertEquals(label, dsv.getLABEL());
    assertArrayEquals(data, dsv.getBinaryContent());
    assertNull(dsv.getContentLocation());
}
 
Example 4
Source File: RequestJob.java    From ingestion with Apache License 2.0 5 votes vote down vote up
/**
 * Set an Application Type to the request depending on a parameter and its corresponding
 * {@code MediaType}.
 *
 * @param webResource     Current target url.
 * @param applicationType ApplicationType to set.
 * @return
 */
public WebResource.Builder setApplicationType(WebResource webResource, String applicationType) {
    if ("TEXT".equals(applicationType)) {
        mediaType = MediaType.TEXT_PLAIN_TYPE;
    } else {
        mediaType = MediaType.APPLICATION_JSON_TYPE;
    }

    return webResource.accept(mediaType);
}
 
Example 5
Source File: MediaTypeHeaderProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
private static MediaType handleMediaTypeWithoutSubtype(String mType) {
    if (mType.startsWith(MediaType.MEDIA_TYPE_WILDCARD)) {
        String mTypeNext = mType.length() == 1 ? "" : mType.substring(1).trim();
        boolean mTypeNextEmpty = StringUtils.isEmpty(mTypeNext);
        if (mTypeNextEmpty || mTypeNext.startsWith(";")) {
            if (!mTypeNextEmpty) {
                Map<String, String> parameters = new LinkedHashMap<>();
                StringTokenizer st = new StringTokenizer(mType.substring(2).trim(), ";");
                while (st.hasMoreTokens()) {
                    addParameter(parameters, st.nextToken());
                }
                return new MediaType(MediaType.MEDIA_TYPE_WILDCARD,
                                     MediaType.MEDIA_TYPE_WILDCARD,
                                     parameters);
            }
            return MediaType.WILDCARD_TYPE;

        }
    }
    Message message = PhaseInterceptorChain.getCurrentMessage();
    if (message != null
        && !MessageUtils.getContextualBoolean(message, STRICT_MEDIA_TYPE_CHECK, false)) {
        MediaType mt = null;
        if (mType.equals(MediaType.TEXT_PLAIN_TYPE.getType())) {
            mt = MediaType.TEXT_PLAIN_TYPE;
        } else if (mType.equals(MediaType.APPLICATION_XML_TYPE.getSubtype())) {
            mt = MediaType.APPLICATION_XML_TYPE;
        } else {
            mt = MediaType.WILDCARD_TYPE;
        }
        LOG.fine("Converting a malformed media type '" + mType + "' to '" + typeToString(mt) + "'");
        return mt;
    }
    throw new IllegalArgumentException("Media type separator is missing");
}
 
Example 6
Source File: LocalStorageTest.java    From proarc with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testSetDatastreamProfile() throws Exception {
    LocalStorage storage = new LocalStorage();
    LocalObject lobject = storage.create();
    String dsID = "dsID";
    MediaType mime = MediaType.TEXT_PLAIN_TYPE;
    String label = "label";
    XmlStreamEditor editor = lobject.getEditor(FoxmlUtils.managedProfile(dsID, mime, label));
    assertNotNull(editor);
    assertNotNull(editor.getProfile());
    assertEquals(mime.toString(), editor.getProfile().getDsMIME());
    assertNull(editor.getProfile().getDsFormatURI());
    byte[] data = "data".getBytes("UTF-8");
    File attachment = tmp.newFile();
    editor.write(attachment.toURI(), 0, null);
    editor.write(new ByteArrayInputStream(data), editor.getLastModified(), null);
    lobject.flush();

    // update mimetype
    editor = lobject.getEditor(FoxmlUtils.managedProfile(dsID, mime, label));
    String expectedMimetype = MediaType.APPLICATION_JSON;
    DatastreamProfile profile = editor.getProfile();
    profile.setDsMIME(expectedMimetype);
    editor.setProfile(profile);
    editor.write(data, editor.getLastModified(), label);
    lobject.flush();

    // check with new editor
    editor = lobject.getEditor(FoxmlUtils.managedProfile(dsID, mime, label));
    assertEquals(expectedMimetype, editor.getProfile().getDsMIME());
}
 
Example 7
Source File: LocalStorageTest.java    From proarc with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testDatastreamEditorWriteStream_ManagedAsAttached() throws Exception {
    LocalStorage storage = new LocalStorage();
    LocalObject lobject = storage.create();
    String dsID = "dsID";
    MediaType mime = MediaType.TEXT_PLAIN_TYPE;
    String label = "label";
    XmlStreamEditor editor = lobject.getEditor(FoxmlUtils.managedProfile(dsID, mime, label));
    assertNotNull(editor);
    assertNotNull(editor.getProfile());
    assertEquals(mime.toString(), editor.getProfile().getDsMIME());
    assertNull(editor.getProfile().getDsFormatURI());
    byte[] data = "data".getBytes("UTF-8");
    File attachment = tmp.newFile();
    editor.write(attachment.toURI(), 0, null);
    editor.write(new ByteArrayInputStream(data), editor.getLastModified(), null);
    lobject.flush();

    // is managed
    DigitalObject dobj = lobject.getDigitalObject();
    DatastreamType ds = FoxmlUtils.findDatastream(dobj, dsID);
    assertNotNull(ds);
    assertEquals(ControlGroup.MANAGED.toExternal(), ds.getCONTROLGROUP());

    // is attached
    DatastreamVersionType dsv = FoxmlUtils.findDataStreamVersion(dobj, dsID);
    assertNotNull(dsv);
    assertEquals(mime.toString(), dsv.getMIMETYPE());
    assertEquals(label, dsv.getLABEL());
    assertNull(dsv.getBinaryContent());
    assertNotNull(dsv.getContentLocation());
    FileInputStream fis = new FileInputStream(attachment);
    byte[] readdata = new byte[data.length];
    assertEquals(data.length, fis.read(readdata));
    fis.close();
    assertArrayEquals(data, readdata);
}
 
Example 8
Source File: LocalStorageTest.java    From proarc with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testDatastreamEditorWriteStream_ManagedAsInlined() throws Exception {
    LocalStorage storage = new LocalStorage();
    LocalObject lobject = storage.create();
    String dsID = "dsID";
    MediaType mime = MediaType.TEXT_PLAIN_TYPE;
    String label = "label";
    XmlStreamEditor editor = lobject.getEditor(FoxmlUtils.managedProfile(dsID, mime, label));
    assertNotNull(editor);
    assertNotNull(editor.getProfile());
    assertEquals(mime.toString(), editor.getProfile().getDsMIME());
    assertNull(editor.getProfile().getDsFormatURI());
    byte[] data = "data".getBytes("UTF-8");
    editor.write(new ByteArrayInputStream(data), 0, null);
    lobject.flush();

    // is managed
    DigitalObject dobj = lobject.getDigitalObject();
    DatastreamType ds = FoxmlUtils.findDatastream(dobj, dsID);
    assertNotNull(ds);
    assertEquals(ControlGroup.MANAGED.toExternal(), ds.getCONTROLGROUP());

    // is inlined
    DatastreamVersionType dsv = FoxmlUtils.findDataStreamVersion(dobj, dsID);
    assertNotNull(dsv);
    assertEquals(mime.toString(), dsv.getMIMETYPE());
    assertEquals(label, dsv.getLABEL());
    assertArrayEquals(data, dsv.getBinaryContent());
    assertNull(dsv.getContentLocation());
}
 
Example 9
Source File: LocalStorageTest.java    From proarc with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testDatastreamEditorWriteBytes_ManagedAsAttached() throws Exception {
    LocalStorage storage = new LocalStorage();
    LocalObject lobject = storage.create();
    String dsID = "dsID";
    MediaType mime = MediaType.TEXT_PLAIN_TYPE;
    String label = "label";
    XmlStreamEditor editor = lobject.getEditor(FoxmlUtils.managedProfile(dsID, mime, label));
    assertNotNull(editor);
    assertNotNull(editor.getProfile());
    assertEquals(mime.toString(), editor.getProfile().getDsMIME());
    assertNull(editor.getProfile().getDsFormatURI());
    byte[] data = "data".getBytes("UTF-8");
    File attachment = tmp.newFile();
    editor.write(attachment.toURI(), 0, null);
    editor.write(data, editor.getLastModified(), null);
    lobject.flush();

    // is managed
    DigitalObject dobj = lobject.getDigitalObject();
    DatastreamType ds = FoxmlUtils.findDatastream(dobj, dsID);
    assertNotNull(ds);
    assertEquals(ControlGroup.MANAGED.toExternal(), ds.getCONTROLGROUP());

    // is attached
    DatastreamVersionType dsv = FoxmlUtils.findDataStreamVersion(dobj, dsID);
    assertNotNull(dsv);
    assertEquals(mime.toString(), dsv.getMIMETYPE());
    assertEquals(label, dsv.getLABEL());
    assertNull(dsv.getBinaryContent());
    assertNotNull(dsv.getContentLocation());
    FileInputStream fis = new FileInputStream(attachment);
    byte[] readdata = new byte[data.length];
    assertEquals(data.length, fis.read(readdata));
    fis.close();
    assertArrayEquals(data, readdata);
}
 
Example 10
Source File: RemoteStorageTest.java    From proarc with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testDatastreamEditorWriteStream_Managed() throws Exception {
    LocalStorage storage = new LocalStorage();
    LocalObject local = storage.create();
    local.setLabel(test.getMethodName());
    String dsID = "dsID";
    MediaType mime = MediaType.TEXT_PLAIN_TYPE;
    String label = "label";
    DatastreamProfile dsProfile = FoxmlUtils.managedProfile(dsID, mime, label);

    RemoteStorage fedora = new RemoteStorage(client);
    fedora.ingest(local, "junit");

    byte[] data = "data".getBytes("UTF-8");
    RemoteObject remote = fedora.find(local.getPid());
    XmlStreamEditor reditor = remote.getEditor(dsProfile);
    assertNotNull(reditor);
    reditor.write(new ByteArrayInputStream(data), reditor.getLastModified(), null);

    // test read cached
    InputStream is = reditor.readStream();
    assertNotNull(is);
    ByteArrayOutputStream resultData = new ByteArrayOutputStream();
    FoxmlUtils.copy(is, resultData);
    is.close();
    assertArrayEquals(data, resultData.toByteArray());
    remote.flush();

    // test remote read
    remote = fedora.find(local.getPid());
    reditor = remote.getEditor(dsProfile);
    is = reditor.readStream();
    assertNotNull(is);
    resultData = new ByteArrayOutputStream();
    FoxmlUtils.copy(is, resultData);
    is.close();
    assertArrayEquals(data, resultData.toByteArray());
}
 
Example 11
Source File: AbstractTemplate.java    From redpipe with Apache License 2.0 5 votes vote down vote up
public static MediaType parseMediaType(String extension) {
	// FIXME: bigger list, and override in config
	if(extension.equalsIgnoreCase("html"))
		return MediaType.TEXT_HTML_TYPE;
	if(extension.equalsIgnoreCase("xml"))
		return MediaType.APPLICATION_XML_TYPE;
	if(extension.equalsIgnoreCase("txt"))
		return MediaType.TEXT_PLAIN_TYPE;
	if(extension.equalsIgnoreCase("json"))
		return MediaType.APPLICATION_JSON_TYPE;
	System.err.println("Unknown extension type: "+extension);
	return MediaType.APPLICATION_OCTET_STREAM_TYPE;
}
 
Example 12
Source File: Rest.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public boolean init( StepMetaInterface smi, StepDataInterface sdi ) {
  meta = (RestMeta) smi;
  data = (RestData) sdi;

  if ( super.init( smi, sdi ) ) {
    data.resultFieldName = environmentSubstitute( meta.getFieldName() );
    data.resultCodeFieldName = environmentSubstitute( meta.getResultCodeFieldName() );
    data.resultResponseFieldName = environmentSubstitute( meta.getResponseTimeFieldName() );
    data.resultHeaderFieldName = environmentSubstitute( meta.getResponseHeaderFieldName() );

    // get authentication settings once
    data.realProxyHost = environmentSubstitute( meta.getProxyHost() );
    data.realProxyPort = Const.toInt( environmentSubstitute( meta.getProxyPort() ), 8080 );
    data.realHttpLogin = environmentSubstitute( meta.getHttpLogin() );
    data.realHttpPassword = Encr.decryptPasswordOptionallyEncrypted( environmentSubstitute( meta.getHttpPassword() ) );

    if ( !meta.isDynamicMethod() ) {
      data.method = environmentSubstitute( meta.getMethod() );
      if ( Utils.isEmpty( data.method ) ) {
        logError( BaseMessages.getString( PKG, "Rest.Error.MethodMissing" ) );
        return false;
      }
    }

    data.trustStoreFile = environmentSubstitute( meta.getTrustStoreFile() );
    data.trustStorePassword = environmentSubstitute( meta.getTrustStorePassword() );

    String applicationType = Const.NVL( meta.getApplicationType(), "" );
    if ( applicationType.equals( RestMeta.APPLICATION_TYPE_XML ) ) {
      data.mediaType = MediaType.APPLICATION_XML_TYPE;
    } else if ( applicationType.equals( RestMeta.APPLICATION_TYPE_JSON ) ) {
      data.mediaType = MediaType.APPLICATION_JSON_TYPE;
    } else if ( applicationType.equals( RestMeta.APPLICATION_TYPE_OCTET_STREAM ) ) {
      data.mediaType = MediaType.APPLICATION_OCTET_STREAM_TYPE;
    } else if ( applicationType.equals( RestMeta.APPLICATION_TYPE_XHTML ) ) {
      data.mediaType = MediaType.APPLICATION_XHTML_XML_TYPE;
    } else if ( applicationType.equals( RestMeta.APPLICATION_TYPE_FORM_URLENCODED ) ) {
      data.mediaType = MediaType.APPLICATION_FORM_URLENCODED_TYPE;
    } else if ( applicationType.equals( RestMeta.APPLICATION_TYPE_ATOM_XML ) ) {
      data.mediaType = MediaType.APPLICATION_ATOM_XML_TYPE;
    } else if ( applicationType.equals( RestMeta.APPLICATION_TYPE_SVG_XML ) ) {
      data.mediaType = MediaType.APPLICATION_SVG_XML_TYPE;
    } else if ( applicationType.equals( RestMeta.APPLICATION_TYPE_TEXT_XML ) ) {
      data.mediaType = MediaType.TEXT_XML_TYPE;
    } else {
      data.mediaType = MediaType.TEXT_PLAIN_TYPE;
    }
    try {
      setConfig();
    } catch ( Exception e ) {
      logError( BaseMessages.getString( PKG, "Rest.Error.Config" ), e );
      return false;
    }
    return true;
  }
  return false;
}
 
Example 13
Source File: PlainTextExamplesMessageBodyReaderTest.java    From vw-webservice with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void hugeFileTest() throws IOException {
	// pass the gzip inputstream to the string message body reader, and at
	// the same time, read from the file.
	// then compare the examples read in to verify that they match.

	// the input stream to read directly from the file
	GZIPInputStream gzipInputStream = new GZIPInputStream(this.getClass()
			.getClassLoader().getResourceAsStream("ner.train.gz"));

	BufferedReader testReader = new BufferedReader(new InputStreamReader(
			gzipInputStream));

	// the input stream that the PlainTextPredictionsMessageBodyReader will
	// use.
	GZIPInputStream gzipInputStreamForTestSubject = new GZIPInputStream(
			this.getClass().getClassLoader()
					.getResourceAsStream("ner.train.gz"));

	PlainTextExamplesMessageBodyReader toTest = new PlainTextExamplesMessageBodyReader();

	MediaType mediaType = MediaType.TEXT_PLAIN_TYPE;

	Iterable<Example> theIterableOfExamples = toTest.readFrom(null, null,
			null, mediaType, null, gzipInputStreamForTestSubject);

	int numExamples = 0;

	boolean dumpExamples = false; // turn on to see some examples

	for (Example example : theIterableOfExamples) {
		String expectedExample = testReader.readLine();

		Assert.assertEquals(expectedExample,
				example.getVWStringRepresentation());

		numExamples++;

		if (dumpExamples && numExamples % 21 == 0) // print every 21st
													// example
		{

			// TODO: get a jenkin's build going, turn on code coverage +
			// findbugs
			// etc etc

			LOGGER.debug("expected example: {}", expectedExample);
			LOGGER.debug("read example    : {}", example);
			LOGGER.debug("");
		}

	}

	Assert.assertTrue(testReader.readLine() == null); // ensure all examples
														// read and
														// verified.
}
 
Example 14
Source File: RemoteStorageTest.java    From proarc with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void testDatastreamEditorRewriteControlGroup() throws Exception {
    // prepare referenced contents
    byte[] data1 = "data1".getBytes("UTF-8");
    File file1 = tmp.newFile();
    FileUtils.writeByteArrayToFile(file1, data1);
    byte[] data2 = "data2".getBytes("UTF-8");
    File file2 = tmp.newFile();
    FileUtils.writeByteArrayToFile(file2, data2);

    LocalStorage storage = new LocalStorage();
    LocalObject local = storage.create();
    System.out.println(local.getPid());
    local.setLabel(test.getMethodName());
    String dsID = "dsID";
    String label = "label";
    MediaType mime1 = MediaType.APPLICATION_OCTET_STREAM_TYPE;
    DatastreamProfile dsProfile1 = FoxmlUtils.managedProfile(dsID, mime1, label);
    XmlStreamEditor leditor = local.getEditor(dsProfile1);
    assertNotNull(leditor);
    leditor.write(file1.toURI(), leditor.getLastModified(), null);
    local.flush();

    RemoteStorage fedora = new RemoteStorage(client);
    fedora.ingest(local, support.getTestUser());

    MediaType mime2 = MediaType.TEXT_PLAIN_TYPE;
    DatastreamProfile dsProfile2 = FoxmlUtils.externalProfile(dsID, mime2, label);
    RemoteObject remote = fedora.find(local.getPid());
    XmlStreamEditor reditor = remote.getEditor(dsProfile1);
    assertNotNull(reditor);
    reditor.setProfile(dsProfile2);
    reditor.write(file2.toURI(), reditor.getLastModified(), null);

    // test read cached
    InputStream is = reditor.readStream();
    assertNotNull(is);
    ByteArrayOutputStream resultData = new ByteArrayOutputStream();
    FoxmlUtils.copy(is, resultData);
    is.close();
    assertArrayEquals(data2, resultData.toByteArray());
    assertEquals(mime2.toString(), reditor.getProfile().getDsMIME());
    assertEquals(ControlGroup.EXTERNAL.toExternal(), reditor.getProfile().getDsControlGroup());

    // test remote read
    remote.flush();
    remote = fedora.find(local.getPid());
    reditor = remote.getEditor(dsProfile1);
    is = reditor.readStream();
    assertNotNull(is);
    resultData = new ByteArrayOutputStream();
    FoxmlUtils.copy(is, resultData);
    is.close();
    assertArrayEquals(data2, resultData.toByteArray());
    assertEquals(mime2.toString(), reditor.getProfile().getDsMIME());
    assertEquals(ControlGroup.EXTERNAL.toExternal(), reditor.getProfile().getDsControlGroup());
}
 
Example 15
Source File: RemoteStorageTest.java    From proarc with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void testDatastreamEditorWriteReference_External() throws Exception {
    LocalStorage storage = new LocalStorage();
    LocalObject local = storage.create();
    local.setLabel(test.getMethodName());
    String dsID = "dsID";
    MediaType mime = MediaType.TEXT_PLAIN_TYPE;
    String label = "label";
    DatastreamProfile dsProfile = FoxmlUtils.externalProfile(dsID, mime, label);

    RemoteStorage fedora = new RemoteStorage(client);
    fedora.ingest(local, support.getTestUser());

    // prepare referenced contents
    byte[] data = "data".getBytes("UTF-8");
    File file = tmp.newFile();
    FileUtils.writeByteArrayToFile(file, data);

    RemoteObject remote = fedora.find(local.getPid());
    XmlStreamEditor reditor = remote.getEditor(dsProfile);
    assertNotNull(reditor);
    reditor.write(file.toURI(), reditor.getLastModified(), "add");

    // test read cached
    InputStream is = reditor.readStream();
    assertNotNull(is);
    ByteArrayOutputStream resultData = new ByteArrayOutputStream();
    FoxmlUtils.copy(is, resultData);
    is.close();
    assertArrayEquals(data, resultData.toByteArray());
    assertEquals(mime.toString(), reditor.getProfile().getDsMIME());
    assertEquals(ControlGroup.EXTERNAL.toExternal(), reditor.getProfile().getDsControlGroup());

    // test remote read
    remote.flush();
    remote = fedora.find(local.getPid());
    reditor = remote.getEditor(dsProfile);
    is = reditor.readStream();
    assertNotNull(is);
    resultData = new ByteArrayOutputStream();
    FoxmlUtils.copy(is, resultData);
    is.close();
    assertArrayEquals(data, resultData.toByteArray());
    assertEquals(mime.toString(), reditor.getProfile().getDsMIME());
    assertEquals(ControlGroup.EXTERNAL.toExternal(), reditor.getProfile().getDsControlGroup());

    // test update MIME + location
    MediaType mime2 = MediaType.APPLICATION_OCTET_STREAM_TYPE;
    byte[] data2 = "data2".getBytes("UTF-8");
    File file2 = tmp.newFile();
    FileUtils.writeByteArrayToFile(file2, data2);
    remote = fedora.find(local.getPid());
    reditor = remote.getEditor(dsProfile);
    DatastreamProfile dsProfile2 = FoxmlUtils.externalProfile(dsID, mime2, label);
    reditor.setProfile(dsProfile2);
    reditor.write(file2.toURI(), reditor.getLastModified(), "update");
    remote.flush();

    remote = fedora.find(local.getPid());
    reditor = remote.getEditor(dsProfile);
    is = reditor.readStream();
    assertNotNull(is);
    resultData = new ByteArrayOutputStream();
    FoxmlUtils.copy(is, resultData);
    is.close();
    assertArrayEquals(data2, resultData.toByteArray());
    assertEquals(mime2.toString(), reditor.getProfile().getDsMIME());
    assertEquals(ControlGroup.EXTERNAL.toExternal(), reditor.getProfile().getDsControlGroup());
}
 
Example 16
Source File: RemoteStorageTest.java    From proarc with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void testDatastreamEditorWriteReference_Managed() throws Exception {
    LocalStorage storage = new LocalStorage();
    LocalObject local = storage.create();
    local.setLabel(test.getMethodName());
    String dsID = "dsID";
    MediaType mime = MediaType.TEXT_PLAIN_TYPE;
    String label = "label";
    DatastreamProfile dsProfile = FoxmlUtils.managedProfile(dsID, mime, label);

    RemoteStorage fedora = new RemoteStorage(client);
    fedora.ingest(local, "junit");

    // prepare referenced contents
    byte[] data = "data".getBytes("UTF-8");
    File file = tmp.newFile();
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(data);
    fos.close();

    RemoteObject remote = fedora.find(local.getPid());
    XmlStreamEditor reditor = remote.getEditor(dsProfile);
    assertNotNull(reditor);
    reditor.write(file.toURI(), reditor.getLastModified(), null);

    // test read cached
    InputStream is = reditor.readStream();
    assertNotNull(is);
    ByteArrayOutputStream resultData = new ByteArrayOutputStream();
    FoxmlUtils.copy(is, resultData);
    is.close();
    assertArrayEquals(data, resultData.toByteArray());
    remote.flush();

    // test remote read
    remote = fedora.find(local.getPid());
    reditor = remote.getEditor(dsProfile);
    is = reditor.readStream();
    assertNotNull(is);
    resultData = new ByteArrayOutputStream();
    FoxmlUtils.copy(is, resultData);
    is.close();
    assertArrayEquals(data, resultData.toByteArray());
}
 
Example 17
Source File: PlainTextExamplesMessageBodyReaderTest.java    From vw-webservice with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void spacesAtBeginningAndDifferentNewlinesTest()
		throws WebApplicationException, IOException {

	for (String newLineToUse : new String[] { "\n", "\r", "\r\n" }) {
		PlainTextExamplesMessageBodyReader toTest = new PlainTextExamplesMessageBodyReader();

		MediaType mediaType = MediaType.TEXT_PLAIN_TYPE;

		StringBuilder theExamples = new StringBuilder();
		theExamples.append("Example 1");
		theExamples.append(newLineToUse);
		theExamples.append("Example 2");
		theExamples.append(newLineToUse);
		theExamples.append("Example3 and 4 and 5");
		theExamples.append(newLineToUse);
		theExamples.append(newLineToUse);

		// note: data needs to be encoded using the correct char set, which
		// must match the mediatype.
		ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
				theExamples.toString().getBytes(
						ReaderWriter.getCharset(mediaType)));

		// the readFrom method only looks at the mediatype and the input
		// stream, so other params can be null.
		Iterable<Example> theIterable = toTest.readFrom(null, null, null,
				mediaType, null, byteArrayInputStream);

		int x = 0;
		for (Example example : theIterable) {
			switch (x++) {
			case 0:
				Assert.assertEquals("Example 1",
						example.getVWStringRepresentation());
				break;

			case 1:
				Assert.assertEquals("Example 2",
						example.getVWStringRepresentation());
				break;

			case 2:
				Assert.assertEquals("Example3 and 4 and 5",
						example.getVWStringRepresentation());
				break;

			case 3:
				Assert.assertEquals("", example.getVWStringRepresentation());
				break;

			default:
				Assert.fail();
			}
		}

		Assert.assertEquals(4, x);

	}

}
 
Example 18
Source File: BatchEEJAXRS2Client.java    From incubator-batchee with Apache License 2.0 4 votes vote down vote up
private MediaType findType(final Class<?> returnType) {
    return returnType.isPrimitive() ? MediaType.TEXT_PLAIN_TYPE : MediaType.APPLICATION_JSON_TYPE;
}
 
Example 19
Source File: Attachment.java    From cxf with Apache License 2.0 4 votes vote down vote up
public MediaType getContentType() {
    String value = handler != null && handler.getContentType() != null ? handler.getContentType()
        : headers.getFirst("Content-Type");
    return value == null ? MediaType.TEXT_PLAIN_TYPE : JAXRSUtils.toMediaType(value);
}
 
Example 20
Source File: AbstractParam.java    From atlas with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the media type of the error message entity.
 *
 * @return the media type of the error message entity
 */
protected MediaType mediaType() {
    return MediaType.TEXT_PLAIN_TYPE;
}