All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.glassfish.admin.rest.provider.FormWriter Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2024 Contributors to the Eclipse Foundation.
 * Copyright (c) 2009, 2018 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0, which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * This Source Code may also be made available under the following Secondary
 * Licenses when the conditions for such availability set forth in the
 * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
 * version 2 with the GNU Classpath Exception, which is available at
 * https://www.gnu.org/software/classpath/license.html.
 *
 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
 */

package org.glassfish.admin.rest.provider;

import jakarta.inject.Inject;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.UriInfo;
import jakarta.ws.rs.ext.MessageBodyWriter;
import jakarta.ws.rs.ext.Provider;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Set;

import org.jvnet.hk2.config.Dom;

import static java.nio.charset.StandardCharsets.UTF_8;

/**
 *
 * @author mh124079
 * @author Ludovic Champenois [email protected]
 */
@Produces(MediaType.APPLICATION_FORM_URLENCODED)
@Provider
public class FormWriter implements MessageBodyWriter {
    @Inject
    private UriInfo uriInfo;

    @Override
    public boolean isWriteable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) {
        return Dom.class.isAssignableFrom(type);
    }

    @Override
    public long getSize(Dom data, Class type, Type genericType, Annotation annotations[], MediaType mediaType) {
        return -1;
    }

    @Override
    public void writeTo(Dom data, Class type, Type genericType, Annotation[] annotations, MediaType mediaType,
            MultivaluedMap headers, OutputStream out) throws IOException {
        //        out.write(preamble.getBytes());
        //        for (String name : data.keySet()) {
        //            out.write("".getBytes());
        //            out.write(name.getBytes());
        //            out.write("".getBytes());
        //            out.write(data.get(name).getBytes());
        //            out.write("".getBytes());
        //        }
        //        out.write(postamble.getBytes());
        out.write(constructForm(data).getBytes(UTF_8));
    }

    private String constructForm(Dom data) {
        StringBuilder sb = new StringBuilder();
        sb.append("\n").append("Data\n")
                .append("

Change ").append(data.toString()).append(":

\n").append("
\n").append("\n"); Set ss = data.model.getAttributeNames(); for (String name : ss) { sb.append(" \n").append(" \n") .append(" \n").append(" \n"); } sb.append(" \n") .append("
").append(name).append(":
\n"); return sb.toString(); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy