Example usage for org.apache.commons.httpclient HttpStatus SC_CREATED

List of usage examples for org.apache.commons.httpclient HttpStatus SC_CREATED

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpStatus SC_CREATED.

Prototype

int SC_CREATED

To view the source code for org.apache.commons.httpclient HttpStatus SC_CREATED.

Click Source Link

Document

<tt>201 Created</tt> (HTTP/1.0 - RFC 1945)

Usage

From source file:org.eclipse.ecr.core.storage.sql.net.BinaryManagerClient.java

@Override
public Binary getBinary(InputStream in) throws IOException {
    Binary binary = binaryManager.getBinary(in);

    // also write to remote
    PostMethod m = new PostMethod(url + getQuery(binary.getDigest()));
    try {// www.j a  v a2s . c  o m
        BinaryRequestEntity writer = new BinaryRequestEntity(binary);
        m.setRequestEntity(writer);
        int status = httpClient.executeMethod(m);
        if (status != HttpStatus.SC_CREATED) {
            log.error(String.format("Could not create remote binary on server %s (%s)", url,
                    String.valueOf(status)));
        }
    } catch (IOException e) {
        log.error(String.format("Could not create remote binary on server %s (%s)", url, e.toString()), e);
    } finally {
        m.releaseConnection();
    }

    return binary;
}

From source file:org.eclipse.lyo.testsuite.oslcv2.asset.AssetTestBase.java

/**
 * Creates an asset and then asserts that it was created. If all goes well the url to
 * the created asset is returned/*  ww w.  j a va 2  s.c om*/
 */
protected String createAsset(String content) throws IOException {
    HttpResponse resp = OSLCUtils.postDataToUrl(currentUrl, creds, acceptType, contentType, content, headers);
    EntityUtils.consume(resp.getEntity());
    assertTrue("Expected: " + HttpStatus.SC_CREATED + ", received: " + resp.getStatusLine().getStatusCode(),
            HttpStatus.SC_CREATED == resp.getStatusLine().getStatusCode());

    Header loc = resp.getFirstHeader("Location");
    return loc.getValue();
}

From source file:org.eclipse.lyo.testsuite.oslcv2.asset.GetAndUpdateBase.java

/**
 * Uploads the artifact set in the property file
 * @param fileName/* w  w w  .j a  v a 2 s.c  om*/
 * @param artifactFactory
 * @return the url location of the artifact
 */
protected String uploadArtifact(String artifactFactory) throws IOException {
    File file = new File(setupProps.getProperty("artifactContentType"));
    Header h = new BasicHeader("oslc_asset.name", file.getName());

    HttpResponse resp = OSLCUtils.postDataToUrl(artifactFactory, creds, acceptType,
            setupProps.getProperty("artifactContentType"), readFileFromProperty("artifactFile"), addHeader(h));
    EntityUtils.consume(resp.getEntity());
    assertTrue("Expected " + HttpStatus.SC_OK + ", received " + resp.getStatusLine().getStatusCode(),
            resp.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED);

    assertTrue("No Location header", resp.getFirstHeader("Location") != null);
    assertTrue("No content length header", resp.getFirstHeader("Content-Length") != null);
    return resp.getFirstHeader("Location").getValue();
}

From source file:org.eclipse.lyo.testsuite.oslcv2.asset.GetAndUpdateJsonTests.java

@Test
public void addArtifactToAsset() throws IOException, ParseException, ParserConfigurationException, SAXException,
        IllegalStateException, JSONException {
    // Get the asset to add the artifact too
    String resp = getAssetAsString();

    JSONObject asset = new JSONObject(resp);
    JSONObject factory = (JSONObject) asset.get("oslc_asset:artifactFactory");
    String artifactFactory = factory.getString("rdf:resource");
    assertTrue("There needs to be an artifact factory url",
            artifactFactory != null && artifactFactory.length() > 0);

    Header[] header = addHeader(new BasicHeader("oslc_asset.name", "/helpFolder/help"));

    String fileName = setupProps.getProperty("createTemplateArtifactXmlFile");
    assertTrue("There needs to be an artifact template file", fileName != null);
    String artifact = OSLCUtils.readFileByNameAsString(fileName);

    HttpResponse response = OSLCUtils.postDataToUrl(artifactFactory, creds, OSLCConstants.CT_JSON,
            OSLCConstants.CT_JSON, artifact, header);
    EntityUtils.consume(response.getEntity());
    assertTrue("Expected " + HttpStatus.SC_OK + ", received " + response.getStatusLine().getStatusCode(),
            response.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED);
}

From source file:org.eclipse.lyo.testsuite.oslcv2.asset.GetAndUpdateRdfXmlTests.java

@Test
public void addArtifactToAsset() throws IOException {
    String artifactFactory = getArtifactFactory();
    Header[] header = addHeader(new BasicHeader("oslc_asset.name", "/helpFolder/help"));

    String fileName = setupProps.getProperty("createTemplateArtifactRdfXmlFile");
    if (fileName == null) // Fall back to the xml if the rdf is not defined
        fileName = setupProps.getProperty("createTemplateArtifactXmlFile");

    assertTrue("There needs to be an artifact template file", fileName != null);
    String artifact = OSLCUtils.readFileByNameAsString(fileName);

    HttpResponse resp = OSLCUtils.postDataToUrl(artifactFactory, creds, OSLCConstants.CT_RDF, null, artifact,
            header);/*from  w w w  .j a v a2  s  .c om*/
    EntityUtils.consume(resp.getEntity());
    assertTrue("Expected: " + HttpStatus.SC_CREATED + ", received: " + resp.getStatusLine().getStatusCode(),
            HttpStatus.SC_CREATED == resp.getStatusLine().getStatusCode());
}

From source file:org.eclipse.lyo.testsuite.oslcv2.asset.GetAndUpdateXmlTests.java

@Test
public void addArtifactToAsset() throws IOException, ParseException, ParserConfigurationException, SAXException,
        XPathExpressionException {
    String artifactFactory = getArtifactFactory();
    Header[] header = addHeader(new BasicHeader("oslc_asset.name", "/helpFolder/help"));

    String fileName = setupProps.getProperty("createTemplateArtifactXmlFile");
    assertTrue("There needs to be an artifact template file", fileName != null);
    String artifact = OSLCUtils.readFileByNameAsString(fileName);

    HttpResponse response = OSLCUtils.postDataToUrl(artifactFactory, creds, OSLCConstants.CT_XML,
            OSLCConstants.CT_XML, artifact, header);
    EntityUtils.consume(response.getEntity());
    assertTrue("Expected " + HttpStatus.SC_CREATED + ", received " + response.getStatusLine().getStatusCode(),
            response.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED);
}

From source file:org.eclipse.lyo.testsuite.oslcv2.asset.UsageCaseJsonTests.java

@Test
public void publishUsageCase() throws IOException, NullPointerException, XPathException,
        ParserConfigurationException, SAXException, JSONException {
    //Gets the asset creation services
    ArrayList<String> serviceUrls = getServiceProviderURLsUsingJson(setupProps.getProperty("baseUri"),
            onlyOnce);/*from w w  w.jav  a2 s.c  o  m*/
    ArrayList<String> creationUrls = TestsBase.getCapabilityURLsUsingRdfXml(OSLCConstants.CREATION_PROP,
            serviceUrls, useDefaultUsageForCreation, null);
    currentUrl = creationUrls.get(0);

    // Creates the asset
    assetUrl = createAsset(jsonCreateTemplate);
    assertTrue("The location of the asset after it was create was not returned", assetUrl != null);

    // Gets the created asset
    String resp = getAssetAsString();
    JSONObject asset = new JSONObject(resp);
    // Gets the artifact factory from the asset
    JSONObject factory = (JSONObject) asset.get("oslc_asset:artifactFactory");
    String artifactFactory = baseUrl + factory.getString("rdf:resource");
    assertTrue("There needs to be an artifact factory",
            artifactFactory != null && artifactFactory.length() > 0);

    // Adds an artifact to the asset
    File file = new File(setupProps.getProperty("artifactContentType"));
    BasicHeader h = new BasicHeader("oslc_asset.name", file.getName());

    HttpResponse response = OSLCUtils.postDataToUrl(artifactFactory, creds, acceptType,
            setupProps.getProperty("artifactContentType"), readFileFromProperty("artifactFile"), addHeader(h));
    EntityUtils.consume(response.getEntity());
    assertTrue("Expected " + HttpStatus.SC_OK + ", received " + response.getStatusLine().getStatusCode(),
            response.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED);

    assertTrue("No Location header", response.getFirstHeader("Location") != null);

    // Updates the artifacts subject
    resp = getAssetAsString();
    asset = new JSONObject(resp);
    JSONArray artifacts = asset.getJSONArray("oslc_asset:artifact");
    JSONObject artifact = artifacts.getJSONObject(0);
    String labelValue = "updated subject";
    artifact.put("oslc:label", labelValue);
    String content = JSONToString(asset);

    // Update the asset
    putAsset(content);
    resp = getAssetAsString();
    asset = new JSONObject(resp);
    artifacts = asset.getJSONArray("oslc_asset:artifact");
    artifact = artifacts.getJSONObject(0);
    assertEquals("The label value was not set", labelValue, artifact.getString("oslc:label"));
}

From source file:org.eclipse.lyo.testsuite.oslcv2.asset.UsageCaseRdfXmlTests.java

@Test
public void publishUsageCase() throws IllegalStateException, IOException {
    // Get url//from  w w w  . java2s.c  o  m
    ArrayList<String> serviceUrls = getServiceProviderURLsUsingRdfXml(setupProps.getProperty("baseUri"),
            onlyOnce);
    ArrayList<String> capabilityURLsUsingRdfXml = TestsBase.getCapabilityURLsUsingRdfXml(
            OSLCConstants.CREATION_PROP, serviceUrls, useDefaultUsageForCreation, null);
    currentUrl = capabilityURLsUsingRdfXml.get(0);

    // Creates the asset
    assetUrl = createAsset(rdfXmlCreateTemplate);
    assertTrue("The location of the asset after it was create was not returned", assetUrl != null);
    baseUrl = setupProps.getProperty("baseUrl");

    HttpResponse resp = getAssetResponse();

    Model model = ModelFactory.createDefaultModel();
    model.read(resp.getEntity().getContent(), baseUrl);
    EntityUtils.consume(resp.getEntity());

    // Gets the artifact factory from the asset
    String artifactFactory = getPropertyValue(model, OSLCConstants.ASSET_ARTIFACT_FACTORY_PROP);
    assertTrue("There needs to be an artifact factory",
            artifactFactory != null && artifactFactory.length() > 0);
    Header[] header = addHeader(new BasicHeader("oslc_asset.name", "/helpFolder/help"));

    // Creates the artifact
    String fileName = setupProps.getProperty("createTemplateArtifactRdfXmlFile");
    if (fileName == null) // Fall back to the xml if the rdf is not defined
        //fileName = setupProps.getProperty("createTemplateArtifactXmlFile");
        fileName = setupProps.getProperty("createTemplateXmlFile");

    assertTrue("There needs to be an artifact template file", fileName != null);
    String artifact = OSLCUtils.readFileByNameAsString(fileName);

    resp = OSLCUtils.postDataToUrl(artifactFactory, creds, OSLCConstants.CT_RDF, null, artifact, header);
    EntityUtils.consume(resp.getEntity());
    assertTrue("Expected: " + HttpStatus.SC_CREATED + ", received: " + resp.getStatusLine().getStatusCode(),
            HttpStatus.SC_CREATED == resp.getStatusLine().getStatusCode());

    // Get and updates the artifacts subject
    resp = getAssetResponse();

    model = ModelFactory.createDefaultModel();
    model.read(resp.getEntity().getContent(), baseUrl);
    EntityUtils.consume(resp.getEntity());

    // TODO make this so that if the label is not there it is added
    Property artifactProp = model.getProperty(OSLCConstants.ASSET_ARTIFACT_PROP);
    String labelValue = "this subject has been changed";
    Selector selectArtifact = new SimpleSelector(null, artifactProp, (RDFNode) null);
    StmtIterator artifactStatements = model.listStatements(selectArtifact);
    List<Statement> statementList = artifactStatements.toList();
    for (int i = 0; i < statementList.size(); i++) {
        Statement artifactStatement = statementList.get(i);
        Property prop = model.createProperty(OSLCConstants.LABEL_PROP);
        setPropertyValue(artifactStatement, prop, labelValue);
    }

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    model.write(output);
    String content = output.toString();
    putAsset(content);

    // Checks the validity of the put
    resp = getAssetResponse();
    model = ModelFactory.createDefaultModel();
    model.read(resp.getEntity().getContent(), baseUrl);
    EntityUtils.consume(resp.getEntity());

    selectArtifact = new SimpleSelector(null, artifactProp, (RDFNode) null);
    artifactStatements = model.listStatements(selectArtifact);
    statementList = artifactStatements.toList();
    for (int i = 0; i < statementList.size(); i++) {
        Statement artifactStatement = statementList.get(i);
        Property prop = model.createProperty(OSLCConstants.LABEL_PROP);
        setPropertyValue(artifactStatement, prop, labelValue);
        StmtIterator statements = artifactStatement.getResource().listProperties(prop);
        assertTrue("No label property was found", statements.hasNext());
        assertEquals(labelValue, statements.next().getObject().toString());
    }
}

From source file:org.eclipse.lyo.testsuite.oslcv2.asset.UsageCaseXmlTests.java

@Test
public void publishUsageCase() throws IOException, ParseException, ParserConfigurationException, SAXException,
        TransformerException, XPathException {
    // Get url/*from   w w w . j  a v  a2 s  .c om*/
    ArrayList<String> serviceUrls = getServiceProviderURLsUsingXML(setupProps.getProperty("baseUri"));
    ArrayList<String> capabilityURLsUsingRdfXml = TestsBase.getCapabilityURLsUsingRdfXml(
            OSLCConstants.CREATION_PROP, serviceUrls, useDefaultUsageForCreation, null);
    currentUrl = capabilityURLsUsingRdfXml.get(0);

    // Create the asset      
    assetUrl = createAsset(xmlCreateTemplate);
    assertTrue("The location of the asset after it was create was not returned", assetUrl != null);

    // Add the artifact to the asset
    String artifactFactory = getArtifactFactory();

    Header[] header = addHeader(new BasicHeader("oslc_asset.name", "/helpFolder/help"));

    //String fileName = setupProps.getProperty("createTemplateArtifactXmlFile");
    String fileName = setupProps.getProperty("createTemplateXmlFile");
    assertTrue("There needs to be an artifact template file", fileName != null);
    String artifact = OSLCUtils.readFileByNameAsString(fileName);

    HttpResponse response = OSLCUtils.postDataToUrl(artifactFactory, creds, OSLCConstants.CT_XML,
            OSLCConstants.CT_XML, artifact, header);
    EntityUtils.consume(response.getEntity());
    assertTrue("Expected " + HttpStatus.SC_OK + ", received " + response.getStatusLine().getStatusCode(),
            response.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED);

    // Get updated asset and update the artifact
    HttpResponse resp = getAssetResponse();
    String content = EntityUtils.toString(resp.getEntity());
    Document document = OSLCUtils.createXMLDocFromResponseBody(content);
    EntityUtils.consume(resp.getEntity());
    String path = "/rdf:RDF/oslc_asset:Asset/oslc_asset:artifact[1]/oslc_asset:Artifact";
    XPath xpath = OSLCUtils.getXPath();
    Node artifactNode = (Node) xpath.evaluate(path, document, XPathConstants.NODE);

    NodeList artifactKids = artifactNode.getChildNodes();
    Node label = null;
    for (int i = 0; i < artifactKids.getLength(); i++) {
        if (artifactKids.item(i).getNodeName().equals("oslc:label")) {
            label = artifactKids.item(i);
            break;
        }
    }

    String labelValue = "this value was changed";
    if (label == null) {
        label = document.createElement("oslc:label");
        label.setTextContent(labelValue);
        artifactNode.appendChild(label);
    } else {
        label.setTextContent(labelValue);
    }

    // Update asset
    content = OSLCUtils.createStringFromXMLDoc(document);
    putAsset(content);

    // Check to see if the label was updated
    resp = getAssetResponse();
    content = EntityUtils.toString(resp.getEntity());
    document = OSLCUtils.createXMLDocFromResponseBody(content);
    EntityUtils.consume(resp.getEntity());
    path = "/rdf:RDF/oslc_asset:Asset/oslc_asset:artifact[1]/oslc_asset:Artifact/oslc:label";
    xpath = OSLCUtils.getXPath();
    label = (Node) xpath.evaluate(path, document, XPathConstants.NODE);
    assertTrue("Could not find the artifact's label node", label != null);
    assertEquals("The label was not updated properly", labelValue, label.getTextContent());
}

From source file:org.eclipse.lyo.testsuite.oslcv2.CreationAndUpdateBaseTests.java

protected void createValidResourceUsingTemplate(String contentType, String accept, String content)
        throws IOException {
    // Issue post request using the provided template
    HttpResponse resp = OSLCUtils.postDataToUrl(currentUrl, basicCreds, accept, contentType, content, headers);

    // Assert the response gave a 201 Created
    String responseBody = EntityUtils.toString(resp.getEntity());
    EntityUtils.consume(resp.getEntity());
    assertEquals(responseBody, HttpStatus.SC_CREATED, resp.getStatusLine().getStatusCode());
    Header location = resp.getFirstHeader("Location");
    // Assert that we were given a Location header pointing to the resource,
    // which is not a MUST according to oslc v2, but probably should be
    // present//from  w w w.  j a  v a  2s  .co  m
    // none the less.
    assertFalse(location == null);
    // Attempt to clean up after the test by calling delete on the given
    // url,
    // which is not a MUST according to the oslc cm spec
    resp = OSLCUtils.deleteFromUrl(location.getValue(), basicCreds, "*/*");
    if (resp.getEntity() != null) {
        EntityUtils.consume(resp.getEntity());
    }
}