Search in sources :

Example 6 with RepositoryException

use of org.apache.sling.ide.transport.RepositoryException in project sling by apache.

the class JcrNewNodeHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    ISelection sel = HandlerUtil.getCurrentSelection(event);
    JcrNode node = SelectionUtils.getFirst(sel, JcrNode.class);
    if (node == null) {
        return null;
    }
    Shell shell = HandlerUtil.getActiveShell(event);
    if (!node.canCreateChild()) {
        MessageDialog.openInformation(shell, "Cannot create node", "Node is not covered by the workspace filter as defined in filter.xml");
        return null;
    }
    Repository repository = ServerUtil.getDefaultRepository(node.getProject());
    NodeTypeRegistry ntManager = (repository == null) ? null : repository.getNodeTypeRegistry();
    if (ntManager == null) {
        if (!doNotAskAgain) {
            MessageDialog dialog = new MessageDialog(null, "Unable to validate node type", null, "Unable to validate node types since project " + node.getProject().getName() + " is not associated with a server or the server is not started.", MessageDialog.QUESTION_WITH_CANCEL, new String[] { "Cancel", "Continue (do not ask again)", "Continue" }, 1) {

                @Override
                protected void configureShell(Shell shell) {
                    super.configureShell(shell);
                    setShellStyle(getShellStyle() | SWT.SHEET);
                }
            };
            int choice = dialog.open();
            if (choice <= 0) {
                return null;
            }
            if (choice == 1) {
                doNotAskAgain = true;
            }
        }
    }
    final NodeType nodeType = node.getNodeType();
    if (nodeType != null && nodeType.getName() != null && nodeType.getName().equals("nt:file")) {
        MessageDialog.openInformation(shell, "Cannot create node", "Node of type nt:file cannot have children");
        return null;
    }
    try {
        final NewNodeDialog nnd = new NewNodeDialog(shell, node, ntManager);
        if (nnd.open() == IStatus.OK) {
            node.createChild(nnd.getValue(), nnd.getChosenNodeType());
            return null;
        }
    } catch (RepositoryException e1) {
        Activator.getDefault().getPluginLogger().warn("Could not open NewNodeDialog due to " + e1, e1);
    }
    return null;
}
Also used : Shell(org.eclipse.swt.widgets.Shell) Repository(org.apache.sling.ide.transport.Repository) JcrNode(org.apache.sling.ide.eclipse.ui.nav.model.JcrNode) NodeType(javax.jcr.nodetype.NodeType) ISelection(org.eclipse.jface.viewers.ISelection) NodeTypeRegistry(org.apache.sling.ide.transport.NodeTypeRegistry) RepositoryException(org.apache.sling.ide.transport.RepositoryException) MessageDialog(org.eclipse.jface.dialogs.MessageDialog)

Example 7 with RepositoryException

use of org.apache.sling.ide.transport.RepositoryException in project sling by apache.

the class DeleteNodeCommand method execute.

@Override
public Result<Void> execute() {
    PostMethod post = new PostMethod(getPath());
    try {
        Part[] parts = { new StringPart(":operation", "delete") };
        post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
        httpClient.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(repositoryInfo.getUsername(), repositoryInfo.getPassword()));
        httpClient.getParams().setAuthenticationPreemptive(true);
        int responseStatus = httpClient.executeMethod(post);
        return resultForResponseStatus(responseStatus);
    } catch (Exception e) {
        return AbstractResult.failure(new RepositoryException(e));
    } finally {
        post.releaseConnection();
    }
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) Part(org.apache.commons.httpclient.methods.multipart.Part) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) RepositoryException(org.apache.sling.ide.transport.RepositoryException) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) RepositoryException(org.apache.sling.ide.transport.RepositoryException) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 8 with RepositoryException

use of org.apache.sling.ide.transport.RepositoryException in project sling by apache.

the class GetNodeContentCommand method execute.

@Override
public Result<ResourceProxy> execute() {
    GetMethod get = new GetMethod(getPath());
    try {
        httpClient.getParams().setAuthenticationPreemptive(true);
        Credentials defaultcreds = new UsernamePasswordCredentials(repositoryInfo.getUsername(), repositoryInfo.getPassword());
        httpClient.getState().setCredentials(new AuthScope(repositoryInfo.getHost(), repositoryInfo.getPort(), AuthScope.ANY_REALM), defaultcreds);
        int responseStatus = httpClient.executeMethod(get);
        // return EncodingUtil.getString(rawdata, m.getResponseCharSet());
        if (!isSuccessStatus(responseStatus))
            return failureResultForStatusCode(responseStatus);
        ResourceProxy resource = new ResourceProxy(path);
        try (JsonReader jsonReader = new JsonReader(new InputStreamReader(get.getResponseBodyAsStream(), get.getResponseCharSet()))) {
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                String name = jsonReader.nextName();
                JsonToken token = jsonReader.peek();
                if (token == JsonToken.STRING) {
                    resource.addProperty(name, jsonReader.nextString());
                } else {
                    jsonReader.skipValue();
                }
            }
            jsonReader.endObject();
        }
        return AbstractResult.success(resource);
    } catch (Exception e) {
        return AbstractResult.failure(new RepositoryException(e));
    } finally {
        get.releaseConnection();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) GetMethod(org.apache.commons.httpclient.methods.GetMethod) AuthScope(org.apache.commons.httpclient.auth.AuthScope) JsonReader(com.google.gson.stream.JsonReader) JsonToken(com.google.gson.stream.JsonToken) RepositoryException(org.apache.sling.ide.transport.RepositoryException) Credentials(org.apache.commons.httpclient.Credentials) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) ResourceProxy(org.apache.sling.ide.transport.ResourceProxy) RepositoryException(org.apache.sling.ide.transport.RepositoryException) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 9 with RepositoryException

use of org.apache.sling.ide.transport.RepositoryException in project sling by apache.

the class UpdateContentCommand method execute.

@Override
public Result<Void> execute() {
    PostMethod post = new PostMethod(getPath());
    try {
        List<Part> parts = new ArrayList<>();
        for (Map.Entry<String, Object> property : properties.entrySet()) {
            if (ProtectedNodes.exists(property.getKey())) {
                continue;
            }
            Object propValue = property.getValue();
            if (propValue instanceof String) {
                parts.add(new StringPart(property.getKey(), (String) propValue));
            } else if (property != null) {
                // TODO handle multi-valued properties
                System.err.println("Unable to handle property " + property.getKey() + " of type " + property.getValue().getClass());
            }
        }
        File f = new File(fileInfo.getLocation());
        if (f.isFile()) {
            parts.add(new FilePart(fileInfo.getName(), f));
        }
        post.setRequestEntity(new MultipartRequestEntity(parts.toArray(new Part[parts.size()]), post.getParams()));
        httpClient.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(repositoryInfo.getUsername(), repositoryInfo.getPassword()));
        httpClient.getParams().setAuthenticationPreemptive(true);
        int responseStatus = httpClient.executeMethod(post);
        return resultForResponseStatus(responseStatus);
    } catch (Exception e) {
        return AbstractResult.failure(new RepositoryException(e));
    } finally {
        post.releaseConnection();
    }
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) ArrayList(java.util.ArrayList) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) RepositoryException(org.apache.sling.ide.transport.RepositoryException) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) RepositoryException(org.apache.sling.ide.transport.RepositoryException) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) Part(org.apache.commons.httpclient.methods.multipart.Part) Map(java.util.Map) File(java.io.File)

Example 10 with RepositoryException

use of org.apache.sling.ide.transport.RepositoryException in project sling by apache.

the class JcrNode method getSerializationKind.

private SerializationKind getSerializationKind(String nodeType) {
    final SerializationKindManager skm = new SerializationKindManager();
    final Repository repo = ServerUtil.getDefaultRepository(getProject());
    if (repo == null) {
        return getFallbackSerializationKind(nodeType);
    }
    try {
        skm.init(repo);
        //TODO: mixins not yet supported
        return skm.getSerializationKind(nodeType, new ArrayList<String>());
    } catch (RepositoryException e) {
        e.printStackTrace();
        return getFallbackSerializationKind(nodeType);
    }
}
Also used : Repository(org.apache.sling.ide.transport.Repository) RepositoryException(org.apache.sling.ide.transport.RepositoryException) SerializationKindManager(org.apache.sling.ide.serialization.SerializationKindManager)

Aggregations

RepositoryException (org.apache.sling.ide.transport.RepositoryException)13 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)5 Repository (org.apache.sling.ide.transport.Repository)5 NodeTypeRegistry (org.apache.sling.ide.transport.NodeTypeRegistry)4 Credentials (org.apache.commons.httpclient.Credentials)3 AuthScope (org.apache.commons.httpclient.auth.AuthScope)3 GetMethod (org.apache.commons.httpclient.methods.GetMethod)3 SerializationKindManager (org.apache.sling.ide.serialization.SerializationKindManager)3 ResourceProxy (org.apache.sling.ide.transport.ResourceProxy)3 CoreException (org.eclipse.core.runtime.CoreException)3 JsonReader (com.google.gson.stream.JsonReader)2 JsonToken (com.google.gson.stream.JsonToken)2 InputStreamReader (java.io.InputStreamReader)2 ArrayList (java.util.ArrayList)2 PostMethod (org.apache.commons.httpclient.methods.PostMethod)2 MultipartRequestEntity (org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity)2 Part (org.apache.commons.httpclient.methods.multipart.Part)2 StringPart (org.apache.commons.httpclient.methods.multipart.StringPart)2 SerializationKind (org.apache.sling.ide.serialization.SerializationKind)2 IStatus (org.eclipse.core.runtime.IStatus)2