
Estoy creando una aplicación REST en el servidor Tomcat 7.
usando la clase de aplicación para bootstrap.
Implementación de la clase de aplicación de la siguiente manera. Creando un objeto singleton de Resource01.
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import com.somename.api.Resource01;
@ApplicationPath("/service")
public class AppConfig extends Application{
public Set<Object> all_resources;
@Override
public Set<Object> getSingletons()
{
all_resources = new HashSet<Object>();
Resource01 z = new Resource01();
all_resources.add(z);
return all_resources;
}
}
La clase de recurso es la siguiente.
Proporciono soporte para 2 métodos http PUT y GET.
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("resource")
public class Resource01 {
@GET
@Path("mailer_status")
@Produces(MediaType.TEXT_HTML)
public String getMailStatus()
{
return "<h2>Mailer is in progress 14.47</h2>";
}
@PUT
@Path("create_config")
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public String getMailConfig(String config)
{
//String instance_rep = m_config.toString();
return "Payload Delivered";
}
}
tomcat-users.xml es el siguiente.
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
<role rolename="manager-gui"/>
<user username="tomcat" password="tomcat" roles="manager-gui"/>
</tomcat-users>
El archivo está ubicado en %CATALINA_HOME%/conf.
Navegué un poco por el motivo del error y, en base a eso, modifiqué web.xml ubicado en %CATALINA_HOME%/webapps/manager/WEB-INF.
<security-constraint>
<web-resource-collection>
<web-resource-name>Admin</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
<http-method>PUT</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>manager-gui</role-name>
</auth-constraint>
<!-- Specifying a Secure Connection -->
<user-data-constraint>
<!-- transport-guarantee can be CONFIDENTIAL (forced SSL), INTEGRAL, or NONE -->
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>Tomcat Manager Application</realm-name>
</login-config>
luego exporto el proyecto web dinámico como un archivo war y lo implemento. para fines de prueba, se utiliza ARC (cliente de descanso avanzado).
Al principio intenté obtener la solicitud: se recibió la respuesta. captura de pantalla adjunta.
OBTENER solicitud
Como se puede ver en la captura de pantalla, se recibe una respuesta 200 OK. Puedo ver la cadena deseada en el navegador.
cuando intento realizar una solicitud PUT. Recibo el error 403 Prohibido. Adjunto algunas capturas de pantalla que describen lo mismo.
PUT_REQUEST_HEADER
PUT_REQUEST-CARGA ÚTIL
PUT_RESPONSE
Se agradece cualquier ayuda/orientación.
Saludos,
Ashish
Respuesta1
No eché un vistazo al web.xml guardado en la carpeta conf.
ahí teníamos esto
<security-constraint>
<web-resource-collection>
<web-resource-name>restricted methods</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method>TRACE</http-method>
<http-method>PUT</http-method>
<http-method>DELETE</http-method>
<http-method>OPTIONS</http-method>
</web-resource-collection>
<auth-constraint />
</security-constraint>
Comenté el método PUT.
<security-constraint>
<web-resource-collection>
<web-resource-name>restricted methods</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method>TRACE</http-method>
<!--<http-method>PUT</http-method>-->
<http-method>DELETE</http-method>
<http-method>OPTIONS</http-method>
</web-resource-collection>
<auth-constraint />
</security-constraint>
Y funcionó !